Symfony2 form collection with custom labels - symfony

I have an object - Book with Persons collection inside (each person has name property) and I need to create a form type persons it is a collection simply checkbox but I would like to have as a label on each checkbox persons name how to do it ?
I have BookType:
$builder->add('persons', 'collection', array(
'type' => 'checkbox',
'options' => array(
'required' => false,
),
));
How to add label with persons name ??

I'm not sure if I understood your question, but I'll try to help you anyway.
Did you already tried this?
$builder->add('persons','entity', array(
'class' => 'VendorBundle:Entity',
'property' => 'name',
'label' => 'YourLabel'
))
(Adapt the code for your configurations by changing 'VendorBundle:Entity' and 'YourLabel' )
This adds a field in your form which has the 'property name' of each 'VendorBundle:Person' and has a label 'YourLabel' right before the dropdown field.

Related

Doctrine ChoiceType how to change values based on existing data in entity

I have a form in a Symfony 3 CRM for Horse Rider Camps, which has a Doctrine ChoiceType field, with the option of whether to select if an attendee of the Camp is a Non-Rider or not. However, the Camp has a limit to the amount of Non-Riders allowed in the Camp so I need to be able to disable the 'Yes' option of the Non-Rider drop down and, ideally, replace it with a disabled value of "The limit has been reached for this camp" or something similar.
Here is the entry for the field:
$builder->add('non_rider', ChoiceType::class,
array(
'label' => 'Is Non-Rider',
'required' => true,
'placeholder' => '-- Please select --',
'choices' => array(
'Yes' => 1,
'No' => 0
),
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control required'
)
));
What I'd like is the choices field to read something like:
'choices' => array(
'You have reached the limit' => null,
'No' => 0
)
And somehow disabled the entry. Is this possible?
You can use the event form to do that
Customizing your Form Based on the Underlying Data

How to display dropdown list from related entities in Symfony?

I want to create a drop-down list from two related entities "Category" and "sub-category".
Under symfony I know how to create it from a single entity at a time but I have not managed to do so in a way that in the drop-down list each category displays the subcategories that belongs to it as indicated in the figure bellow.
You have to give arrays to your 'choices' field, each one representing a category.
$builder->add('stockStatus', ChoiceType::class, array(
'choices' => array(
'Main Statuses' => array(
'Yes' => 'stock_yes',
'No' => 'stock_no',
),
'Out of Stock Statuses' => array(
'Backordered' => 'stock_backordered',
'Discontinued' => 'stock_discontinued',
),
),
));
you can find more information here: https://symfony.com/doc/current/reference/forms/types/choice.html#grouping-options

How to populate a select field based on a previous selected value in Doctrine?

I am working on a Symfony2 CRM which implements Doctrine ORM for the forms. One of the forms on the CRM is for creating customer addresses, and has all the standard text fields such as street, city, postcode etc. but also a dropdown for country and county. The problem is, if I retrieve all counties from the database the list is huge and not only takes ages to load but it's very difficult for my client to find the county since it's for every country in the world.
What I'd like is for them to select the country first and then the county field is auto populated based on the selected country. However, I am unsure how to do this in Doctrine.
Here is my buildForm function in the form type (noting that here, Zone means County):
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('company' , 'text', array('required' => false ));
$builder->add('firstname' , 'text', array('required' => true, 'label' => 'First name' ));
$builder->add('lastname' , 'text', array('required' => true, 'label' => 'Last name' ));
$builder->add('address1' , 'text');
$builder->add('address2' , 'text', array('required' => false ));
$builder->add('city' , 'text');
$builder->add('country' , 'entity',
array(
'class' => 'AppBundle:Oc49Country',
'property' => 'name',
'empty_value' => 'Choose an option',
));
$builder->add('zone' , 'entity',
array(
'class' => 'AppBundle:Oc49Zone',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('z')
->orderBy('z.name', 'ASC');
},
'empty_value' => '-- Please select --',
'required' => true,
'label' => 'County/State'
));
$builder->add('postcode' , 'text');
$builder->add('save', 'submit', array(
'attr' => array(
'class' => 'btn btn-primary'
),
));
}
Is there a way of adding an additional parameter to the query builder in order to filter the county list, on the fly?
Since you want to change the County select list after the page is already rendered by symfony, you'll have to use ajax in some way to update just that county select list.
I would use an jquery/ajax call to populate the County select list based on the selected country. From your twig template, add some a jquery function along these lines:
$('#country_select).on('change', function() {
url: '/path/to/controller/to/get/countyList',
method: 'GET'
}).done(function() {
// update options
});
This question will help with details. Obviously my code is rough, but hopefully this helps.

Form collection preset data not based on entity

I have a difficult problem to solve. I need to implement a functionality to create custom forms (that is declare what fields it should have) and then be able to save and retrieve the data. The form generation mechanism is based on EAV model - form template entity has form fields entity (which are form templates attributes). Each form field has type, name, label etc.
Then I dynamically display the form( foreach $fields as $field (...) $formBuilder->add($field->getType() etc.). After all that the data is saved in another entity called FormInstanceData which consist of field name:field value pairs.
The hard part is that I have to be able to create form templates with form field groups which behave like collections (new ones can be added with JS). The form (generated with the template) displays correctly, but I have problem with retrieving the data (as the final data is not an entity for obvious reasons). Simple fields can be successfully filled out with retrieved data (by passing data option with the field name as key), but I can't get the nested collection fields to work - even after passing the data, the collections simply don't display.
The part of the code responsible for that looks like this:
elseif ($fieldType === 'collection'){
$subfields = $field->getSubfields();
$formBuilder->add('subfields', 'collection', array(
'type' => new FormCollectionType($subfields),
'allow_add' => true,
'mapped' => false,
'allow_delete' => true,
'by_reference' => false,
'options' => array('required' => false, 'data' => array(
array('title' => 'lorem', 'subtitle' => 'ipsum'),
array('title' => 'lorem', 'subtitle' => 'ipsum')
The FormCollectionType is also generated dynamically with the $subfields parameter. In this case, each item in collection has two fields - title and subtitle. With the data I passed, two already filled out input groups should appear, but nothing does. You can still add new (empty) groups with JS.
Please advise.
Ok, turned out the data needs to be passed not as:
'options' => array('required' => false, 'data' => array(
array('title' => 'lorem', 'subtitle' => 'ipsum'),
array('title' => 'lorem', 'subtitle' => 'ipsum')
but:
'data' => array(
array('title' => 'lorem', 'subtitle' => 'ipsum'),
array('title' => 'lorem', 'subtitle' => 'ipsum')

Symfony2 Forms - Selected Value in Dropdown using Propel ModelType

I'm trying to set the selected value of a 'model' (ModelType from the Symfony Propel Bridge) form type and I'm failing miserably.
There is Product table with the following structure:
id, name, category_id
Example data would be:
id => 1, name => 'Test', category_id => 3
Another table holds the Category data, so category_id is a foreign key.
id => 1, name => 'Category 1'
id => 2, name => 'Category 2'
id => 3, name => 'Category 3'
The form type I'm working with is:
$builder->add('category_id', 'model', array(
'class' => 'Foo\BarBundle\Model\Category',
'required' => true,
'multiple' => false,
'expanded' => false,
'label' => 'Pick a product category!',
'query' => CategoryQuery::create()->orderByName(),
));
Now when I load up the form, I get a nice select drop-down containing the ordered Categories as you would expect.
What I'd like the drop-down to do is display the correct "selected" Category for a Product that already exists (use case: in a Product Edit form). So, Product ID 1 would have a selected option of "Category 3".
I tried setting a "data" option (which was ignored) so I'm perplexed as to why something so fundamental seems so difficult.
Any help would be greatly appreciated.
UPDATE: Problem solved, see below:
$builder->add('category', 'model', array(
'class' => 'Foo\BarBundle\Model\Category',
'required' => true,
'multiple' => false,
'expanded' => false,
'label' => 'Pick a product category!',
'query' => CategoryQuery::create()->orderByName(),
));
Changed 'category_id' to 'category' and everything was right with the world once more.

Resources