-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Description
Build a form from a form type class:
public function buildForm( FormBuilder $builder, array $options )
{
$builder
->add( 'text', 'text', array(
'label' => 'search.form.text.label'
) )
->add( 'date', 'date', array(
'widget' => 'single_text',
'format' => 'dd.MM.yyyy',
'label' => 'search.form.date.label'
) );
}
Add basic validation rules where none of the fields are required:
Me\MyBundle\Form\Model\TextSearch:
properties:
date:
- Type: \DateTime
Render the form:
<form method="post" {{ form_enctype( form ) }}>
{{ form_widget( form ) }}
<input type="submit" value="Search" />
</form>
All the fields get a required
attribute:
<label for="text_search_text" class=" required">Text</label>
<input type="text" id="text_search_text" name="text_search[text]" required="required" value="" />
This is incorrect, since the validation rules state that they are not required.
Under Forms (2.0) – Form Validation, the documentation states:
HTML5 Validation
As of HTML5, many browsers can natively enforce certain validation constraints on the client side. The most common validation is activated by rendering arequired
attribute on fields that are required. […]
Generated forms take full advantage of this new feature by adding sensible HTML attributes that trigger the validation.
This is clearly not the case: all the fields get the required
attribute.
Also, under Built-in Field Types – Field Type Options (i.e. not under Form Validation), one can read:
The
required
option
The most common option is therequired
option, which can be applied to any field. By default, therequired
option is set totrue
, meaning that HTML5-ready browsers will apply client-side validation if the field is left blank.
In other words, Symfony is artificially introducing a discrepancy between the client- and the server-side validation criteria, and this by default.
Moreover, there is a conflict between the two cited paragraphs of the documentation. The first one states that the required
attribute is rendered on fields that are required, which is confirmed by the following paragraph under Field Type Guessing – Field Type Options Guessing:
required
: The required option can be guessed based off of the validation rules (i.e. is the fieldNotBlank
orNotNull
) or the Doctrine metadata (i.e. is the fieldnullable
). This is very useful, as your client-side validation will automatically match your validation rules.
However, the second paragraph states that the required
option is set to true
by default.
Resolution
-
The documentation should be corrected for consistency: under Built-in Field Types – Field Type Options:
By default, the
required
option is set totrue
on the fields that are guessed to be required based on the validation rules, meaning that HTML5-ready browsers will apply client-side validation if the field is left blank. -
Symfony’s behaviour should be changed to match the documentation: the
required
option should be set based on the guessed value and not totrue
by default.