How to change default name in form Symfony2 - forms

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'
))

Related

php symfony2 name of field's value

I've got a problem with the name of displayed field's value. In my FormType i have something like below:
$builder->add(
'level',
'entity',
array(
'class' => 'AppBundle:Level',
'label' => false,
'property' => 'name',
'mapped' => false,
'attr' => array(
'class' => 'none'
)
)
);
And insteed of property name, I would like to display my own result of toString() method. How can I do this?

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.

forms - Symfony 2 - default attributes for labels and fields in all forms

I am using Symfony2 and Twitter Bootstrap along with some customized styles. I would like to have default classes added to 'text' and 'textarea' fields for the whole project unless specified not to use them.
This is the manual way of achieving what I want in one form Type, however it is not efficient at all.
$builder
->add('name', 'text', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'col-md-4 control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('description', 'textarea', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('referenceNo', 'text', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'col-md-4 control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('client','text',array()) //Don't use styling
Should I create a service and call it inside every form Type to get default settings and then pass it as argument?
Should I extend form_div_layout.html.twig and modify it (form customization)? Or is there a better and more efficient way of achieving this task?
First idea: use traits (http://php.net/traits)
Second idea: extend a base form. Call the parent function to get default values.
Both ways are better than creating a service and calling it every time. I prefer using traits to achieve this.

symfony2 form entity option value

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