3

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();
        }
4
  • Where does $errors2 come from when the valudation is being assigned to $errors. Is that a typo? Commented May 28, 2012 at 13:42
  • I didn't copy all code and apparently cut too much. I added missing lines. Commented May 28, 2012 at 13:47
  • Is there something else missing? You are binding the $request to $form and then validating $profile. Presumably you are doing something like $profile->setAdded($form->getData()->getAdded()) somewhere? Commented May 30, 2012 at 11:26
  • No I don't - everything is handled by Sf2 forms component. I added more code to controller. Commented May 30, 2012 at 12:20

1 Answer 1

1

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 }
Sign up to request clarification or add additional context in comments.

1 Comment

There are other fields (like firstName) which are validated correct (as not empty and min length = 3)

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.