문제

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

도움이 되었습니까?

해결책

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

다른 팁

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']
]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top