create a form for several entities symfony2.3 (best practice) - forms

I want to create a form from several entities (article, category, adress, attributes, gallery) with custom fields (remove fields, customize css, ...),
what is the best practice with Symfony2 and forms
thank you.

Typically you want to you an embedded collection of forms to do this. An example shown bellow:
$builder->add('subject','text', array(
'required' => false,
));
$builder->add('body','textarea', array(
'required' => false,
));
$builder->add('files','collection', array(
'type' => new DocumentForm(),
'allow_add' => true,
'allow_delete' => true,
'label' => false,
));
This form binds to my message entity, however, you still have a collection type which relates to a different form that is attached to my file entity.
You want to embed entities and forms. You can find more information on this here:
http://symfony.com/doc/current/cookbook/form/form_collections.html

Related

How can I Validate All Children of a Form Collection?

I have spent a long time without making progress on what seems to be a simple problem. I need to allow the user to add up to three references to a form. Each reference will have a name and phone number (two text inputs). I would like to do some simple validation on each one. I have created my ReferenceType and nested it into the main form with the following (note, I created the PhoneNumber validator and added the Null() just for testing):
$builder->add('references', 'collection', array(
'type' => new ReferenceType(),
'by_reference' => false,
'allow_add' => true,
'label' => false,
'required' => false,
'attr' => array('class' => 'reference'),
'options' => array(
'required' => false,
'label' => false,
),
'cascade_validation' => true,
'constraints' => array(
new Null(),
new PhoneNumber(),
)
));
ReferenceType:
$builder->add('name', 'text', array(
'label' => 'Reference',
'required' => false,
'cascade_validation' => true,
'attr' => array(
'placeholder' => 'Name'
)
));
$builder->add('phone', 'text', array(
'label' => false,
'required' => false,
'attr' => array(
'placeholder' => 'Phone Number'
),
'cascade_validation' => true,
'constraints' => array(
new Length(array('max' => 14)),
new PhoneNumber(),
)
));
All of that is quite simple and straight forward. However, I have been unable to get the form to throw an error for any reason related to those references. I use Propel for my ORM and have a lot of validation which works correctly on the main form. These references also display and function correctly except for validation. They are all mapped to one column (references) and I have tried Propel's type="array" as well as defining my own getter and setter which serializes the array of items. The type=array doesn't work at all with the collection, and serializing it seems to work ok.
I have searched SO and symfony.com docs for an answer but have found nothing that would actually cause either name or phone to throw an error. I have tried adding validation to my validation.yml file without success (3 chars only for testing):
references:
- Valid: ~
- Collection:
fields:
name:
- Length:
max: 3
maxMessage: Please limit your reference's name to 3 characters or less.
I'm probably missing something very obvious here, but I can't seem to grasp what. I have also dug through the Profiler and everything appears correct in there as well.
Could any provide hints on things to look for? It seems that creating a new database just for references is a little overkill (they don't need to be searchable or anything). Propel has some information on collections, but it didn't seem to make a difference. Or am I entirely missing the idea here and perhaps I should implement the fields an entirely different way?
Symfony 2.6
Propel 2

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

Symfony 2.3 add form collections of entities

I'm triying to make a dynamic form adding collections inside an entity.
I have followed the code example in the Symfony's documentation, and it works, but what I want to do is add a new form (the form of the entity collections).
So, if I have an entity A that contains a collection of entities B, I want to add new entities B dynamically in the form, but I don't know how to do it.
The entity A form should be something like:
$builder->add('entityB', 'collection', array(
'type' => 'HOW TO PUT THE FORM OF THE ENTITY B???',
'options' => array(
'required' => false,
),
'allow_add'=>true,
));
Taken from the Cookbook:
$builder->add('entityB', 'collection', array(
'type' => new EntityBType(),
'options' => array(
'required' => false
),
'allow_add' => true
));
This is assuming that you have created a Form Type Class for EntityB (not manually creating it when needed in your controller). The linked cookbook entry gives a lot of good examples based on per-case situations.

Disable some checkboxes in form

I was wondering if there is an easy way of how to disable one checkbox from modifying it by user (Symfony 2.1). I was trying something like this:
$builder->add('adminRoles', 'entity', array(
'property' => 'roleName',
'class' => 'MyBundle:Role',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('r')
->orderBy('r.roleName', 'ASC');
},
'disabled' => $this->disabledRoles,
'expanded' => true,
'multiple' => true
));
By $this->disabledRoles I meant an array of IDs of Role entities or entities themselves, but it seems that it just accepts boolean value which is applied for all entities (checkboxes). Thanks for your advice :-)
You will need to add a form listener in order to customize individual elements.
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html
It might seem like a lot of work but it's easy enough once you work through the examples. You will end up passing disabledRoles to the listener and then setting the disabled flag accordingly.

How to use the ChoiceListInterface in Symfony 2?

I'd like to display a dynamic list of checkboxes in a form.
So far, I built a form embedding a static list of checkboxes, and I created a Tag entity for different values in different languages and populated the database. I'd like to replace the static checkboxes by a dynamic list based on the Tag entity.
The documentation says I should use the ChoiceListInterface. But it is really poorly documented. Would you have an example or a global logic explanation to help me ?
You can extend LazyChoiceList abstract class and implement loadChoiceList() method, create a service of it, inject it to the form and set it as choice_list option.
Finally, I used an entity field type :
->add('tags', 'entity', array(
'class' => 'bndMyBundle:Tag',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('t')
->orderBy('t.en', 'ASC');
},
'expanded' => true,
'multiple' => true,
'property' => 'en',
))
Then, I just need to replace the 'en' value by the user's current locale to choose the right language.