Question

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

Était-ce utile?

La solution

@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)

Autres conseils

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']
]);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top