title: Symfony 3: make TextareaType field optional
body: <p>I have a form with one optional field (remark). By default all are mandatory fields. I wanted to make 'remark' field optional. I have tried a lot of solutions. It seems none is working. How can I make it optional?</p>
  
  <p>Here is my code</p>
  
  <p>RequestItem.php</p>
  
  <pre><code>/**
   * RequestItem
   *
   * @ORM\Table(name="request_item")
   * @ORM\Entity(repositoryClass="InventoryBundle\Repository\RequestItemRepository")
   */
  class RequestItem
  {
    /**
       * @var string
       *
       * @ORM\Column(name="remark", type="text", nullable=true)
       */
      private $remark;
  </code></pre>
  
  <p>RequestItemType.php</p>
  
  <pre><code>class RequestItemType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('item', EntityType::class, array(
                'class' =&gt; 'InventoryBundle:Item',
                'choice_label' =&gt; 'name',
                'expanded' =&gt; false,
                'multiple' =&gt; false,
              ),
                array('attr' =&gt;array('class' =&gt;'form-control', 'style' =&gt;'margin-bottom:15px; width:100px')))
              -&gt;add('date', DateType::class, array('attr' =&gt;array('class' =&gt;'form-control', 'style' =&gt;'margin-bottom:15px')))
              -&gt;add('quantity', TextType::class,array('attr' =&gt;array('class' =&gt;'form-control', 'style' =&gt;'margin-bottom:15px; width:75px')))
              -&gt;add('client', TextType::class,array('attr' =&gt;array('class' =&gt;'form-control', 'style' =&gt;'margin-bottom:15px; width:200px')))
              -&gt;add('remark', TextareaType::class, array('attr' =&gt;array('class' =&gt;'form-control', 'style' =&gt;'margin-bottom:15px', 'required' =&gt; false), 'empty_data'  =&gt; null))
          ;
      }
  </code></pre>
  
  <p>I put 'required' => false to remark field.</p>
  
  <p>new.html.twig</p>
  
  <pre><code>{% extends 'base.html.twig' %}
  
  {% form_theme form _self %}
  
  {% use 'form_div_layout.html.twig' with form_label as base_form_label %}
  {% block form_label %}
    {{ block('base_form_label') }}
      {% if required %}
        &lt;span class="required" title="This field is required" style="color:red"&gt;*&lt;/span&gt;
      {% endif %}
  {% endblock %}
  
  {% block body %}
      &lt;h1&gt;Request Item creation&lt;/h1&gt;
      {% if is_granted("ROLE_SUPER_ADMIN") %}
  
      {% if app.session.flashBag.has('warning') %}
          &lt;div class="alert alert-danger" role="alert"&gt;
              {% for msg in app.session.flashBag.get('warning') %}
                  {{ msg }}
              {% endfor %}
          &lt;/div&gt;
      {% endif %}
  
      {{ form_start(form) }}
          {{ form_widget(form) }}
          {{ form_row(form.remark, {'required': false}) }}
          &lt;input type="submit" value="Create" class="btn btn-primary" /&gt;
      {{ form_end(form) }}
  
      &lt;hr /&gt;
      &lt;a href="{{ path('requestitem_index') }}" class="btn btn-success"&gt;Back to the list&lt;/a&gt;
      {% else %}
          &lt;h4&gt;Please &lt;a href="{{ path('fos_user_security_login') }}" class="btn btn-default"&gt;login&lt;/a&gt;&lt;/h4&gt;
      {% endif %}
  {% endblock %}
  </code></pre>
  
  <p>I add {{ form_row(form.remark, {'required': false}) }} in the twig file.</p>
  
  <p>The page source view for the remark field</p>
  
  <pre><code>&lt;div&gt;  &lt;label for="request_item_remark" class="required"&gt;Remark&lt;/label&gt;
            &lt;span class="required" title="This field is required" style="color:red"&gt;*&lt;/span&gt;
      &lt;textarea id="request_item_remark" name="request_item[remark]" required="required" class="form-control" style="margin-bottom:15px" &gt;&lt;/textarea&gt;&lt;/div&gt;
  </code></pre>
  
  <p>The textarea field is required.</p>
  
  <p>I have also tried by adding in new.html.twig file</p>
  
  <pre><code>{% extends 'base.html.twig' %}
  
  {% form_theme form _self %}
  
  {% block textarea_widget %}
    &lt;div class="textarea_widget"&gt;
      {% set required=required|default('not required') %}
      {{ block('form_widget_simple') }}
    &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>It is strange, the textarea field becomes input text field and it is still required.</p>
  
  <pre><code>&lt;div&gt;  &lt;label for="request_item_remark" class="required"&gt;Remark&lt;/label&gt;
            &lt;span class="required" title="This field is required" style="color:red"&gt;*&lt;/span&gt;
        &lt;div class="textarea_widget"&gt;
          &lt;input type="text" id="request_item_remark" name="request_item[remark]" required="required" class="form-control" style="margin-bottom:15px"  /&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>The table structure</p>
  
  <pre><code>CREATE TABLE `request_item` (
    `id` int(11) NOT NULL,
    `item_id` int(11) DEFAULT NULL,
    `date` date NOT NULL,
    `quantity` int(11) NOT NULL,
    `client` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `remark` longtext COLLATE utf8_unicode_ci
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  </code></pre>