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'.....)
Related
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]
])
Is it possible to set de default radio choice option to business? I'm using the following symfony code but none of the radio options are selected default in the form.
->add('business', 'choice', array(
'translation_domain' => 'messages',
'choices' => array('business' => true, 'private' => false),
'expanded' => true,
'multiple' => false,
'choices_as_values' => true,
))
You can pass default data to your form:
// Inside a controller
$this->createForm(YourFormType::class, [
'business' => true, // Default value for your business field
]);
You can also set the default value in the form builder:
->add('business', 'choice', array(
'translation_domain' => 'messages',
'choices' => array('business' => true, 'private' => false),
'expanded' => true,
'multiple' => false,
'choices_as_values' => true,
'data' => true, // Default value
))
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()
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?
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
)
)
)