@@ -29,6 +29,7 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
29
29
class ContainsAlphanumeric extends Constraint
30
30
{
31
31
public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
32
+ public $mode = 'strict'; // If the constraint has configuration options, define them as public properties
32
33
}
33
34
34
35
.. code-block :: php-attributes
@@ -45,8 +46,7 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
45
46
}
46
47
47
48
Add ``@Annotation `` or ``#[\Attribute] `` to the constraint class if you want to
48
- use it as an annotation/attribute in other classes. If the constraint has
49
- configuration options, define them as public properties on the constraint class.
49
+ use it as an annotation/attribute in other classes.
50
50
51
51
.. versionadded :: 5.2
52
52
@@ -103,6 +103,11 @@ The validator class only has one required method ``validate()``::
103
103
// separate multiple types using pipes
104
104
// throw new UnexpectedValueException($value, 'string|int');
105
105
}
106
+
107
+ // access your configuration options like this:
108
+ if ('strict' === $constraint->mode) {
109
+ // ...
110
+ }
106
111
107
112
if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
108
113
// the argument must be a string or an object implementing __toString()
@@ -141,7 +146,7 @@ You can use custom validators like the ones provided by Symfony itself:
141
146
142
147
/**
143
148
* @Assert\NotBlank
144
- * @AcmeAssert\ContainsAlphanumeric
149
+ * @AcmeAssert\ContainsAlphanumeric(mode="loose")
145
150
*/
146
151
protected $name;
147
152
@@ -161,7 +166,7 @@ You can use custom validators like the ones provided by Symfony itself:
161
166
// ...
162
167
163
168
#[Assert\NotBlank]
164
- #[AcmeAssert\ContainsAlphanumeric]
169
+ #[AcmeAssert\ContainsAlphanumeric(options: ['mode' => 'loose']) ]
165
170
protected $name;
166
171
167
172
// ...
0 commit comments