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

Commit5e2b161

Browse files
Added documentation for GroupSequence
1 parentb84abc6 commit5e2b161

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

‎book/validation.rst

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,9 @@ With this configuration, there are two validation groups:
771771

772772
* ``Default`` - contains the constraints not assigned to any other group;
773773

774+
* ``User`` - contains the constraints that belongs to group ``Default``
775+
(this group is usefull for:ref:`book-validation-group-sequence`);
776+
774777
* ``registration`` - contains the constraints on the ``email`` and ``password``
775778
fields only.
776779

@@ -779,13 +782,136 @@ as the second argument to the ``validate()`` method::
779782

780783
$errors = $validator->validate($author, array('registration'));
781784

785+
If no groups are specified, all constraints that belong in group ``Default``
786+
will be applied.
787+
782788
Of course, you'll usually work with validation indirectly through the form
783789
library. For information on how to use validation groups inside forms, see
784790
:ref:`book-forms-validation-groups`.
785791

786792
..index::
787793
single: Validation; Validating raw values
788794

795+
.. _book-validation-group-sequence:
796+
797+
Group Sequence
798+
--------------
799+
800+
In some cases, you want to validate your groups by steps. To do this, you can
801+
use the ``GroupSequence`` feature. In the case an object defines a group sequence,
802+
the groups in the group sequence will be validated in order.
803+
804+
Group sequences cannot contain the group ``Default``, this would create a
805+
cycle, but need to contain the group ``{ClassName}`` instead.
806+
807+
For example, suppose you have a ``User`` class and want to validate that the
808+
username and the password are different only if all other validations passes
809+
(in order to avoid multiple error messages).
810+
811+
..configuration-block::
812+
813+
..code-block::yaml
814+
815+
# src/Acme/BlogBundle/Resources/config/validation.yml
816+
Acme\BlogBundle\Entity\User:
817+
group_sequence:
818+
-User
819+
-Strict
820+
getters:
821+
passwordLegal:
822+
-"True":
823+
message:"The password cannot match your username"
824+
groups:[Strict]
825+
properties:
826+
username:
827+
-NotBlank:~
828+
password:
829+
-NotBlank:~
830+
831+
..code-block::php-annotations
832+
833+
// src/Acme/BlogBundle/Entity/User.php
834+
namespace Acme\BlogBundle\Entity;
835+
836+
use Symfony\Component\Security\Core\User\UserInterface;
837+
use Symfony\Component\Validator\Constraints as Assert;
838+
839+
/**
840+
* @Assert\GroupSequence({"Strict", "User"})
841+
*/
842+
class User implements UserInterface
843+
{
844+
/**
845+
* @Assert\NotBlank
846+
*/
847+
private $username;
848+
849+
/**
850+
* @Assert\NotBlank
851+
*/
852+
private $password;
853+
854+
/**
855+
* @Assert\True(message="The password cannot match your username", groups={"Strict"})
856+
*/
857+
public function isPasswordLegal()
858+
{
859+
return ($this->username !== $this->password);
860+
}
861+
}
862+
863+
..code-block::xml
864+
865+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml-->
866+
<classname="Acme\BlogBundle\Entity\User">
867+
<propertyname="username">
868+
<constraintname="NotBlank" />
869+
</property>
870+
<propertyname="password">
871+
<constraintname="NotBlank" />
872+
</property>
873+
<getterproperty="passwordLegal">
874+
<constraintname="True">
875+
<optionname="message">The password cannot match your username</option>
876+
<optionname="groups">
877+
<value>Strict</value>
878+
</option>
879+
</constraint>
880+
</getter>
881+
<group-sequence>
882+
<value>User</value>
883+
<value>Strict</value>
884+
</group-sequence>
885+
</class>
886+
887+
..code-block::php
888+
889+
// src/Acme/BlogBundle/Entity/User.php
890+
namespace Acme\BlogBundle\Entity;
891+
892+
use Symfony\Component\Validator\Mapping\ClassMetadata;
893+
use Symfony\Component\Validator\Constraints as Assert;
894+
895+
class User
896+
{
897+
public static function loadValidatorMetadata(ClassMetadata $metadata)
898+
{
899+
$metadata->addPropertyConstraint('username', new Assert\NotBlank());
900+
$metadata->addPropertyConstraint('password', new Assert\NotBlank());
901+
902+
$metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
903+
'message' => 'The password cannot match your first name',
904+
'groups' => array('Strict'),
905+
)));
906+
907+
$metadata->setGroupSequence(array('User', 'Strict'));
908+
}
909+
}
910+
911+
In this example, it will first validate all constraints in group ``User``
912+
(eg. ``Default``). Only if all constraints in that group are valid, the second
913+
group, ``Strict``, will be validated.
914+
789915
.. _book-validation-raw-values:
790916

791917
Validating Values and Arrays

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp