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,
));
Related
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
}
I have a password repeated field type in Symfony2 form that goes like this:
->add('password', 'repeated', array(
'type' => 'password',
'invalid_message' => 'Les mots de passe doivent correspondre',
'options' => array('required' => true),
'first_options' => array('label' => 'Mot de passe'),
'second_options' => array('label' => 'Mot de passe (validation)'),
'required' => $bRequired,
'trim' => true,
'constraints' => array(
new Assert\Regex(array(
'pattern' => "/^(?=.*[0-9])(?=.*[a-z])[a-zA-Z0-9!?+]{8,15}$/",
'match' => true,
'message' => "msg"
)),
new Constraints\NotBlank(),
)
))
In twig I'm used to show this field like this:
{% for passwordField in form.password %}
{{ form_row(passwordField,{'attr': { 'class': 'form-control'} }) }}
{% endfor %}
I'm wondering if there is a way to display the password field and the validation field separately so I can display them in different places in my form.
As it is explained in doc:
// in your template.html.twig
/.../
{{ form_row(form.password.first,{'attr': { 'class': 'form-control'} }) }}
{{ form_row(form.password.second,{'attr': { 'class': 'form-control'} }) }}
/.../
where:
The names first and second are the default names for the two sub-fields. However, these names can be controlled via the first_name and second_name options. If you've set these options, then use those values instead of first and second when rendering.
i can't figure out how to set a default vaule to a birthday field in symfony.
for text field this works:
{{ form_widget(form.name, {'value' : 'Max' }) }}
is it possible to set a date default value? something like:
{{ form_widget(form.birthday, {'value' : '02.03.1980' } )}}
symfony.com/doc/current/reference/forms/types/birthday.html
thank you!
Thank you for the hint (see data field). This works with choice. I set the default date in the controller and not in the twig template.
$formAntragssteller = $this->createForm(new StandardType(), $antragssteller)
->add('geburtsdatum', 'birthday', array(
'widget' => 'choice',
'attr' => array(
'class' => 'form-control'
),
'label' => 'Geburtsdatum',
'input' => 'string',
'format' => 'dd.MM.yyyy',
'empty_value' => array('year' => 'Jahr', 'month' => 'Monat', 'day' => 'Tag'),
'data' => '1984-04-04'
));
"value" has nothing to do with the form type, but only with the internal widget template handling
you need to match the right widget via the "widget" word in the add method of the form builder
i.e. if you set ['widget' => 'single_text', 'format' => 'dd-MM-yyyy'] value is for sure in the form of '02-03-1980'
birthday field tipe - widget paragraph
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'),
));
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}) }}