File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,46 @@ You can use ``#[HasNamedArguments]`` to make some constraint options required::
6666 }
6767 }
6868
69+ Constraint with private properties
70+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71+
72+ Constraints are cached for efficiency, but private properties are not supported
73+ by default.
74+
75+ So if you're using private properties in your constraint, you must override default
76+ ``__sleep `` method ::
77+
78+ // src/Validator/ContainsAlphanumeric.php
79+ namespace App\Validator;
80+
81+ use Symfony\Component\Validator\Attribute\HasNamedArguments;
82+ use Symfony\Component\Validator\Constraint;
83+
84+ #[\Attribute]
85+ class ContainsAlphanumeric extends Constraint
86+ {
87+ public string $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
88+
89+ #[HasNamedArguments]
90+ public function __construct(
91+ private string $mode,
92+ ?array $groups = null,
93+ mixed $payload = null,
94+ ) {
95+ parent::__construct([], $groups, $payload);
96+ }
97+
98+ public function __sleep(): array
99+ {
100+ return array_merge(
101+ parent::__sleep(),
102+ [
103+ 'mode'
104+ ]
105+ );
106+ }
107+ }
108+
69109Creating the Validator itself
70110-----------------------------
71111
You can’t perform that action at this time.
0 commit comments