@@ -221,106 +221,50 @@ Constraint Validators with Custom Options
221221~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222222
223223If you want to add some configuration options to your custom constraint, first
224- define those options as public properties on the constraint class:
224+ define those options as public properties on the constraint class::
225225
226- .. configuration-block ::
227-
228- .. code-block :: php-annotations
229-
230- // src/Validator/Foo.php
231- namespace App\Validator;
232-
233- use Symfony\Component\Validator\Constraint;
226+ // src/Validator/Foo.php
227+ namespace App\Validator;
234228
235- /**
236- * @Annotation
237- */
238- class Foo extends Constraint
239- {
240- public $mandatoryFooOption;
241- public $message = 'This value is invalid';
242- public $optionalBarOption = false;
243-
244- public function __construct(
245- $mandatoryFooOption,
246- string $message = null,
247- bool $optionalBarOption = null,
248- array $groups = null,
249- $payload = null,
250- array $options = []
251- ) {
252- if (\is_array($mandatoryFooOption)) {
253- $options = array_merge($mandatoryFooOption, $options);
254- } elseif (null !== $mandatoryFooOption) {
255- $options['value'] = $mandatoryFooOption;
256- }
257-
258- parent::__construct($options, $groups, $payload);
259-
260- $this->message = $message ?? $this->message;
261- $this->optionalBarOption = $optionalBarOption ?? $this->optionalBarOption;
262- }
229+ use Symfony\Component\Validator\Constraint;
263230
264- public function getDefaultOption()
265- {
266- // If no associative array is passed to the constructor this
267- // property is set instead.
231+ #[\Attribute]
232+ class Foo extends Constraint
233+ {
234+ public $mandatoryFooOption;
235+ public $message = 'This value is invalid';
236+ public $optionalBarOption = false;
268237
269- return 'mandatoryFooOption';
238+ public function __construct(
239+ $mandatoryFooOption,
240+ string $message = null,
241+ bool $optionalBarOption = null,
242+ array $groups = null,
243+ $payload = null,
244+ array $options = []
245+ ) {
246+ if (\is_array($mandatoryFooOption)) {
247+ $options = array_merge($mandatoryFooOption, $options);
248+ } elseif (null !== $mandatoryFooOption) {
249+ $options['value'] = $mandatoryFooOption;
270250 }
271251
272- public function getRequiredOptions()
273- {
274- // return names of options which must be set.
252+ parent::__construct($options, $groups, $payload);
275253
276- return ['mandatoryFooOption'] ;
277- }
254+ $this->message = $message ?? $this->message ;
255+ $this->optionalBarOption = $optionalBarOption ?? $this->optionalBarOption;
278256 }
279257
280- .. code-block :: php-attributes
281-
282- // src/Validator/Foo.php
283- namespace App\Validator;
284-
285- use Symfony\Component\Validator\Constraint;
286-
287- #[\Attribute]
288- class Foo extends Constraint
258+ public function getDefaultOption()
289259 {
290- public $mandatoryFooOption;
291- public $message = 'This value is invalid';
292- public $optionalBarOption = false;
293-
294- public function __construct(
295- $mandatoryFooOption,
296- string $message = null,
297- bool $optionalBarOption = null,
298- array $groups = null,
299- $payload = null,
300- array $options = []
301- ) {
302- if (\is_array($mandatoryFooOption)) {
303- $options = array_merge($mandatoryFooOption, $options);
304- } elseif (null !== $mandatoryFooOption) {
305- $options['value'] = $mandatoryFooOption;
306- }
307-
308- parent::__construct($options, $groups, $payload);
309-
310- $this->message = $message ?? $this->message;
311- $this->optionalBarOption = $optionalBarOption ?? $this->optionalBarOption;
312- }
313-
314- public function getDefaultOption()
315- {
316- return 'mandatoryFooOption';
317- }
260+ return 'mandatoryFooOption';
261+ }
318262
319- public function getRequiredOptions()
320- {
321- return ['mandatoryFooOption'];
322- }
263+ public function getRequiredOptions()
264+ {
265+ return ['mandatoryFooOption'];
323266 }
267+ }
324268
325269Then, inside the validator class you can access these options directly via the
326270constraint class passes to the ``validate() `` method::
@@ -343,30 +287,6 @@ the custom options like you pass any other option in built-in constraints:
343287
344288.. configuration-block ::
345289
346- .. code-block :: php-annotations
347-
348- // src/Entity/AcmeEntity.php
349- namespace App\Entity;
350-
351- use App\Validator as AcmeAssert;
352- use Symfony\Component\Validator\Constraints as Assert;
353-
354- class AcmeEntity
355- {
356- // ...
357-
358- /**
359- * @Assert\NotBlank
360- * @AcmeAssert\Foo(
361- * mandatoryFooOption="bar",
362- * optionalBarOption=true
363- * )
364- */
365- protected $name;
366-
367- // ...
368- }
369-
370290 .. code-block :: php-attributes
371291
372292 // src/Entity/AcmeEntity.php
0 commit comments