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

Appearance settings

Commit f7b5169

Browse filesBrowse files
committed
Constraints cache with private properties
1 parent a17b5c9 commit f7b5169
Copy full SHA for f7b5169

File tree

Expand file treeCollapse file tree

1 file changed

+40
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+40
-0
lines changed

‎validation/custom_constraint.rst

Copy file name to clipboardExpand all lines: validation/custom_constraint.rst
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
69109
Creating the Validator itself
70110
-----------------------------
71111

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.