Reputation: 2945
When I submit form the text field containing date isn't validated although I defined constraint in entity. What is incorrect? Do I need to write custom date validator for text field containing date?
In my form class I have
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('added', 'date', array(
'required' => false,
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'attr' => array(
'class' => 'datepicker'
)
))
}
In entity
/**
* @var date
*
* @Assert\Date(message = "test")
* @ORM\Column(name="added", type="date", nullable=true)
*/
private $added;
And in controller (I need those errors listed)
$request = $this->getRequest();
$r = $this->getProfileRepository();
$profile = $id ? $r->find($id) : new \Alden\XyzBundle\Entity\Profile();
/* @var $profile \Alden\XyzBundle\Entity\Profile */
$form = $this->createForm(new ProfileType(), $profile);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
$errors = $this->get('validator')->validate($profile);
foreach ($errors as $e)
{
/* @var $e \Symfony\Component\Validator\ConstraintViolation */
$errors2[$e->getPropertyPath()] = $e->getMessage();
}
if (count($errors2))
{
...
} else {
$em = $this->getEntityManager();
$em->persist($profile);
$em->flush();
}
Upvotes: 3
Views: 2102
Reputation: 4957
You may need to update your configuration. According to the Validation section of the Symfony2 book:
The Symfony2 validator is enabled by default, but you must explicitly enable annotations if you're using the annotation method to specify your constraints:
For example:
# app/config/config.yml
framework:
validation: { enable_annotations: true }
Upvotes: 1