Symfony 2 form - date widget and validator

126 views Asked by At

I have field birthday:

/**
 * @ORM\Column(type="date", nullable=true)
 */
protected $birthday;

and Form:

->add('birthday', null, array('widget' => 'single_text', 'format' => 'dd-MM-yyyy'))

How can I change message for this if value is not correctly?

I try in Entity:

public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('birthday', new Assert\Date(array('message' => 'test')));
    }

But this validator is not used... So where is validator for this?

1

There are 1 answers

0
Jean D. On

Seems like a DateValidator::validate issue:

The method contains the following check before doing any validation:

if (null === $value || '' === $value || $value instanceof \DateTime) {
        return;
}

The $value parameter is of type \DateTime event when using the single_text option.

A possible workaround would be to change the error message at the form level:

$form->add('fieldName', 'date', [
            'widget' => 'single_text',
            'format' => 'dd-MM-yyyy',
            'invalid_message' => 'your message'
        ])

Hope this helps.