1

I get this error message:

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

This is the code of my form in symfony

$builder->add('dateOfBirth', 'datetime', [
    'widget' => 'single_text',
    'label'  => 'Geboortedatum',
    'attr' => ['class' => 'form-control birthdaypicker'],
    'label_attr' => ['class' => 'col-sm-2 control-label']
]);

And this is the template:

<div class="form-group">
    {{ form_label(form_pupil.dateOfBirth) }}
    <div class="col-sm-10">
        {{ form_widget(form_pupil.dateOfBirth) }}
        <small>{{ form_errors(form_pupil.dateOfBirth) }}</small>
    </div>
</div>

Document (Entity)

/**
 * @MongoDB\Date
 * @Assert\NotBlank(message="Een geboortedatum is vereist")
 * @Assert\Length(max=10, maxMessage="Een geboortedatum kan maximaal 10 karakters bevatten")
 */
 protected $dateOfBirth;

In my document(entity) it's also a date type. I have no idea why Symfony expects a string..

3
  • Can you post more details about the error message? Is it coming from Symfony form/validation or from Doctrine ODM? Commented Mar 18, 2014 at 11:06
  • vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/LengthValidator.php at line 33 - } if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { throw new UnexpectedTypeException($value, 'string'); } $stringValue = (string) $value; Commented Mar 18, 2014 at 11:13
  • In case anyone hits this error, I came across when I had an invalid validation rule in a validation.yml config file. Commented Feb 28, 2016 at 21:40

2 Answers 2

3
@Assert\Length(max=10, maxMessage="Een geboortedatum kan maximaal 10 karakters bevatten")

Can't check the length of a datetime object! (removed this assert and it worked)

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

Comments

0

Try this :

$builder->add('dateOfBirth', 'datetime', [
    'data' => new \DateTime(),
    'widget' => 'single_text',
    'label'  => 'Geboortedatum',
    'attr' => ['class' => 'form-control birthdaypicker'],
    'label_attr' => ['class' => 'col-sm-2 control-label']
]);

4 Comments

Can't use \DateTime(), Mongo doesn't support datetime and i only need date.
if you replace datetime by date removing 'data' => new \DateTime(), line it works ?
I changed the second parameter of add to date. And removed the 'data' in the third parameter array. But the main problem was the assert validation in my Document. I used @Assert\Length on a date object. Which caused the error.
why do not you do the check to the creation of form?

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.