BootstrapCollectionType and prototype Symfony 3.2 - symfony

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

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'=> '[",", " "]'
)

entitytype multiple default value

i have a entitytype and i want give default value (i have the id of entity type)
->add('esame_' . $i, EntityType::class, array(
'label' => false,
'mapped' => false,
'class' => 'AppBundle:Nome_esame',
'required' => true,
'multiple' => true,
'data'=>array($id_Nome_esame1,$id_Nome_esame2) ,
'choice_label' => 'nome',
// 'disabled' => 'disabled',
'attr' => array(
'placeholder' => 'Esami',
'class' => 'max_width esame_row select_esame',
// 'class'=>'col-md-12 col-md-offset-0 col-xs-9 col-xs-offset-3 ',
)
))
My goal is give 2 or more default value to that entity
i try
'data'=>array($id_Nome_esame1,$id_Nome_esame2)
where $id_Nome_esame1, and $id_Nome_esame2 are the id value of entity type by it don't work
I think your data has to be the same type as the class (AppBundle:Nome_esame)
Try something like this (you will need to bring doctrine or the entity manager into your FormType if you didn't do that yet)
->add('esame_' . $i, EntityType::class, array(
'label' => false,
'mapped' => false,
'class' => 'AppBundle:Nome_esame',
'required' => true,
'multiple' => true,
'data' => array($this->em->getReference("AppBundle:Nome_esame", $id_Nome_esame1), $this->em->getReference("AppBundle:Nome_esame", $id_Nome_esame2)),
'choice_label' => 'nome',
// 'disabled' => 'disabled',
'attr' => array(
'placeholder' => 'Esami',
'class' => 'max_width esame_row select_esame',
// 'class'=>'col-md-12 col-md-offset-0 col-xs-9 col-xs-offset-3 ',
)
))
To get the entity manager, if you use the formType as a service, inject doctrine into it, if not, just past it via __construct.
Hope this helps,
Alexandru Cosoi

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

Do not show collection number

I have a form "Survey" with a collection.
->add('comments', 'collection', array(
'type' => new CommentType() ,
'allow_add' => false,
'allow_delete' => false,
'label' => false,
)
)
My Form "CommentType" have only one field for entering comments:
$builder->add('comment', 'text', array('label' => 'comment', 'translation_domain' => 'messages', 'attr' => array('maxlength' => 255)));
If i render my collection in twig template like this
{{ form_row(form.comments, {'attr': {}}) }}
Symfony/Twig always render a control label "control-label required" with a number for the rendered collection.
As example:
0 -> control-label required
Comment -> Label
[] -> Input field
1 -> control-label required
Comment -> Label
[] -> Input field
How can i disable this control label?
Update
Add options and label attributes for your collection, as follows
->add('comments', 'collection', array(
'type' => new CommentType() ,
'allow_add' => false,
'allow_delete' => false,
'options' => array(
'label' => false,
)
)
I think you disabled the wrong label , leave the label of the collection and disable the one in your ->add('comment','text'.....)

using sonata_type_collection against a custom form type instead of a property/relationship with another entity

is it possible to use sonata_type_collection against a custom form type instead of a property/relationship with another entity?
Something like this:
$formMapper->add('foo', 'sonata_type_collection',
array('type' => new \myVendor\myBundleBundle\Form\Type\BlockType()
));
Which throws me the following error
The current field `contingut` is not linked to an admin. Please create one for the target entity : ``
EDIT:
Something like this did the trick:
$formMapper->add('contingut', 'collection', array(
'type' => new \myVendor\myBundleBundle\Form\Type\Block1Type(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
));
Instead of a custom type in your example, you can also use a native entity type.
$formMapper->add('cars',
'collection',
array(
'type' => 'entity',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
'options' => array(
'class' => 'YourBundle:Car',
'property' => 'name',
'label' => false
)
)
)

Resources