Symfony 2: Change inner custom forms fields options - symfony

In my action, I have this:
$formBuilder = $this->createFormBuilder($myEntity);
$formBuilder
// (...)
->add('myInnerEntities', 'collection', array(
'label' => 'My inner entities',
'type' => new InnerEntityType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'options' => [
'InnerEntityTypeField1' => [
'label' => 'Change this image'
],
'InnerEntityTypeField2' => [
'label' => 'Change the caption'
]
]
))
In the 'options' of the collection of InnerEntityType, I'm trying to override the options of the fields of each InnerEntityType in the collection.
The above code doesn't work. How could I achieve that, with of course avoiding to create several InnerEntityType classes with different label (or other options) values ?

<ul class="tags" data-prototype="{% filter escape %}{% include 'yourBundle:formviewfolder:prototype.html.twig' with {'form': form.collectionname.vars.prototype} %}{% endfilter %}">
{{ form_errors(form.collectionname) }}
{{ form_widget(form.collectionname) }}
</ul>
create one prototype.html.twig in main form view folder as following
//prototype.html.twig
<div >
{{ form_row(form.InnerEntityTypeField1,{'label':'your label'}) }}
</div>
<div >
{{ form_row(form.InnerEntityTypeField2,{'label':'your label'}) }}
</div>

Related

symfony form CheckboxType inside CollectionType = prototype empty

I am trying to use a CheckBoxType in a form inside a CollectionType but the prototype only contains the label and no checkbox at all !
This is really weird; I don't understand because the symfony documentation here does not mention anything special to do ?
When I click on the button to add a new file I only get the label
What am I missing please?
THE COLLECTION
->add('file', CollectionType::class, array(
'label' => false,
'entry_type' => FileType::class,
'error_bubbling' => false,
'entry_options' => [ 'required' => false, 'error_bubbling' => true, ],
'allow_add' => true,
'allow_delete' => true
))
THE CHECKBOX INSIDE FILETYPE
->add('main', CheckboxType::class,[
'label' => 'Make this one the main picture',
'required' => false,
]);
THE RESULTING PROTOTYPE
data-prototype="<div id=\"new_item_group_pictures_itemFile___name__\"> <div class=\"form-item\"><div class=\"form-label\"></div><div class=\"custom-select\"><input type=\"file\" id=\"new_item_group_pictures_itemFile___name___file\" name=\"new_item[group_pictures][itemFile][__name__][file]\" class=\"form-file\" /></div></div> <div class=\"form-item\"><div class=\"form-label\"><label for=\"new_item_group_pictures_itemFile___name___description\">Description</label></div><input type=\"text\" id=\"new_item_group_pictures_itemFile___name___description\" name=\"new_item[group_pictures][itemFile][__name__][description]\" maxlength=\"255\" class=\"form-input form-text\" /></div> <div class=\"form-item\"><div class=\"form-label\"><label for=\"new_item_group_pictures_itemFile___name___main\">Make this one the main picture</label></div></div></div>"
ENTITY PROPERTY
/**
* #var boolean
* #ORM\Column(type="boolean", nullable=true)
*/
private $main;
you have to render your embeded form collection iterating over it:
{% for t in form.file %}
{{ form_row(t) }}
{% endfor %}
This will render each checkbox and you can manipulate them in your view
Normally checkboxType should work fine, but if you want to try another solution you can set your second form to ChoiceType and add expanded (to expand choices like checkboxes) and multiple true.
->add('main', ChoiceType::class,[
'label' => 'Make this one the main picture',
'required' => false,
'choices' => array(),
'expanded' => true,
'multiple' => true
]);
The label of the collection type should be true and when you render the form field of the collection type use form_widget instead of form_row

How can I add css classes to ChoseType?

I am trying to add some css classes to the ChoiceType Form in my case a radio box (i set the option 'multiple' to false and the option 'expanded' to true).
i don't know which option I Should use i tryed attr and choice_attr but it didn't work.
My problem is:
How to add a css classe to the radio and the text of the radio?
$builder
->add('enLigne', ChoiceType::class, [
'choices' => $publier,
'attr' => [
'class' => 'radio',
],
'choice_attr' => function($val, $key, $index) {
return ['class' => 'ace input-lg'];
},
'multiple' => false,
'expanded' => true,
'label' => 'Publier *',
'label_attr' => [
"class" => "smaller lighter blue",
"style" => "font-size: 21px;",
]
])
i want to do a radio box like this:
HTML code:
<div class="radio">
<label>
<input name="form-field-radio" type="radio" class="ace input-lg">
<span class="lbl bigger-120"> large radio</span>
</label></div>
Don't add html attributes using php. Instead add those attributes when rendering the form. Example:
{{ form_start(form) }}
{{ form_row(form.enLigne, { 'attr': { 'class': 'smaller lighter blue' } } }}
{{ form_end(form)

Add category of each element in a form

Well, currently I have something like this:
And I would like to add categories appears before filters, like this:
My form code:
$formFilter = $this->createFormBuilder()
->add('_', EntityType::class,array(
'class' => 'loicFilterBundle:Filter',
'multiple' => true,
'expanded' => true,
'choice_label' => function($value) {
return ($value->getName());
},
))
->add('Appliquer filtres', SubmitType::class)
->getForm();
How to add categories before ?
Thanks for help :) .
EDIT:
I finally succeeded by doing it in the twig view(but also need to let group by in the controller):
{{ form_start(form) }}
{% for group in form.filterfilter.vars.choices %}
<div class="col-xs-2">
<h3>{{ group.label }}</h3>
{% for value in group.choices %}
{{ form_widget(form.filterfilter[value.value]) }}
{% endfor %}
</div>
{% endfor %}
{{ form_end(form) }}
What about using the group_by option (see doc) ?
Something like :
$formFilter = $this->createFormBuilder()
->add('_', EntityType::class,array(
'class' => 'loicFilterBundle:Filter',
'multiple' => true,
'expanded' => true,
'choice_label' => function($value) {
return $value->getName();
},
'group_by' => function($value) {
return $value->getCategory()->getName();
},
))
->add('Appliquer filtres', SubmitType::class)
->getForm();

disabled attribute not working in symfony on radio button

Here is my attribute
->add(
'target',
DaimChoiceType::class,
[
'required' => true,
'disabled' => $this->disabledStatus(),
'label' => 'attr.target',
'choices' => [
'common_settings.ui.yes' => 1,
'common_settings.ui.no' => 0,
],
'label_attr' => ['class' => 'radio-inline health-target-radio'],
'expanded' => true,
'multiple' => false,
]
)
Here is my code
{% if viewMode is defined %}
{% set disabled = {'attr':{'disabled': 'disabled'}} %}
{% endif %}
{{ form_row(form.healthInsuranceType.target, disabled) }} // disabled attribute not working here
{{ form_row(form.healthInsuranceType.issueDate, disabled) }} //disabled attribute working here
Please help me to fix the issue in symfony

Symfony Form, CSRF token missing

Like in the topic, i have problem with CSRF token missing. This is my form:
$builder
->add('email', 'email', array(
'label' => 'Adres e-mail'
))
->add('userFirstname', 'text', array(
'label' => 'Imię',
'required' => false
))
->add('userLastname', 'text', array(
'label' => 'Nazwisko',
'required' => false
))
->add('userBusiness', 'entity', array(
'label' => 'Firma',
'required' => false,
'class' => 'Cloud\CrmBundle\Entity\RelationContact',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')->where("u.type = 'b'");
},
'empty_value' => true
))
->add('old_password', 'password', array(
'label' => 'Stare hasło',
'mapped' => false,
'required' => false
))
->add('new_password', 'repeated', array(
'first_options' => array(
'label' => 'Nowe hasło'),
'second_options' => array(
'label' => 'Powtórz nowe hasło'),
'mapped' => false,
'required' => false,
'type' => 'password'
));
My view:
<div class="form-horizontal">
{{ form_row(form.email) }}
{{ form_row(form.userFirstname) }}
{{ form_row(form.userLastname) }}
{{ form_row(form.userBusiness) }}
{{ form_row(form.old_password) }}
{{ form_row(form.new_password) }}
</div>
</div>
What's wrong guys? Any ideas? :( I just don't understand this strange error... What could cause that ?
Probably you've to add this _token by hand because you're trying to display form manually:
{{ form_widget(form._token) }}
Symfony2 set a hidden field with the required informations. For this you have to include the hidden fields with:
{{ form_widget(form._token) }}
if you don't want the CSRF-Protection then you can disable the dunction in your parameters file.
Disable symfony 2 csrf token protection on ajax submit
If you use form_start and form_end symfony will add the token field to the form automatically
<div class="form-horizontal">
{{ form_start(form) }}
{{ form_row(form.email) }}
{{ form_row(form.userFirstname) }}
{{ form_row(form.userLastname) }}
{{ form_row(form.userBusiness) }}
{{ form_row(form.old_password) }}
{{ form_row(form.new_password) }}
{{ form_end(form) }}
</div>

Resources