symfony2 form entity option value - forms

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,
])

Related

Symfony2 FormType Entity set value to specific property

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.

How to change default name in form Symfony2

I have this builder at FormType class:
$builder
->add('fkTblSources', 'entity', array(
'class' => 'BlaBlaBundle:TblSources',
'property' => 'name',
))
->add('save', 'submit');`
This form shows "fkTblSources" as name of row. I need to put this name because it is the field name of the entity "TblTicket" that i want to create with this form.
How can I change this name?
Ok, it's easy, sorry.
Name of field is changed by 'label' option.
->add('fkTblSources', 'entity', array(
'class' => 'BlaBlaBundle:TblSources',
'property' => 'name',
'label' => 'name for field here'
))

Symfony - Combine two properties in a single entity form field

I have an entity for publications with many different fields as booktitle, conference etc. I want to build a search form and one of the feature requests is to combine two search parameters in a single choice field. So far I have something like this in the form builder:
$builder->add('booktitle', 'entity', array(
'required' => false,
'label' => 'Conference/Booktitle',
'property' => 'booktitle',
'class' => 'indPubBundle:Publication',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('p')
->groupBy('p.booktitle')
->orderBy('p.booktitle', 'ASC');
}
));
Basically I am displaying all booktitles as a choice field. What I want now is to have the conferences as well in the same choice field. Is there a way to achieve that?
The Entity field type is a child of the Choice field type. So you can provide Data via the "choices" parameter. Combine that with a (e.g. repository) method that returns an array with the data you need might be a solution that works for you.
$builder->add('booktitle', 'entity', array(
'required' => false,
'label' => 'Conference/Booktitle',
'class' => 'indPubBundle:Publication',
'choices' => $this->getDoctrine()->getRepository('indPubBundle:Publication')->getData(),
));

how to change silex form factory name attribute

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();

Symfony2 - Translate entity field type options

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.