<p dir="auto">Having a layer of validation on the browser is great, but most of the time the guesser gets it all wrong!</p>
<p dir="auto">Take this example: If you create a boolean field on an entity and let the guesser do its work, it sets the field as a required checkbox.<br>
How can one choose the false option then?</p>
<p dir="auto">The same happen with text fields.<br>
If the column is not nullable but I'm not validating it's content, an empty value is just saved as an empty string on the db…<br>
… unless you have HTML5 validation enabled on your browser. Why don't the type guesser don't follow what the rest of the application is doing?</p>
<p dir="auto">Maybe the guesser should set the "required" atribute only when the field is explicitly validated with NotBlank or NotNull.</p>
<p dir="auto">Build a form from a form type class:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="public function buildForm( FormBuilder $builder, array $options )
{
    $builder
        -&gt;add( 'text', 'text', array(
            'label' =&gt; 'search.form.text.label'
        ) )
        -&gt;add( 'date', 'date', array(
            'widget' =&gt; 'single_text',
            'format' =&gt; 'dd.MM.yyyy',
            'label' =&gt; 'search.form.date.label'
        ) );
}"><pre class="notranslate"><code class="notranslate">public function buildForm( FormBuilder $builder, array $options )
{
    $builder
        -&gt;add( 'text', 'text', array(
            'label' =&gt; 'search.form.text.label'
        ) )
        -&gt;add( 'date', 'date', array(
            'widget' =&gt; 'single_text',
            'format' =&gt; 'dd.MM.yyyy',
            'label' =&gt; 'search.form.date.label'
        ) );
}
</code></pre></div>
<p dir="auto">Add basic validation rules where none of the fields are required:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Me\MyBundle\Form\Model\TextSearch:
  properties:
    date:
      - Type: \DateTime"><pre class="notranslate"><code class="notranslate">Me\MyBundle\Form\Model\TextSearch:
  properties:
    date:
      - Type: \DateTime
</code></pre></div>
<p dir="auto">Render the form:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;form method=&quot;post&quot; {{ form_enctype( form ) }}&gt;
    {{ form_widget( form ) }}
    &lt;input type=&quot;submit&quot; value=&quot;Search&quot; /&gt;
&lt;/form&gt;"><pre class="notranslate"><code class="notranslate">&lt;form method="post" {{ form_enctype( form ) }}&gt;
    {{ form_widget( form ) }}
    &lt;input type="submit" value="Search" /&gt;
&lt;/form&gt;
</code></pre></div>
<p dir="auto">All the fields get a <code class="notranslate">required</code> attribute:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;label for=&quot;text_search_text&quot; class=&quot; required&quot;&gt;Text&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;text_search_text&quot; name=&quot;text_search[text]&quot; required=&quot;required&quot; value=&quot;&quot; /&gt;"><pre class="notranslate"><code class="notranslate">&lt;label for="text_search_text" class=" required"&gt;Text&lt;/label&gt;
&lt;input type="text" id="text_search_text" name="text_search[text]" required="required" value="" /&gt;
</code></pre></div>
<p dir="auto">This is incorrect, since the validation rules state that they are not required.</p>
<p dir="auto">Under <em>Forms (2.0) – Form Validation</em>, the documentation states:</p>
<blockquote>
<p dir="auto"><strong>HTML5 Validation</strong><br>
As of HTML5, many browsers can natively enforce certain validation constraints on the client side. The most common validation is activated by rendering a <code class="notranslate">required</code> attribute <strong>on fields that are required</strong>. […]<br>
Generated forms take full advantage of this new feature <strong>by adding sensible HTML attributes</strong> that trigger the validation.</p>
</blockquote>
<p dir="auto">This is clearly not the case: all the fields get the <code class="notranslate">required</code> attribute.</p>
<p dir="auto">Also, under <em>Built-in Field Types – Field Type Options</em> (i.e. not under <em>Form Validation</em>), one can read:</p>
<blockquote>
<p dir="auto"><strong>The <code class="notranslate">required</code> option</strong><br>
The most common option is the <code class="notranslate">required</code> option, which can be applied to any field. <strong>By default, the <code class="notranslate">required</code> option is set to <code class="notranslate">true</code></strong>, meaning that HTML5-ready browsers will apply client-side validation if the field is left blank.</p>
</blockquote>
<p dir="auto">In other words, Symfony is artificially introducing a discrepancy between the client- and the server-side validation criteria, and this by default.</p>
<p dir="auto">Moreover, there is a conflict between the two cited paragraphs of the documentation. The first one states that the <code class="notranslate">required</code> attribute is rendered on fields that are required, which is confirmed by the following paragraph under <em>Field Type Guessing – Field Type Options Guessing</em>:</p>
<blockquote>
<p dir="auto"><code class="notranslate">required</code>: The required option can be guessed based off of the validation rules (i.e. is the field <code class="notranslate">NotBlank</code> or <code class="notranslate">NotNull</code>) or the Doctrine metadata (i.e. is the field <code class="notranslate">nullable</code>). This is very useful, as your client-side validation will automatically match your validation rules.</p>
</blockquote>
<p dir="auto">However, the second paragraph states that the <code class="notranslate">required</code> option is set to <code class="notranslate">true</code> by default.</p>
<p dir="auto"><strong>Resolution</strong></p>
<ol dir="auto">
<li>
<p dir="auto">The documentation should be corrected for consistency: under <em>Built-in Field Types – Field Type Options</em>:</p>
<blockquote>
<p dir="auto">By default, the <code class="notranslate">required</code> option is set to <code class="notranslate">true</code> <strong>on the fields that are guessed to be required based on the validation rules</strong>, meaning that HTML5-ready browsers will apply client-side validation if the field is left blank.</p>
</blockquote>
</li>
<li>
<p dir="auto">Symfony’s behaviour should be changed to match the documentation: the <code class="notranslate">required</code> option should be set based on the guessed value and not to <code class="notranslate">true</code> by default.</p>
</li>
</ol>