Symfony2 FormType Entity set value to specific property - forms

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.

Related

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

Symfony2 doctrine form - entity type

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.

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 add where clause in formtype symfony2

its been days now that am looking for the 'how' of this problem. I just learned symfony2 and i cant find a way to this, i've tried different stuff found on the net but i could not get any to work.
Here is my problem, I have a form PictureType in which i include another entity Collection (which is a collection of picture or if you prefer an album of picture).
When the user uploads his picture he needs to select in which album he wants to put it.
My problem is that I cant figure out how to tell in my PictureType to select ONLY the collections of the current user.
here is my pictureType
$builder
->add('file')
->add('collection', 'entity', array(
'class' => 'AppPictureBundle:Collection',
'property' => 'name'
));
I want to insert after property something like this
Where 'user_id' = $this->getUser()
I have a manyToOne on Collection target User and a ManyToOne on picture target collection.
There the query_builder option for that:
$builder
->add('file')
->add('collection', 'entity', array(
'class' => 'AppPictureBundle:Collection',
'property' => 'name',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->where('c.user = :user')
->setParameter('user', $this->getCurrentUser());
},
));

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