I'm using the FormBuilder to create my form. That works fine. The problem is my "Licence Object" which creates an select field with options. These options should be translated. But how to do that?
$form = $this ->createFormBuilder($request)
->add('title', 'text',
array( 'label' => $this->get('translator')->trans('form.title', array(), 'client_request_a_photo'))
)
->add('description', 'textarea',
array( 'label' => $this->get('translator')->trans('form.description', array(), 'client_request_a_photo'))
)
->add('licence','document',
array('class'=>'WunschbildBundle\Document\Licence', 'property'=>'options',
'label' => $this->get('translator')->trans('form.licence', array(), 'client_request_a_photo'))
)
->getForm();
In any case, you don't provide what is the 'document' field type, so we can't help much. However, from what I understand the options are fetched through the attribute 'options' of the 'Licence' object. If you want those to be translated, the object 'Licence' must be translatable and the attribute 'options' must have translations. You can do this by using the doctrine extensions bundle. The documentation about Translatable can be found here. Hope this help.
Related
I'm generating a form and have an entity mapped to a select field, using the entity FormType
I can set the select to display whatever property i like with:
'property' => 'myProperty' (in symfony 2.7 its) 'choice_label' => 'myProperty'
I'm Using Symfony 2.6 so the 'choice_value' option is not avaliable
I don't see a way to specify which property to use as the actual value of the select, it seems to default to the id field, which isn't what i want.
$myData = $this->fpem->getRepository('GameBundle:ContestTimeKey')->getCurrentKeysQuery($currentTimeframe);
$builder
->add('contestTimeKey', 'entity', array(
'class' => 'GameBundle:ContestTimeKey',
'property' => 'dateString',
'query_builder' => $myData,
'label' => 'Select Date',
'placeholder' => 'Pick a Date',
))
->add('userID', 'hidden')
->getForm();
}
The $myData is a query returned from the repository getting the entities i want to populate the select with.
I have a nested form with prototype feature in Symfony 2. Here is the parent form which contains the collection:
$builder
->add('rooms', 'collection', array(
'type' => new RoomForm(),
'allow_add' => true,
'allow_delete' => true,
'data' => array(new RoomForm()),
))
As you can see, no data_class is defined. After the form submission $form->getData() correctly return associative array.
RoomForm is a simple form class composed by two fields:
$builder
->add(
$builder->create('dateAvailabilityStart', 'text', array(
'label' => 'label.from'
)))
->add(
$builder->create('dateAvailabilityEnd', 'text', array(
'label' => 'label.until'
)))
I would like find a way to populate my collection with existing RoomForm (for edit mode) and associate data in correct fields.
Any ideas?
You could do it from within your controller. Given that above form type is named as RoomFormCollection you could do something like this:
// This should be an array
$rooms = ... // Either from database or...
$form = $this->createForm(new RoomFormCollection(), array(
'rooms' => rooms
));
Another thing, 'data' => array(new RoomForm()), is not valid. RoomForm as its name suggests is a form type, not data struct. You should remove it...
Hope this helps...
I'm trying to add an entity field in a symfony2 form but it's always giving me the same error: '500 (Internal Server Error) '.
This is the class i'm using to create the form. It was automatically programmed with doctrine and CRUD.
class ClientType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('companyName', 'text' , array( 'attr' => array( 'class' => 'companyname' ) ) )
->add('contactUserName','text' , array( 'attr' => array( 'class' => 'contactusername' ) ))
->add('phone','text' , array( 'attr' => array( 'class' => 'phone' ) ))
->add('subdomain','text' , array( 'attr' => array( 'class' => 'subdomain' ) ))
->add('email','text' , array( 'attr' => array( 'class' => 'email' ) ))
->add('website','text' , array( 'attr' => array( 'class' => 'website' ) ))
;
}
This works fine, but then, i try something like this:
->add('client', 'entity', array(
'class' => 'BackendBundle:Client'));
'500 (Internal Server Error)'
I tried so many different ways to do this, but it's always the same error.
The thing is, i can add or remove the fields that were created at the beggining when this class was done by the doctrine CRUD but if i try adding more fields with different types, it won't let me.
Should i make my own Type class so i can customize my forms or is there a way to modify the form doctrine made?
TY
The thing is, i can add or remove the fields that were created at the
beggining when this class was done by the doctrine CRUD but if i try
adding more fields with different types, it won't let me.
This is because the command that created your ClientType.php did so, based upon the structure of your BackendBundle\Entity\Client.php file. The form is mapped to the entity that you intend to create. If you want more fields on the form, you will need to add the fields as properties in your BackendBundle\Entity\Client.php, then run:
php bin/console doctrine:generate:entities <VENDOR>/<BUNDLE>/Entity/Client
or if using Symfony 2 < version 2.5
php app/console doctrine:generate:entities <VENDOR>/<BUNDLE>/Entity/Client
To generate the getters and setters for that field, and then
php bin/console doctrine:schema:update --force
or if using Symfony 2 < version 2.5
php app/console doctrine:schema:update --force
To add the new field(s) to the database table.
Now you can try to add the field as you were, ensuring that the first argument in the add() method exactly matches how you names your property in the entity.
i need to create a form with symfony that has an entity type, so this is im using
->add('assignee', 'entity', array(
'label' => 'Assignee',
'class' => 'PortalBundle:TrnUser',
'property' => 'username',
))
in the generated html it assigns userid as the option value, but i need the username as the option value. something like,
<option value="admin">admin</option>
how can i do this? please help.
thanks..
You need data transformers. They help you to show data in form as you want.
There you can find all information about Data Transformers in Symfony2:
http://symfony.com/doc/current/cookbook/form/data_transformers.html
You could use the 'choice_value' option with the name of the field you want to use instead of the id.
$builder
->add('user', 'entity', [
'class' => 'YourBundle\Entity\Locations',
'property' => 'name',
'choice_value' => 'name',
'required' => true,
])
How do i change the default name attribute for an input in my form that is created using the form factory?
here is an example of the simple form i am using:
$form = $app['form.factory']->createBuilder('form')
->add('image','file)
->add('longitude', 'hidden')
->add('latitude', 'hidden')
->getForm();
i have tried putting the attributes into an array without successfully changing the name, although with this method i could change the label or class etc:
->add('latitude', 'text', array('attr'=>array("name"=>'newname')))
it seems like a very simple request to be able to change the name of an input, so you would have thought there would be an obvious way to do it. With the code above it would still show the name as name=form[latitude]
Use createNamedBuilder instead createBuilder to overwrite the fields name. The name will be the first argument in add function.
$personal_form = $app['form.factory']->createNamedBuilder(null, 'form')
->add('name', 'text', array(
'label' => 'Nombre',
'data' => 'Nombre'
))
->add('surname', 'text', array(
'label' => 'Apellidos',
'data' => 'Apellidos'
))
->add('email', 'email', array(
'label' => 'E-mail',
'data' => 'E-mail'
))
->getForm();