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

Commit09ae39d

Browse files
committed
Adding details about the 2.4 API as comments
1 parent9625778 commit09ae39d

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

‎cookbook/validation/custom_constraint.rst‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,34 @@ The validator class is also simple, and only has one required method ``validate(
6565
public function validate($value, Constraint $constraint)
6666
{
6767
if (!preg_match('/^[a-zA-Za0-9]+$/', $value, $matches)) {
68+
// If you're using the new 2.5 validation API (you probably are!)
6869
$this->context->buildViolation($constraint->message)
6970
->setParameter('%string%', $value)
7071
->addViolation();
7172
);
73+
74+
// If you're using the old 2.4 validation API
75+
/*
76+
$this->context->addViolation(
77+
$constraint->message,
78+
array('%string%' => $value)
79+
);
80+
*/
7281
}
7382
}
7483
}
7584

76-
..note::
77-
78-
The ``validate`` method does not return a value; instead, it adds violations
79-
to the validator's ``context`` property. Therefore, a value could be considered
80-
as being valid if it causes no violations to be added to the context.The
81-
violation is constructed and added to the context using a
82-
``ConstraintViolationBuilder`` returned by the ``buildViolation`` method
83-
call. The parameter given to the ``buildViolation`` call is the error message
84-
to use for that violation. The ``addViolation`` method call finally adds the
85-
violation to the context.
85+
Inside ``validate``, you don't need to return a value. Instead, you add violations
86+
to the validator's ``context`` property and a value will be considered valid
87+
if it causes no violations. The ``buildViolation`` takes the error message
88+
as its argument and returns an instance of
89+
:class:`Symfony\\Component\\Validator\\Violation\\ConstraintViolationBuilder`
90+
The ``addViolation`` method call finally adds the violation to the context.
8691

8792
..versionadded::2.5
88-
The ``buildViolation`` method was added in Symfony 2.5. For usage examples with
89-
older Symfony versions, see the corresponding versions of this documentation page.
93+
The ``buildViolation`` method was added in Symfony 2.5. For usage examples
94+
with older Symfony versions, see the corresponding versions of this documentation
95+
page.
9096

9197
Using the new Validator
9298
-----------------------

‎reference/constraints/Callback.rst‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Configuration
5151
5252
use Symfony\Component\Validator\Constraints as Assert;
5353
use Symfony\Component\Validator\Context\ExecutionContextInterface;
54+
// if you're using the older 2.4 validation API, you'll need this instead
55+
// use Symfony\Component\Validator\ExecutionContextInterface;
5456
5557
class Author
5658
{
@@ -101,6 +103,8 @@ those errors should be attributed::
101103

102104
// ...
103105
use Symfony\Component\Validator\Context\ExecutionContextInterface;
106+
// if you're using the older 2.4 validation API, you'll need this instead
107+
// use Symfony\Component\Validator\ExecutionContextInterface;
104108

105109
class Author
106110
{
@@ -114,16 +118,26 @@ those errors should be attributed::
114118

115119
// check if the name is actually a fake name
116120
if (in_array($this->getFirstName(), $fakeNames)) {
121+
// If you're using the new 2.5 validation API (you probably are!)
117122
$context->buildViolation('This name sounds totally fake!')
118123
->atPath('firstName')
119124
->addViolation();
125+
126+
// If you're using the old 2.4 validation API
127+
/*
128+
$context->addViolationAt(
129+
'firstName',
130+
'This name sounds totally fake!'
131+
);
132+
*/
120133
}
121134
}
122135
}
123136

124137
..versionadded::2.5
125-
The ``buildViolation`` method was added in Symfony 2.5. For usage examples with
126-
older Symfony versions, see the corresponding versions of this documentation page.
138+
The ``buildViolation`` method was added in Symfony 2.5. For usage examples
139+
with older Symfony versions, see the corresponding versions of this documentation
140+
page.
127141

128142
Static Callbacks
129143
----------------
@@ -138,10 +152,17 @@ have access to the object instance, they receive the object as the first argumen
138152

139153
// check if the name is actually a fake name
140154
if (in_array($object->getFirstName(), $fakeNames)) {
155+
// If you're using the new 2.5 validation API (you probably are!)
141156
$context->buildViolation('This name sounds totally fake!')
142157
->atPath('firstName')
143158
->addViolation()
144159
;
160+
161+
// If you're using the old 2.4 validation API
162+
$context->addViolationAt(
163+
'firstName',
164+
'This name sounds totally fake!'
165+
);
145166
}
146167
}
147168

@@ -156,6 +177,8 @@ your validation function is ``Vendor\Package\Validator::validate()``::
156177
namespace Vendor\Package;
157178

158179
use Symfony\Component\Validator\Context\ExecutionContextInterface;
180+
// if you're using the older 2.4 validation API, you'll need this instead
181+
// use Symfony\Component\Validator\ExecutionContextInterface;
159182

160183
class Validator
161184
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp