10

So, I want to be able send a null option to my DOB field.

Here is my form builder:

->add('birthDate', DateType::class, array(
                'widget' => 'single_text',
                'format' => 'yyyy-MM-dd'))

And here is those field in my entity

 /**
     * @ORM\Column(
     *     type="date",
     *     nullable=true
     * )
     * @JMS\Groups("single")
     *
     * @var \DateTime
     */
    protected $birthDate;

When I`m trying to send a null I got an error msg

Expected argument of type "DateTime", "NULL" given

any ideas?

CRITICAL - Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: "Expected argument of type "DateTime", "NULL" given" at /var/www/server.local/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 253 



  $type = $trace[$i]['args'][0];
    $type = is_object($type) ? get_class($type) : gettype($type);
    throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given', substr($message, $pos, strpos($message, ',', $pos) - $pos), $type));
}

}

9
  • 1
    When you are getting this error? Commented Apr 13, 2016 at 12:53
  • When submitting form whith birthDate field Commented Apr 13, 2016 at 12:56
  • and also have you made sure that the DB schema is in sync with the entity ? Commented Apr 13, 2016 at 12:56
  • I`m sure, schema in sync with DB Commented Apr 13, 2016 at 13:00
  • 1
    Does the setter for your $birthDate look like this: setBirthDate(\DateTime $value)? If yes, change it to setBirthDate(\DateTime $value = null) Commented Apr 13, 2016 at 13:54

4 Answers 4

22

In this case, the problem was caused by PHP type hinting. If you use type hinting (for instance setBirthDate(\DateTime $value)) then PHP forces you that you actually provide a DateTime object. Obviously, null is not such an object. To resolve this problem, it is possible to give $value a default value like this: setBirthDate(\DateTime $value = null).

This is documented behavior and explained in the PHP Documentation (http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration).

Relevant passage:

To specify a type declaration, the type name should be added before the parameter name. The declaration can be made to accept NULL values if the default value of the parameter is set to NULL.

Sign up to request clarification or add additional context in comments.

1 Comment

Note: For PHP 7.1 you can also type hint it as optional nullable: setBirthDate(?\DateTime $value)
5

The problem occurs due type-hinted setter as it is mentioned in the comments. There are two solutions:

1. Use 'by_reference' => true on your form:

$builder->add(
    'birthDate',
    DateType::class,
    [
        'widget' => 'single_text',
        'format' => 'yyyy-MM-dd',
        'by_reference' => true,
    ]
);

2. Let your setter accept null:

public function setBirthDate(\DateTime $value = null)
{
   .....
}

1 Comment

Your first solution makes more sense. 2nd one is also working, but that will make the field hold unwanted data(null)
3

Don't pass any values to it. Make the field not required by doing this:

->add(
    'birthDate', 
    DateType::class, 
    array(
        'required' => false,
        'widget' => 'single_text',
        'format' => 'yyyy-MM-dd'
    )
)

7 Comments

How are you setting the form data? ->handleRequest($request); ?
No. As you see I'm using ORM annotations, so that's why your solution doesn't work for me, that's how I think
ORM annotations do nothing for validation, they're mapping to the database. If you were using @Assert\Datetime or @Assert\NotNull then I could understand the validation failure. Please use Symfony's recommended way of form submissions, here
It`s to late to use recommended way, because I already have a big working project.
Okay, so how are you setting the form data?
|
0

I have been using DOB field in my project. Try this. My ORM file looks like this <field name="dob" type="date" column="dob" nullable="true"/>

->add('dob','birthday',array(
            'widget' => 'single_text',
            'format' => 'dd-MM-yyyy',
            'required' => false,
            'attr' => array('class' => 'datepicker',
                'data-provide' => 'datepicker','data-date-format' => 'dd-mm-yyyy')
        ))

1 Comment

It`s not so simple for me. I have created a angular directive, that consist of 3 dropdown inputs for day, date and year

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.