symfony2.5 maxlength deprecated - forms

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'),
));

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
}

forms - Symfony 2 - default attributes for labels and fields in all forms

I am using Symfony2 and Twitter Bootstrap along with some customized styles. I would like to have default classes added to 'text' and 'textarea' fields for the whole project unless specified not to use them.
This is the manual way of achieving what I want in one form Type, however it is not efficient at all.
$builder
->add('name', 'text', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'col-md-4 control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('description', 'textarea', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('referenceNo', 'text', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'col-md-4 control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('client','text',array()) //Don't use styling
Should I create a service and call it inside every form Type to get default settings and then pass it as argument?
Should I extend form_div_layout.html.twig and modify it (form customization)? Or is there a better and more efficient way of achieving this task?
First idea: use traits (http://php.net/traits)
Second idea: extend a base form. Call the parent function to get default values.
Both ways are better than creating a service and calling it every time. I prefer using traits to achieve this.

How do I allow html tags in label for Zend form element using addElement()?

I am brand new to Zend and I've been given a project to make adjustments on. I'd like to add html to the labels for my form elements but I can't seem to get it right.
Here's what I have:
$this->addElement('text', 'school_name', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 150)),
),
'required' => true,
'label' => 'Name* :<img src="picture.png">,
'size' => '90',
));
As is, of course, the <img src="picture.png"> text gets escaped and the whole string is displayed.
I've read that I need to use 'escape' => false in some capacity but I can't figure out where/how to use it in my specific case.
Any help would be great. Thanks!
After calling addElement fetch the label's decorator and change the escape setting:
$form->getElement('school_name')->getDecorator('label')->setOption('escape', false);
If you use this type of label a lot, you should consider writing a custom decorator.
You can also use the disable_html_escape in 'label_options' when adding an element to the form:
$this->add(array(
....
'options' => array(
'label' => '<span class="required">Name</span>,
'label_options' => array(
'disable_html_escape' => true,
)
),
...
));
Credit to Théo Bouveret's post 'Button content in ZF2 forms' for the answer.

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

Zend Framework Form Submit Button Style

Is it possible to change the form's Submit button from the defaut - like the Gmail button say, which is blue?
Currently I have this in my form:
$this->addElement('submit', 'form_element_signup', array('label' => 'Sign up',
'tabindex' => 20,
'decorators' => array('ViewHelper',
array(array('div' => 'HtmlTag'),
array ('tag' => 'button',
'class' => 'action blue')))));
But this still gives me the default submit button within my div button.
you can add your custom class with your own css property like below
$this->addElement(new Zend_Form_Element_Button( 'send',
array( 'label' => 'registrieren',
'class' => 'button-red',
'type' => 'submit',
'escape' => false,
'required' => false,
'ignore' => false, ) ));
Yes just apply the appropriate CSS to the 'div', 'tag' or 'class' as required.
22 CSS Button Styling Tutorials and Techniques may help.