Skip to content

Commit 6c72ace

Browse files
committed
minor #21882 [Validator] Update constraint serialization docs to use __serialize()… (lacatoire)
This PR was squashed before being merged into the 8.0 branch. Discussion ---------- [Validator] Update constraint serialization docs to use __serialize()… [Validator] Update constraint serialization docs to use __serialize() instead of __sleep() Fixes #21290 Commits ------- 32d857b [Validator] Update constraint serialization docs to use __serialize()…
2 parents 1aced2c + 32d857b commit 6c72ace

1 file changed

Lines changed: 5 additions & 16 deletions

File tree

validation/custom_constraint.rst

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,10 @@ You can use ``#[HasNamedArguments]`` to make some constraint options required::
6565
Constraint with Private Properties
6666
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6767

68-
Constraints are cached for performance reasons. To achieve this, the base
69-
``Constraint`` class uses PHP's :phpfunction:`get_object_vars` function, which
70-
excludes private properties of child classes.
71-
72-
If your constraint defines private properties, you must explicitly include them
73-
in the ``__sleep()`` method to ensure they are serialized correctly::
68+
Constraints are cached for performance reasons. The base ``Constraint`` class
69+
implements `__serialize()`_ which automatically handles all properties,
70+
including private ones defined in child classes. You can use private properties
71+
in your constraints without any extra configuration::
7472

7573
// src/Validator/ContainsAlphanumeric.php
7674
namespace App\Validator;
@@ -91,16 +89,6 @@ in the ``__sleep()`` method to ensure they are serialized correctly::
9189
) {
9290
parent::__construct(null, $groups, $payload);
9391
}
94-
95-
public function __sleep(): array
96-
{
97-
return array_merge(
98-
parent::__sleep(),
99-
[
100-
'mode'
101-
]
102-
);
103-
}
10492
}
10593

10694
Creating the Validator itself
@@ -665,3 +653,4 @@ class to check precisely which of the constraints failed to pass::
665653
}
666654
}
667655

656+
.. _`__serialize()`: https://www.php.net/manual/en/language.oop5.magic.php#object.serialize

0 commit comments

Comments
 (0)