Symfony2 doctrine form - entity type - forms

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.

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.

Provide default data in collection child form

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...

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 - 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.

Symfony2 forms ignores attributes in custom fields when same defined in buidler

When I define attributes in form builder and same attributes as default in custom field the other are ignored. In form builder I have:
$builder
->add('validityOfADecisionOnDisability', new JQDateType(), array(
'attr' => array(
'rel' => 'permanent',
)
))
and custom field
class JQDateType extends AbstractType {
public function getDefaultOptions(array $options)
{
return array(
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'attr' => array(
'class' => 'datepicker'
)
);
}
and it renders html
<input type="text" rel="permanent" required="required"
name="profile[validityOfADecisionOnDisability]"
id="profile_validityOfADecisionOnDisability">
without class. But when I add class to attributes in builder
$builder
->add('validityOfADecisionOnDisability', new JQDateType(), array(
'attr' => array(
'rel' => 'permanent',
'class' => 'datepicker',
)
))
eevrything works as expected. How should I define attributes in JQDateType() ? I tried to use array_merge() and array_merge_recursive() in JQDateType::getDefaultOptions() but it didn't help.
This was a bug that is fixed in Symfony 2.1.
#Koral I guess you can use annotation in your Entity class is better than 'getDefaultOptions' ,you can see here :
http://symfony.com/doc/current/book/validation.html
Yes you can validate all fields of your forms directly in your Entity Class,for example :
/**
* #Assert\Min(limit = "18", message = "You must be 18 or older to enter")
*/
protected $age;
Sounds like a bug. You should open a ticket on https://github.com/symfony/symfony/issues