I need to translate a label of an active form without changing the model.
This:
$form->label($model, 'myField', array('class' => 'title'))
Takes the translation by default from the translation.php in the protected/messages/lang/ folder.
Now I want the label to take the translation from another file (without changing the model).
How would I do that? The docs are a little vague on what kind of variables I can pass to the label...
You can specify label in the $htmlOptions array:
http://www.yiiframework.com/doc/api/1.1/CHtml#activeLabel-detail
$form->label(
$model,
'myField',
array('class' => 'title', 'label' => Yii::t('myCategory', 'Field label'))
)
Yii::t() method translates the given message from source language to target language. You can read more information about internationalization here:
http://www.yiiframework.com/doc/api/1.1/YiiBase#t-detail
http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n
Related
In my blade I have this line of code:
{!! Form::file('motivation', old('motivation'), ['id' => 'inputGroupMotivation', 'class' => 'custom-file-input']) !!}
As you can see i set an id by doing 'id' => 'inputGroupMotivation'. However when I go to the page where this blade is rendered it outputs this:
<input name="motivation" type="file">
How come it does not take the id and class attributes I've set in my code?
I have not personally used the Form facade in Laravel, however from briefly looking at the documentation, it seems that the syntax for the file method is a bit different from regular input methods such as text. Namely, the second parameter is not the old value, but the list of attributes you would want to pass.
In your case you would this code instead:
{!! Form::file('motivation', ['id' => 'inputGroupMotivation', 'class' => 'custom-file-input']) !!}
For more information, take a look at the LaravelCollective's documentation page.
I am building a web application using ZF2 and Doctrine. I have a view containing a base form to which the user can add multiple instances of a fieldset, the filedsets are added via HTML template and js cloning. We are making use of the Doctrine hydrator and cascade=persist to write to the dB. It is all working but I am concerned when the fieldsets are added it results in multiple items with the same ID which breaks w3 standards. Has anyone a solution or work around for this? Or would it be considered acceptable in this instance?
An example of one fieldset element:
$this->add(array(
'name' => 'glassAssemblyID',
'attributes' => array(
'type'=> 'hidden',
'id' => 'glassAssemblyID',
),
));
Many thanks
James
You should set the ID in JavaScript after cloning the element.
This is an easy one. Just just change your code to:
$this->add(array(
//'name' => 'glassAssemblyID',
'attributes' => array(
'type'=> 'hidden',
//'id' => 'glassAssemblyID',
),
));
No point in putting out an element id which is obviously not being used.
If you really feel you do need ids for some reason then put out something like EntityType-id for your ids.
I'm wondering how can I dynamically set the HTML class attribute to checkbox elements using FormBuilder in Symfony2.
I'm using an element generated by an entity :
add('myEntity', 'entity', array(
'class' => 'MyBundle:myEntity',
'property' => 'label',
'multiple' => true,
'expanded' => true,
))
This only sets the id of the checkboxes according to this template : [form_name]_[class_name]_[id] (for example acme_my_form_my_entity_5)
I want to know if I can also set the class attribute with these values : [form_name][class_name][label]. Label is another field in the entity.
Is this possible ?
Yes. Is possible, but you will need to read the Docs!
Form Customization
If you are lazy and do not want to read them, then just search google using "custom checkboxes symfony" and you will come back to Stackoverflow, where there are many other examples like this. Replying with code will be just a copy of another examples, so you better build your own customized one and paste it as your own solution.
If you want an easy solution, keep in mind you can to something like this directly in your Twig template:
{{ form_widget(form.myEntity, { 'attr': {'class': **Your_dinamic_class_value**} }) }}
In Symfony2, I'm using formbuilder. I'm setting the labels in the form, as per the documentation.
However, when I'm on the 'show' and 'index' pages, I have to copy the labels into Twig.
Is there a way to have the same labels used everywhere? The options I have thought of:
Access the formbuilder configuration, but without actually building the form
Have a central config file, and lookup from the formbuilder and the twig files into that file
However, either way requires me to 'do' something, which I'm not used to in Symfony. It seems like this is something that would already have been solved, but I'm not sure how.
You can utilize translation system to overcome this problem. Make sure that you have enabled translation in config.yml.
If you have added field in your formtype like this
$builder->add('title', 'text', array(
'label'=> 'model.title'
));
//.....
Create a file named messages.en.yml in your bundles Resources/translations directory (replace en with your default locale and create multiple files based on the locales. Check translation chapter of the book.) and put following
#src/YourBundle/Resources/translation/messages.en.yml
model:
title: "Title"
field: "Field"
#....
Add and edit forms label will show Title. In index and show pages you can do
{{ "model.title" | trans([], 'messages') }}
Though this process is a bit lengthy but it is one time and you can change the value of the labels by changing the translation files.
I just want to say something about #Mun Mun Das's answer.
In Symfony 6, it looks like we don't even need to use trans() anymore in the template in the form. I defined the label in the Type file (php) and it translates directly. I don't know why though.
Example
FormatType
#src/Form/UserType.php
$builder->add('uEmail', TextType::class, array(
'label'=> 'uEmail' ,
'attr' => [ 'maxlength' => 50]
))
Translation file
#translations/message.en.yaml
uEmail: Email
It automatically translates it. I guess Symfony try to translate it by default ? If label is not set in UserType.php or in the template with form_label() function, it will display "u Email" by default
I'm trying to modify in my Twig template the labels of the choices that I have for a choice field in my form. I create my field as usual in PHP:
->add('registered','choice',array('choices' => array('1' => 'Yes', '0' => 'No'),'expanded' => true))
and then in my template if a condition is true then I want to do something like this:
{{ form_widget(form.registered,{'choices':{'1':'Ναι','0':'Όχι'}})}}
So the idea would be that instead of Yes and No, I would get it in Greek. My problem is that I can't figure out the right attribute to pass to the form_widget function, I've tried with 'choices' but it doesn't seem to work.
Put Yes and No in a global message translation domain and Symfony will translate them automatically. In 2.1 you will be able to put translations for entities into different domains, not only into default one.