Symfony 3 weird "choice" error - symfony

I'm trying to build a checkbox list in Symfony 3.
This code:
$choices = [
'a' => 'fsssssss',
];
$builder->add('memberships', ChoiceType::class, [
'choices' => $choices,
'expanded' => true,
'multiple' => false
]);
Returns the following error: Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php (line 73)
If I add two more options, the error goes away and the field is shown correctly:
$choices = [
'a' => 'fsssssss',
'd' => 'fsssssss',
'g' => 'fsssssss',
];
$builder->add('memberships', ChoiceType::class, [
'choices' => $choices,
'expanded' => true,
'multiple' => false
]);
Actually, there are many combinations that work and many that don't; I failed to see any pattern.
Am I doing something wrong? Is there some sort of cache I need to clear?

Just to close this question:
If you use a form with the data_class option and a form field represents a property of an entity which should get selected by choices you can use EntityType:
$builder->add('memberships', EntityType::class, [
'class' => MyClass::class,
'choices' => $choices,
'expanded' => true,
'multiple' => false
]);

Related

Form entity type options pre-checked

I have a form field which displays a bunch of checkboxes, the field is an EntityType. When the form renders on the front-end, a bunch of the checkboxes, not all, are already checked.
I'd like all options to be unchecked when the form renders but I can't figure out why specific ones are already checked. Below is what I currently have:
->add('example', EntityType::class, [
'class' => Example::class,
'multiple' => true,
'required' => false,
'by_reference' => false,
'expanded' => true,
'query_builder' => function (EntityRepository $repository) use ($client) {
return $repository
->createQueryBuilder('ex')
->join('ex.quote', 'e', Expr\Join::WITH, 'e.client = :client')
->setParameter('client', $client->getId());
},
]);
I have tried adding things like:
'choice_attr' => [
'checked' => false,
]
Any help would be great.

How to validate EntityType in Symfony

I made form using form builder
$builder
->add('name', null, [
'attr' => ['autofocus' => true],
'label' => 'label.name',
])
->add('city', EntityType::class, [
// looks for choices from this entity
'class' => City::class,
// uses the City.name property as the visible option string
'choice_label' => 'name',
'placeholder' => 'choose.city'
])
->add('university', EntityType::class, [
// looks for choices from this entity
'class' => University::class,
// uses the University.name property as the visible option string
'choice_label' => 'name',
])
->add('courses', EntityType::class, [
// looks for choices from this entity
'class' => Course::class,
// uses the University.name property as the visible option string
'choice_label' => 'name',
'multiple' => true,
])
;
Now my form looks like this
I want for dropdown choices to be integers and not blank.
For single dropdowns all I need to add is: #Assert\NotBlank(message="student.university_not_blank")
How can I set that those values on single dropdown should be integers?
How can I validate multiple choice dropdown not to be blank and its values to be integers?

Error for Constraint added to Field does not show for field

I dynamically generate a form and add constraints (i.e. Choice).
$builder->add('test', 'choice', [
'choices' => [1, 'one', 2 => 'two'],
'required' => true,
'expanded' => true,
'error_bubbling' => true,
'cascade_validation' => true,
'label' => 'this_is_a_test',
'multiple' => false,
'constraints' => [
new NotBlank([
'groups' => ['Default']
]),
new Choice([
'min' => 1,
'choices' => [1, 2],
'groups' => ['Default']
])
]
]);
When submitting the form with empty data, the error shows up for the form, not the element of the form where i added the constraint (checked in profiler as well).
There is not Option atPath for those constraints and i add them directly to the field, so i do not get why they show up for the form.
That's what the error_bubbling option does (which you set to true in your form type):
If true, any errors for this field will be passed to the parent field or form. For example, if set to true on a normal field, any errors for that field will be attached to the main form, not to the specific field.

Symfony2 - Drop down with the possibility of adding fields

Sorry for my english.
I need to create a drop down list (select option) with values ​​taken from an Entity.
I must also add options (I use select2 to do this).
When I send the form it doesn't work. How can I configure the field type to make it work?
I need the possibilities to adding multiple persons
My current code is
...
->add('person', 'entity', array(
'attr' => array(
'class' => 'tags'
),
'class' => 'AppBundle:Person',
'data_class' => null,
'label' => 'Persons',
'mapped' => false,
'multiple' => true,
'required' => false
));
Select2
$('select.tags').select2({
language: 'it',
tags: true,
tokenSeparators: [','],
width: '100%'
});
Error message
The offset "0" does not exist.

Display data attributes in collection field Symfony 2

I have 2 fields in my form. First Name and Email.
$builder->add('firstName', 'text', [
'label' => 'First Name',
'required' => true,
'attr' => [
'data-msg-required' => 'First name is required'
],
'trim' => true])
->add('emails', 'collection', [
'type' => new RegisterEmail,
'required' => true,
'by_reference' => false,
'label' => false,
'options' => [
'attr' => [
'data-msg-required' => 'Email is required'
]
],
]);
However data-msg required is only displayed for first field but not for second one. I know I can add that directly in twig template but is there anyway I can achieve that through form class only.
You should add it to the default value of the RegisterEmail type, with setDefaults()

Resources