Skip to content

Commit 09d1d91

Browse files
committed
Merge pull request #2097 from WouterJ/ref_constraint_php
Added missing PHP and XML formats in new constraints
2 parents cae9f9f + 2b8e09b commit 09d1d91

File tree

4 files changed

+109
-14
lines changed

4 files changed

+109
-14
lines changed

reference/constraints/Count.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ you might add the following:
4444
.. code-block:: php-annotations
4545
4646
// src/Acme/EventBundle/Entity/Participant.php
47+
namespace Acme\EventBundle\Entity;
48+
4749
use Symfony\Component\Validator\Constraints as Assert;
4850
4951
class Participant
@@ -73,6 +75,26 @@ you might add the following:
7375
</property>
7476
</class>
7577
78+
.. code-block:: php
79+
80+
// src/Acme/EventBundle/Entity/Participant.php
81+
namespace Acme\EventBundle\Entity;
82+
83+
use Symfony\Component\Validator\Mapping\ClassMetadata;
84+
use Symfony\Component\Validator\Constraints as Assert;
85+
86+
class Participant
87+
{
88+
public static function loadValidatorMetadata(ClassMetadata $data)
89+
{
90+
$metadata->addPropertyConstraint('emails', new Assert\Count(array(
91+
'min' => 1,
92+
'max' => 5,
93+
'minMessage' => 'You must specify at least one email',
94+
'maxMessage' => 'You cannot specify more than {{ limit }} emails',
95+
)));
96+
}
97+
}
7698
7799
Options
78100
-------

reference/constraints/Length.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ To verify that the ``firstName`` field length of a class is between "2" and
4444
.. code-block:: php-annotations
4545
4646
// src/Acme/EventBundle/Entity/Participant.php
47+
namespace Acme\EventBundle\Entity;
48+
4749
use Symfony\Component\Validator\Constraints as Assert;
4850
4951
class Participant
@@ -73,6 +75,27 @@ To verify that the ``firstName`` field length of a class is between "2" and
7375
</property>
7476
</class>
7577
78+
.. code-block:: php
79+
80+
// src/Acme/EventBundle/Entity/Participant.php
81+
namespace Acme\EventBundle\Entity;
82+
83+
use Symfony\Component\Validator\Mapping\ClassMetadata;
84+
use Symfony\Component\Validator\Constraints as Assert;
85+
86+
class Participant
87+
{
88+
public static function loadValidatorMetadata(ClassMetadata $metadata)
89+
{
90+
$metadata->addPropertyConstraint('firstName', new Assert\Length(array(
91+
'min' => 2,
92+
'max' => 50,
93+
'minMessage' => 'Your first name must be at least {{ limit }} characters length',
94+
'maxMessage' => 'Your first name cannot be longer than than {{ limit }} characters length',
95+
)));
96+
}
97+
}
98+
7699
Options
77100
-------
78101

reference/constraints/Range.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ the following:
4343
.. code-block:: php-annotations
4444
4545
// src/Acme/EventBundle/Entity/Participant.php
46+
namespace Acme\EventBundle\Entity;
47+
4648
use Symfony\Component\Validator\Constraints as Assert;
4749
4850
class Participant
@@ -72,6 +74,27 @@ the following:
7274
</property>
7375
</class>
7476
77+
.. code-block:: php
78+
79+
// src/Acme/EventBundle/Entity/Participant.php
80+
namespace Acme\EventBundle\Entity;
81+
82+
use Symfony\Component\Validator\Mapping\ClassMetadata;
83+
use Symfony\Component\Validator\Constraints as Assert;
84+
85+
class Participant
86+
{
87+
public static function loadValidatorMetadata(ClassMetadata $metadata)
88+
{
89+
$metadata->addPropertyConstraint('height', new Assert\Range(array(
90+
'min' => 120,
91+
'max' => 180,
92+
'minMessage' => 'You must be at least 120cm tall to enter',
93+
'maxMessage' => 'You cannot be taller than 180cm to enter',
94+
)));
95+
}
96+
}
97+
7598
Options
7699
-------
77100

reference/constraints/UserPassword.rst

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,47 @@ password:
4444
4545
.. code-block:: php-annotations
4646
47-
// src/Acme/UserBundle/Form/Model/ChangePassword.php
48-
namespace Acme\UserBundle\Form\Model;
49-
50-
use Symfony\Component\Security\Core\Validator\Constraint as SecurityAssert;
51-
52-
class ChangePassword
53-
{
54-
/**
55-
* @SecurityAssert\UserPassword(
56-
* message = "Wrong value for your current password"
57-
* )
58-
*/
59-
protected $oldPassword;
60-
}
47+
// src/Acme/UserBundle/Form/Model/ChangePassword.php
48+
namespace Acme\UserBundle\Form\Model;
49+
50+
use Symfony\Component\Security\Core\Validator\Constraint as SecurityAssert;
51+
52+
class ChangePassword
53+
{
54+
/**
55+
* @SecurityAssert\UserPassword(
56+
* message = "Wrong value for your current password"
57+
* )
58+
*/
59+
protected $oldPassword;
60+
}
61+
62+
.. code-block:: xml
63+
64+
<!-- src/UserBundle/Resources/config/validation.xml -->
65+
<class name="Acme\UserBundle\Form\Model\ChangePassword">
66+
<property name="Symfony\Component\Security\Core\Validator\Constraint\UserPassword">
67+
<option name="message">Wrong value for your current password</option>
68+
</property>
69+
</class>
70+
71+
.. code-block:: php
72+
73+
// src/Acme/UserBundle/Form/Model/ChangePassword.php
74+
namespace Acme\UserBundle\Form\Model;
75+
76+
use Symfony\Component\Validator\Mapping\ClassMetadata;
77+
use Symfony\Component\Security\Core\Validator\Constraint as SecurityAssert;
78+
79+
class ChangePassword
80+
{
81+
public static function loadValidatorData(ClassMetadata $metadata)
82+
{
83+
$metadata->addPropertyConstraint('oldPassword', new SecurityAssert\UserPassword(array(
84+
'message' => 'Wrong value for your current password',
85+
)));
86+
}
87+
}
6188
6289
Options
6390
-------

0 commit comments

Comments
 (0)