Closed
Description
Origin of this ticket:
Currently, metadata caching fails if a constraint is not serializable. The only core constraint that is susceptible to this problem is the Callback constraint when used with an object method or closure:
// OK
new Callback(array('SomeClass', 'validateMe'));
new Callback('validateMeFunction');
// Not OK
new Callback(array($this, 'validateMe'));
new Callback(function ($value, $context) { ... });
When trying to serialize the last two constraints, an error is raised (#10083) that is a little hard to debug.
Two solutions come to mind:
- Disallow closures/objects methods in Callback and require all constraints to always be serializable. I don't quite like this, since I find passing closures to Callback very useful (e.g. in the "constraints" option of the Form component). After all, caching is not always necessary.
- Add a method
isSerializable()
toConstraint
. This would returntrue
by default andfalse
in edge cases. If caching is turned on,GenericMetadata::addConstraint()
checksisSerializable()
and fails if an unserializable constraint is added.
The second solution adds minor overhead (the additional function call in addConstraint()
), but only if metadata is not cached. I prefer that solution since it maintains the current flexibility, but returns a useful error message when a constraint is not serializable.
What do you think?