title: Symfony 4 handling null DateType form field
body: <p>I have a form in Symfony 4 where I implement the DateType as a text field</p>
  
  <pre><code>-&gt;add('DateOfBirth', DateType::class, array(
      'required' =&gt; false,
      'widget' =&gt; 'single_text',
      'empty_data' =
  ))
  </code></pre>
  
  <p>however the field is optional to fill in for the form.  When I submit the form with an empty value for that field I receive the error:</p>
  
  <blockquote>
    <p>Expected argument of type "DateTimeInterface", "NULL" given.</p>
  </blockquote>
  
  <p>erroring on the line</p>
  
  <pre><code>$form-&gt;handleRequest($request);
  </code></pre>
  
  <p>It seems I need to pass in a default value (i.e. - 1/1/1990) as null is not a valid DateTime value?  What is the correct way to handle this?  I looked on <a href="https://symfony.com/doc/current/reference/forms/types/datetime.html" rel="noreferrer">this</a> page and tried various things (i.e. - changing input type) but nothing seems to fix this.</p>
  
  <p>Thanks in advance! </p>
  
  <p>My Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use NoProtocol\Encryption\MySQL\AES\Crypter;
  
  /**
   * @ORM\Entity(repositoryClass="App\Repository\PatientsRepository")
   * @ORM\Table(name="Patients")
   */
  class PatientSearch
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @ORM\Column(type="integer")
       */
      private $PatientId;
  
      /**
       * @ORM\Column(type="string", length=100, nullable=true)
       */
      private $Address1;
  
      /**
       * @ORM\Column(type="string", length=100, nullable=true)
       */
      private $Address2;
  
      /**
       * @ORM\Column(type="string", length=50, nullable=true)
       */
      private $City;
  
      /**
       * @ORM\Column(type="string", length=2, nullable=true)
       */
      private $State;
  
      /**
       * @ORM\Column(type="date", nullable=true)
       */
      private $DateOfBirth;
  
      /**
       * @ORM\Column(type="string", length=50)
       */
      private $LastName;
  
      /**
       * @ORM\Column(type="string", length=50)
       */
      private $FirstName;
  
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
  
      public function getPatientId(): ?int
      {
          return $this-&gt;PatientId;
      }
  
      public function setPatientId(int $PatientId): self
      {
          $this-&gt;PatientId = $PatientId;
  
          return $this;
      }
  
      public function getAddress1(): ?string
      {
          return $this-&gt;Address1;
      }
  
      public function setAddress1(string $Address1): self
      {
          $this-&gt;Address1 = $Address1;
  
          return $this;
      }
  
      public function getAddress2(): ?string
      {
          return $this-&gt;Address2;
      }
  
      public function setAddress2(string $Address2): self
      {
          $this-&gt;Address2 = $Address2;
  
          return $this;
      }
  
      public function getCity(): ?string
      {
          return $this-&gt;City;
      }
  
      public function setCity(string $City): self
      {
          $this-&gt;City = $City;
  
          return $this;
      }
  
      public function getState(): ?string
      {
          return $this-&gt;State;
      }
  
      public function setState(string $State): self
      {
          $this-&gt;State = $State;
  
          return $this;
      }
  
      public function getDateOfBirth(): ?\DateTimeInterface
      {
          return $this-&gt;DateOfBirth;
      }
  
      public function setDateOfBirth(\DateTimeInterface $DateOfBirth): self
      {
          $this-&gt;DateOfBirth = $DateOfBirth;
  
          return $this;
      }
  
      public function getLastName(): ?string
      {
          return $this-&gt;LastName;
      }
  
      public function setLastName(string $LastName): self
      {
          $this-&gt;LastName = $LastName;
  
          return $this;
      }
  
      public function getFirstName(): ?string
      {
          return $this-&gt;FirstName;
      }
  
      public function setFirstName(string $FirstName): self
      {
          $this-&gt;FirstName = $FirstName;
  
          return $this;
      }
  }
  </code></pre>
  
  <p>Type:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Form;
  
  use App\Entity\PatientSearch;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\Extension\Core\Type\DateType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class PatientSearchType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('FirstName', TextType::class, array('required' =&gt; false))
              -&gt;add('LastName', TextType::class, array('required' =&gt; false))
              -&gt;add('DateOfBirth', DateType::class, array(
                  'required' =&gt; false,
                  'widget' =&gt; 'single_text',
                  'empty_data'  =&gt; '',
                  ))
          ;
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults([
              'data_class' =&gt; PatientSearch::class,
          ]);
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Controller;
  
  use App\Form\PatientSearchType;
  use App\Entity\PatientSearch;
  use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  use Symfony\Component\Routing\Annotation\Route;
  use Symfony\Component\HttpFoundation\Request;
  
  class PatientSearchController extends AbstractController
  {
      /**
       * @Route("/patient/search", name="patient_search")
       */
      public function index(Request $request)
      {
          $form = $this-&gt;createForm(PatientSearchType::class);
  
          $form-&gt;handleRequest($request);
  
          dump($form);
          dump($form-&gt;isValid());
  
          $search = null;
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              $searchFormData = $form-&gt;getData();
              dump($searchFormData);
              $search = $this-&gt;getDoctrine()
                  -&gt;getRepository(PatientSearch::class)
                  -&gt;findBy(array(
                      'LastName' =&gt; $searchFormData-&gt;getLastName(),
                      'FirstName' =&gt; $searchFormData-&gt;getFirstName(),
                      'DateOfBirth' =&gt; $searchFormData-&gt;getDateOfBirth(),
                      ));
  
          }
  
          return $this-&gt;render('patient_search/index.html.twig', [
              'search_form' =&gt; $form-&gt;createView(),
              'search' =&gt; $search
          ]);
      }
  }
  </code></pre>
  
  <p>Error Dump (after setting 'empty_data' => '1/1/2000)':</p>
  
  <pre><code>FormErrorIterator {#542 ▼
    -form: Form {#507 ▼
      -config: FormBuilder {#508 ▶}
      -parent: null
      -children: OrderedHashMap {#509 ▼
        -elements: array:3 [▼
          "FirstName" =&gt; Form {#510 ▶}
          "LastName" =&gt; Form {#513 ▶}
          "DateOfBirth" =&gt; Form {#516 ▼
            -config: FormBuilder {#517 ▶}
            -parent: Form {#507}
            -children: OrderedHashMap {#518 ▶}
            -errors: array:1 [▼
              0 =&gt; FormError {#825 ▼
                #messageTemplate: "This value is not valid."
                #messageParameters: array:1 [▼
                  "{{ value }}" =&gt; "1/1/2000"
                ]
                #messagePluralization: null
                -message: "This value is not valid."
                -cause: ConstraintViolation {#794 ▼
                  -message: "This value is not valid."
                  -messageTemplate: "This value is not valid."
                  -parameters: array:1 [▶]
                  -plural: null
                  -root: Form {#507}
                  -propertyPath: "children[DateOfBirth]"
                  -invalidValue: "1/1/2000"
                  -constraint: Form {#532 ▶}
                  -code: "1dafa156-89e1-4736-b832-419c2e501fca"
                  -cause: TransformationFailedException {#520 ▶}
                }
                -origin: Form {#516}
              }
            ]
            -submitted: true
            -clickedButton: null
            -modelData: null
            -normData: null
            -viewData: "1/1/2000"
            -extraData: []
            -transformationFailure: TransformationFailedException {#520 ▼
              #message: "Unable to reverse value for property path "DateOfBirth": Date parsing failed: U_PARSE_ERROR"
              #code: 0
              #file: "/mnt/c/Users/ElementZero/source/php/phleb-manager/vendor/symfony/form/Form.php"
              #line: 1137
              -previous: TransformationFailedException {#523 ▶}
              trace: {▶}
            }
            -defaultDataSet: true
            -lockSetData: false
          }
        ]
        -orderedKeys: array:3 [▶]
        -managedCursors: []
      }
      -errors: []
      -submitted: true
      -clickedButton: null
      -modelData: PatientSearch {#501 ▶}
      -normData: PatientSearch {#501 ▶}
      -viewData: PatientSearch {#501 ▶}
      -extraData: []
      -transformationFailure: null
      -defaultDataSet: true
      -lockSetData: false
    }
    -errors: []
  }
  </code></pre>