Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Validator][GroupSequences] GroupSequences still execute next group if first fail #9939

Closed
@lemoinem

Description

@lemoinem

Hello,

Using the example below (smallest I could think of).

If you try the given controller, you will have the following results:

  • test1 : all OK [as expected]
  • test2 : "is not three" [as expected]
  • test3 :_CRASH_ ""Error: Call to a member function myBar() on a non-object in /myProject/src/Me/MeBundle/Doc/Foo.php line 31"" [_NOT_ as expected]
  • test4 : "bar should not be null" [as expected]

I would expect test3 to give the same result as test4 (Meaning: If the first group of the GroupSequence is violated, the others are not executed).

This is explicitly specified in the doc here :http://symfony.com/doc/current/book/validation.html#group-sequence ""In this example, it will first validate all constraints in the group User (which is the same as the Default group).Only if all constraints in that group are valid, the second group, Strict, will be validated."" (emphasis mine)

and here:#2947 (comment)

cc @bschussek

<?phpnamespaceME\MEBundle\Doc;useSymfony\Component\Validator\ConstraintsasAssert;/** * @Assert\GroupSequence({"FilledIn", "Foo"}) */class Foo{protected$bar;protected$checkBar;publicfunction__construct($bar,$checkBar =false)    {$this->bar =$bar;$this->checkBar =$checkBar;    }/**     * @Assert\True(groups={"Foo"}, message="is not three")     */publicfunctionisThree()    {if ($this->checkBar &&null ===$this->bar) {returnnewBar(null);        }return3 ==$this->getBar()->myBar();    }/**     * @Assert\NotNull(groups={"FilledIn"}, message="bar should not be null")     */publicfunctiongetBar()    {return$this->bar;    }}
<?phpnamespaceMe\MeBundle\Doc;class Bar{protected$bar;publicfunction__construct($bar)    {$this->bar =$bar;    }publicfunctionmyBar()    {return$this->bar;    }}
<?phpnamespaceMe\MeBundle\Controller;useMe\MeBundle\Doc;useSymfony\Bundle\FrameworkBundle\Controller\Controller;useSensio\Bundle\FrameworkExtraBundle\Configuration\Route;useSensio\Bundle\FrameworkExtraBundle\Configuration\Template;useSymfony\Component\HttpFoundation\JsonResponse;class DefaultControllerextends Controller{protectedfunctiongetFooData(Foo$foo)    {$json = [['bar' =>null ===$foo->getBar() ?null : (array)$foo->getBar()]];$violations =$this->get('validator')->validate($foo);$errors = [];foreach($violationsas$violation) {$errors[] =$violation->getMessage();        }$json[] =$errors;return$json;    }/**     * @Route("/test1", defaults={"_format"="json"})     */publicfunctiontest1Action()    {newFoo(null);$foo =newFoo(newBar(3));returnnewJsonResponse($this->getFooData($foo));    }/**     * @Route("/test2", defaults={"_format"="json"})     */publicfunctiontest2Action()    {$foo =newFoo(newBar(4));returnnewJsonResponse($this->getFooData($foo));    }/**     * @Route("/test3", defaults={"_format"="json"})     */publicfunctiontest3Action()    {$foo =newFoo(null);returnnewJsonResponse($this->getFooData($foo));    }/**     * @Route("/test4", defaults={"_format"="json"})     */publicfunctiontest4Action()    {$foo =newFoo(null,true);returnnewJsonResponse($this->getFooData($foo));    }}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp