Reputation: 14856
I'm trying to validate data in an imported csv file with Symfony2's validation component (as of the 2.1 branch) using an external yml ruleset (not in a Symfony project):
use Symfony\Component\Validator\Validation;
$builder = Validation::createValidatorBuilder();
$builder->addYamlMapping('rules.yml');
$validator = $builder->getValidator();
$row = (object)array('name' => 'foo');
$violations = $validator->validate($row);
This is my yml file:
stdClass:
properties:
name:
- MinLength: 10
Now there is the problem that it does not seem to be possible with the Validator Component to validate objects with dynamic properties (like the stdClass
or any other class with magic getters and setters).
When I run that code I get a message saying:
[Symfony\Component\Validator\Exception\ValidatorException]
Property forename does not exist in class stdClass
This is due to the PropertyMetaclass.php in the highlighted line where the existence is checked by using property_exists()
which obviously does not work as it's checking against the class and not the object.
Does anyone know how I can get the Validator to work with objects having dynamic properties?
Upvotes: 3
Views: 3468
Reputation: 14856
Unfortunately it's currently not possible to validate objects with dynamic properties according to this official statement, so I opened up a feature request.
My workaround was now to write a very simple code generator class that dumps the class definitions to a file on disc which is then used as class file for the data.
Upvotes: 1
Reputation: 1620
object is not a PHP type.
use stdClass in your yaml file instead of object
see http://php.net/manual/en/language.types.object.php
Upvotes: 0