Skip to content

Commit bf3978c

Browse files
committed
[cookbook][validation] Minor tweaks for #1403
1 parent f15c85e commit bf3978c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

cookbook/validation/custom_constraint.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ How to create a Custom Validation Constraint
66

77
You can create a custom constraint by extending the base constraint class,
88
:class:`Symfony\\Component\\Validator\\Constraint`.
9-
As an example we're going to create a simple validator that checks if string
9+
As an example we're going to create a simple validator that checks if a string
1010
contains only alphanumeric characters.
1111

1212
Creating Constraint class
@@ -23,17 +23,18 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
2323
*/
2424
class ContainsAlphanumeric extends Constraint
2525
{
26-
public $message = 'Missing at least one alphanumeric character in "%string%" string';
26+
public $message = 'The string "%string%" contains an illegal character: it can only contain letters or numbers.';
2727
}
2828
2929
.. note::
3030

3131
The ``@Annotation`` annotation is necessary for this new constraint in
3232
order to make it available for use in classes via annotations.
33-
Options for your constraint are represented as public properties on the constraint class.
33+
Options for your constraint are represented as public properties on the
34+
constraint class.
3435

35-
Creating Validator itself
36-
-------------------------
36+
Creating the Validator itself
37+
-----------------------------
3738

3839
As you can see, a constraint class is fairly minimal. The actual validation is
3940
performed by a another "constraint validator" class. The constraint validator
@@ -76,8 +77,8 @@ The validator class is also simple, and only has one required method: ``isValid`
7677
Don't forget to call ``setMessage`` to construct an error message when the
7778
value is invalid.
7879

79-
Using newly created validator
80-
-----------------------------
80+
Using the new Validator
81+
-----------------------
8182

8283
Using custom validators is very easy, just as the ones provided by Symfony2 itself:
8384

@@ -147,6 +148,10 @@ Using custom validators is very easy, just as the ones provided by Symfony2 itse
147148
}
148149
}
149150
151+
If your constraint contains options, then they should be public properties
152+
on the custom Constraint class you created earlier. These options can be
153+
configured like options on core Symfony constraints.
154+
150155
Constraint Validators with Dependencies
151156
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152157

0 commit comments

Comments
 (0)