symfony: Assert\Valid not working as expected with Embedded Forms and validation groups

The Assert\Valid is not taken into account and the form is just considered valid, even if the constraints are not met. The issue can be reproduced by taking the example over here : http://symfony.com/doc/current/book/forms.html#embedding-a-single-object

  • Add any constraint to Entity\Category with: groups={"validationgroup"}
  • Change @Assert\Type(...) in Entity\Task to @Assert\Valid
  • Add 'validation_groups' => array('validationgroup') to Form\Type\CategoryType in $resolver->setDefaults
  • Remove 'cascade_validation' => true, from Form/Type/TaskType. As we use Assert\Valid, this should not be necessary, or am I wrong?
  • Create a controller:
class TestController extends Controller
{
    public function indexAction()
    {
        $form = $this->createForm(new TaskType(), new Task());
        $form->add('save', 'submit');

        $form->handleRequest($this->getRequest());

        if($form->isValid()){
            echo '<p>the form is valid...</p>';
        }

        return $this->render('AcmeDemoBundle:Test:taskform.html.twig', array(
            'form' => $form->createView()
        ));
    }
}
  • and twig template :
<h3>Add task</h3>
{{ form(form) }}

I can provide you this bundle, zipped, if you want. I have also found that (by placing a debug closure in validation_groups) the validation_groups is read by the validator but it doesn’t seem to be used.

Tested with version 2.3.7

capture

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 38 (28 by maintainers)

Most upvoted comments

Maybe the crucial point is that the “validation_groups” setting is only respected on the root form (unless “cascade_validation” is used), because that’s the form which triggers the validation process.

I think this needs to be in the documentation for Forms. If I’m reading this correctly, then setting validation_groups on embedded forms have no effect, and only the validation_groups on parent form’s matter?

It seems counter-intuitive to me that I’d have to set a validation group on the parent form when it only applies to the underlying object for an embedded form.

Yes this is the crucial point. I think it makes no sense to ignore the child validation_groups settings. I think you need to have your validation group logic separated to each form. You don’t want to duplicate the logic.