symfony 2, form_rest(form) shows collection fields - forms

My form has 3 collection fields:
$builder->add('affiliates', 'collection', array(
'type' => new AffiliateForm(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'options' => array(
'affiliate_types' => $options['affiliate_types'],
'business_types' => $options['business_types'],
),
));
$builder->add('other_businesses', 'collection', array(
'type' => new OtherBusinessForm(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
$builder->add('welfare_activities', 'collection', array(
'type' => new WelfareActivityForm(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'options' => array(
'welfare_activity_types' => $options['welfare_activity_types'],
),
));
In the template I show each subform field separately one by one, like below:
<td class="t1c5" >{{ form_widget(affiliate.location) }}
{{ form_errors(affiliate.location) }}</td>
At the end of the form I did:
{{ form_rest(form) }}
But it causes to display the following words at the end of form, when given collection is empty: "Affiliates", "Other businesses", "Welfare activities".
So the question is:
Why those words are displayed on the form?
I can do the following to avoid the above issue:
<div style="display:none;">{{ form_rest(form) }}</div>
Is it correct way of dealing with the problem (maybe i can make a field hidden or whatever)?
Thank you.

those words are displayed on the form because you forget :
{{ form_label(affiliate.location) }}
…
…
According to the doc :
form_rest(view, variables)
This renders all fields that have not yet been rendered for the given form. It's a good idea to always have this somewhere inside your form as it'll render hidden fields for you and make any fields you forgot to render more obvious (since it'll render the field for you).

{{ form_end(form, {'render_rest': false}) }}

Related

Recaptcha google with EWZRecaptchaBundle for Symfony

I want to have a recaptcha system to my contact form symfony. For that I use a EWZRecaptchaBundle. But I try lot of stuff tricks for running recaptcha but my form submit run without testing validation recaptcha widget (invisible and visible)
Can you help me to run recaptcha correctly. My submit form run without deal with recaptcha but I have the widget display correctly.
config.yml
ewz_recaptcha:
public_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
locale_key: %kernel.default_locale%
locale_from_request: false
enabled: true
verify_host: true
ajax: false
ContactType
->add('recaptcha', EWZRecaptchaType::class, array(
'attr' => array(
'options' => array(
'theme' => 'light',
'type' => 'image',
'size' => 'invisible',
'defer' => true,
'async' => true,
'callback' => 'onReCaptchaSuccess',
'bind' => 'contact_submit',
)
),
'mapped' => false,
'constraints' => array(
new RecaptchaTrue()
)
)
)
// ->add('recaptcha', EWZRecaptchaType::class)
->add('submit', SubmitType::class, [
'label' => 'form.submit.send',
'attr' => ['class' => 'btn1 form_recaptcha_submit', 'id' => 'contact_submit']
])
Twig template
{{ form_widget(form.recaptcha, { 'attr': {
'options' : {
'theme': 'light',
'type': 'image',
'size': 'invisible'
},
} }) }}
contact (entity)
I have my field recaptcha
private $recaptcha;
I have the widget recaptcha google which display correctly but my submit form don't work with this. never.
I have registered my domain on google website recaptcha
You need to define the validation in your form (it is also possible to validate in the entity class). Also in my case I did not create a field in my eneity. I prefered to use what we call a "non mapped" field. To use a non mapped field you just need to use 'mapped' => false, in your contactType
I show you below the code I used:
My ContactType:
->add('recaptcha', EWZRecaptchaType::class, array(
'attr' => array(
'options' => array(
'theme' => 'light',
'type' => 'image',
'size' => 'normal',
'defer' => true,
'async' => true,
)
)
,
'mapped' => false,
'constraints' => array(
new RecaptchaTrue()
)
))
I give you my config.yml so you can see I don't need that much options to be defined to make recaptcha works:
ewz_recaptcha:
public_key: xxxx
private_key: xxxx
# Not needed as "%kernel.default_locale%" is the default value for the locale key
locale_key: %kernel.default_locale%
locale_from_request: true
edit: to display the recaptcha widget with twig in my template, I just use {{ form_end(form) }} and the recaptcha widget is on top of the submit button.
edit 2: You don't need to do any validation in your controller but this one:
if ($contactForm->isSubmitted() && $contactForm->isValid())
{
//your logic
}

Symfony2 - NotBlank constraint not working on EntityType

I have two EntityType fields in my form and both has NotBlank constraint assigned to them.
Now, I have this issue that NotBlank constraint is not working only on one field with multiple => true set on.
$builder
->add('preferredCountries', EntityType::class, array(
'required' => false,
'class' => 'IndexBundle:Country',
'property' => 'name',
'empty_value' => 'Choose',
'multiple' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->where('c.name != :name')
->orderBy('c.name', 'ASC')
->setParameter('name', 'Other');
},
'constraints' => array(
new NotBlank(array(
'message' => 'blank!!!',
)),
)
))
->add('internshipProgram', EntityType::class, array(
'required' => false,
'class' => 'IndexBundle:InternshipProgram',
'property' => 'name',
'empty_value' => 'Choose',
'constraints' => array(
new NotBlank(array(
'message' => 'blank!!!',
)),
)
))
In this case when I submit empty values, field internshipProgram get an error, and prefferedCountries not.
Form display:
<div class="form-group col-xs-12">
{{ form_label(current_internship_form.preferredCountries, 'Preferred countries', { 'label_attr': {'class': 'label-text'} }) }}
{{ form_widget(current_internship_form.preferredCountries) }}
<span class="error text-danger small">{{ form_errors(current_internship_form.preferredCountries) }}</span>
</div>
<div class="form-group col-xs-12">
{{ form_label(current_internship_form.internshipProgram, 'What type of training agreement will you have?', { 'label_attr': {'class': 'label-text'} }) }}
{{ form_widget(current_internship_form.internshipProgram, { 'id': 'internship_program', 'attr': {'class': 'form-control '}}) }}
<span class="error text-danger small">{{ form_errors(current_internship_form.internshipProgram) }}</span>
</div>
Is there a mistake in my code or is it somehow related to multiple choice selection? Has anyone had similar issue and know how to solve it?
You can't use NotBlank constraint on EntityType with multiple set to true. As the array will never be null. You should try using the count constraint like this:
$builder
->add('preferredCountries', EntityType::class, array(
'required' => false,
'class' => 'IndexBundle:Country',
'property' => 'name',
'empty_value' => 'Choose',
'multiple' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->where('c.name != :name')
->orderBy('c.name', 'ASC')
->setParameter('name', 'Other');
},
'constraints' => array(
new Count(array(
'min' => 1,
'minMessage' => "Should not be blank"
))
)
))
...
Also you can specify at Entity level,
/**
* #Count(min = 1, minMessage = "At least one branch must be selected")
*/
protected $multiCheckBox;

symfony2.5 maxlength deprecated

I would like to know why was max_length in form type deprecated?
And how to achieve the desired effect the cleanest way now ?
See related issue on Github. This option only add html attribute to textarea. You can manually add it via attributes:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => 255),
));
You can add it in builder:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => 255),
));
or in twig:
{{ form_widget(form.field, {'attr': {'maxlength': 500}}) }}
IMPORTANT! Attribute value must be int, string will dont work:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => '255'),
));

Symfony, Hidden Form Field

i have problem with hiding my form fields. For example:
->add('new_password', 'repeated', array(
'first_options' => array(
'label' => 'Nowe hasło',
'attr' => array('style'=>'display:none;')),
'second_options' => array(
'label' => 'Powtórz nowe hasło',
'attr' => array('style'=>'display:none;')),
'mapped' => false,
'required' => false,
));
Field is not visible but label is visible. I want to have hidden field but label should be hidden to. I want to show it in Jquery after clicking on the button. Any ideas guys ?
First, this is not a Hidden Field Type but a repeated type you want to hide by passing a style='display:none;' attribute.
In general, if you don't want to display a given label, you may need to customize your form rendering.
For example,
{{ form_row(yourForm.new_password) }} {# in case you're using the form_row helper #}
should be replaced by
{{form_widget(yourForm.new_password) }}
Because form_row(yourForm.yourField) is in fact a shortcut for,
{{ form_errors(yourForm.yourField) }}
{{ form_label(yourForm.yourField) }}
{{ form_widget(yourForm.yourField) }}
Also,
Why do you need to hide a repeated password field this way?
If you want to show it only when you click on a button why dont you wrap the newPassword widget in a div with display none?
But if you want to add attributes to the label you can use the option label_attr like this:
{{ form_row(form.name, {'label_attr ':{'class':'hidden'}}) }}
or
->add('new_password', 'repeated', array(
'first_options' => array(
'label' => 'Nowe hasło',
'label_attr' => array('style'=>'display:none;')),
'second_options' => array(
'label' => 'Powtórz nowe hasło',
'label_attr' => array('style'=>'display:none;')),
'mapped' => false,
'required' => false,
));

Symfony2 form collection with checkboxes

I'm building a form with Symfony2 and need to group some checkboxes. I simply cannot figure out how to pass choices/label along to the checkboxes in BonusGroup.
Form:
$builder->add('groups', 'collection', array(
'type' => new BonusGroup(),
'allow_add' => false,
'allow_delete' => false,
'by_reference' => false
));
BonusGroup():
$builder->add('bonus', 'choice', array(
'choices' => $options['bonus'],
'multiple' => true,
'expanded' => true
));
View.twig:
{% for group in form.groups %}
{{ form_label(group) }}
{% for final in group.bonus %}
{{ form_widget(final) }}
{% endfor %}
{% endfor %}
Passing data to form:
$data = array(
'groups' =>
array ('Group 1 label' => array())
);
$form = $app['form.factory']->createBuilder(new Form(), $data))->getForm();
Any tips?
Thanks!
First, change form_widget to form_row, but it won't work yet because collection type need a piece of JavaScript to work.
See examples here:
http://symfony.com/doc/2.1/reference/forms/types/collection.html