disabled attribute not working in symfony on radio button - symfony

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

Related

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

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

Symfony 2: Change inner custom forms fields options

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>

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>

FOSUserBundle : Overriding a Twig template : adding HTML elements

I'm using FOSUserBundle, and I want to add a few HTML elements to the registration form. Actually, I did and I can see the added properties to the User class in my form. The issue is that I want those fields (first name, last name, date of birth, etc..) to get the look and feel of my CSS template (bootstrap).
I succeeded to do that for the login page by overriding it, since the HTML are explicitly declared. I want to do the same for the register page, however it seems confused to me, because here's the content of :
register.html.twig
{% extends "FOSUserBundle::layout.html.twig" %}
{% block fos_user_content %}
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
{% endblock fos_user_content %}
register_content.html.twig
{% trans_default_domain 'FOSUserBundle' %}
{{ form_widget(form) }}
How can I access to the elements that I see in the page from this code ?
Try this
<div class="form-group {% if form.plainPassword.first.vars.errors %}has-error{% endif %}">
<label class="col-lg-2 control-label">Password:</label>
<div class="col-lg-5">
{{ form_widget(form.plainPassword.first, {'attr': {'class': 'form-control input-lg', 'placeholder': 'Enter password', 'required': 'required'}}) }}
{% for errorItem in form.plainPassword.first.vars.errors %}
<label class="control-label has-error" for="{{ form.plainPassword.vars.id }}">{{ errorItem.message }}</label>
{% endfor %}
</div>
<div class="col-lg-5">
{{ form_widget(form.plainPassword.second, {'attr': {'class': 'form-control input-lg', 'placeholder': 'Enter password again', 'required': 'required'}}) }}
</div>
</div>
It works for me.
Please see the official documentation here: "Overriding Forms". You will need to create a custom registration form type class, declare it as a service, and tell FOSUserBundle to use it.
To customize the template, see "Overriding Templates". In your case, you could create app/Resources/FOSUserBundle/views/Registration/register.html.twig.
replace form_widget(form) with something like:
form_widget(form.username)
form_widget(form.email)
form_widget(form.plainPassword)
form_widget(form.myField)
form_rest(form)
in your custom RegistrationFormType class, you could add a class to the username field with:
$builder
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle', 'attr' => array('class'=>'myClass')))
also see form docs on rendering fields by hand: http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template
Here is how I addressed this problem using FOSUserBundle, PUGXMultiUserBundle & BraincraftedBoostrapBundle:
Per FOSUserBundle documentation, add a UserBundle as a child of FOSUser. Create a custom RegistrationFormType that includes the following, where only the attr and label_attr arrays are added:
$builder
->add('email', 'email', array(
'label' => 'form.email',
'translation_domain' => 'FOSUserBundle',
'attr' => array(
'placeholder' => 'E-mail',
),
'label_attr' => array(
'class' => 'sr-only',
)))
->add('username', null, array(
'label' => 'form.username',
'translation_domain' => 'FOSUserBundle',
'attr' => array(
'placeholder' => 'Username',
),
'label_attr' => array(
'class' => 'sr-only',
)))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password',
'attr' => array(
'placeholder' => 'Password',
),
'label_attr' => array(
'class' => 'sr-only',
)),
'second_options' => array('label' => 'form.password_confirmation',
'attr' => array(
'placeholder' => 'Confirm password',
),
'label_attr' => array(
'class' => 'sr-only',
)),
'invalid_message' => 'fos_user.password.mismatch',
))
;
The registration form template then looks like this:
<form action="{{ path('volunteer_registration') }}" method="POST" class="form-inline">
{{ bootstrap_set_style('form-inline') }}
{{ form_row(form.firstName) }}
{{ form_row(form.lastName) }}
{{ form_row(form.username) }}
{{ form_row(form.email) }}
<br/>
{{ form_row(form.plainPassword) }}
{{ form_row(form.plainPassword.second) }}
{{ bootstrap_set_style('') }}

Resources