Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 885 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Symfony2 DateTime null accept

#1
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));
}
}
Reply

#2
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'
)
)
Reply

#3
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')
))
Reply

#4
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 (

[To see links please register here]

).

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.
Reply

#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)
{
.....
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through