Display data attributes in collection field Symfony 2 - symfony

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

Related

Allow spaces when using Select2 tags symfony

I try to create tags if not exist by using select2 entity. When I try to add a tag that contains spaces I can't.
For example :
"Yupi yola" --> not work because tag contains spaces, is getting only first word "Yupi".
"Test" ---> work
"Test_test" ---> work
I added the Select2Entity in my form type
->add('groupe', Select2EntityType::class, [
'remote_route' => 'route',
'class' => Class::class,
'primary_key' => 'id',
'text_property' => 'name',
'minimum_input_length' => 2,
'page_limit' => 10,
'allow_clear' => true,
'delay' => 250,
'cache' => true,
'cache_timeout' => 60000, // if 'cache' is true
'language' => 'en',
'placeholder' => 'Select a group',
'allow_add' => array(
'enabled' => true,
'new_tag_text' => '',
'tokenSeparators'=> '[",", " "]'
),
'multiple' => false,
'attr' => ['style' => 'width:100%'],
'scroll' => true,
])
Please help me. Thx in advance.
Instead of 'tokenSeparators', use 'tag_separators'
'allow_add' => array(
'enabled' => true,
'new_tag_text' => '',
'new_tag_prefix' => '__',
'tag_separators'=> '[",", " "]'
)

BootstrapCollectionType and prototype Symfony 3.2

I got the error :
Neither the property "amount" nor one of the methods "amount()", "getamount()"/"isamount()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".
when I try to get the form field using "prototype" in a Form template.
Form Template:
{% block proposal_service_widget %}
{% set prototype_html = '<td>' ~ form_widget(prototype.amount) ~
'</td>' %}
The Entities are Proposal, Service and ProposalService
For ProposalType I have an option "proposal_service" as true, and the form template is identifying this option.
->add('services', BootstrapCollectionType::class, [
'label' => 'Services:',
'type' => ProposalServiceType::class,
'proposal_service' => true,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'sub_widget_col' => 10,
'button_col' => 2,
'add_button_text' => 'Add Service',
]);
My question is Why the Form Template is not identifying the "prototype.amount" ????
if I have this field on my ProposalServiceType ??
$builder
->add('amount', null, [
'label' => 'Qty:',
'attr' => ['class' => 'amount', 'min' => 1]
])

Symfony 3 weird "choice" error

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

Translations are not saved correctly using A2lix Translation + KnpLabs DoctrineBehaviors

In my Symfony project I use SonataAdminBundle and A2lixTranslationForm + KnpLabsDoctrineBehaviors bundles for multi-language support.
I have Serial and Episode entities which use standard OneToMany and ManyToOne relationship. I display episodes using sonata_type_collection:
->add('episodes', 'sonata_type_collection', [
'by_reference' => false,
],
[
'edit' => 'inline',
'inline' => 'table',
]
)
By default A2lixTranslation bundle displays translatable fields under each other. However, I need to display each translation field of the episode in its own column. In order to achieve this, I created three virtual fields with the same property_path:
->add('episode_title', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'title' => [
'attr' => [
'pattern' => false,
],
'label' => false,
],
],
'exclude_fields' => ['subTitle', 'description'],
'label' => 'Title',
])
->add('episode_subtitle', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'subTitle' => [
'label' => false,
],
],
'exclude_fields' => ['title', 'description'],
'label' => 'Subtitle',
'required' => false,
])
->add('episode_description', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'description' => [
'field_type' => 'textarea',
'label' => false,
],
],
'exclude_fields' => ['title', 'subTitle'],
'label' => 'Description',
'required' => false,
])
As you can see, I display only one field in each a2lix_translations form.
But I faced the following problem: only the last field value (description in this case) is saved when I add new episode using Add new button of the collection form and then clicking Update button of the parent (Serial) form. Wherein already existing episodes are saved correctly if edited.
Could you please point me why is this happening and how to fix the issue. I suppose each translations form values rewrite the previous ones, but why do this work normally when saving existing records?
Or maybe there is another way to display each field in the own column in sonata collection form, this also will be helpful.
Thank you.

Symfony2 collection and related choice field not (yet) managed

I've got a form with a collection field "options" and a choice field "defaultOption". My goal is to enable the user to create a new entity, add some options and select a default option from the list of option he/she just added before saving the entire thing.
Here's part of the form that shows both fields:
$form->add('options', 'collection', array(
'type' => new CustomFieldOptionType(),
'label' => 'custom.field.options',
'allow_add' => true,
'allow_delete' => true,
'options' => array(
'label_render' => false,
'widget_control_group' => false,
),
'by_reference' => false,
'attr' => array('class' => 'options')
));
$form->add('defaultOption', 'entity', array(
'label' => 'custom.field.default',
'class' => 'XFDOBundle:CustomFieldOption',
'choices' => $field->getOptions(),
'property' => 'name',
'required' => false,
'empty_value' => 'custom.field.default.empty',
'attr' => array('class' => 'default-option')
));
I've tried several things with jquery and form event listeners to (re)populate the "defaultOption" field with the new (not-yet-persisted) options, but that resulted in to nothing or "Entities passed to the choice field must be managed" exceptions.
Any idea how I can get this working?

Resources