3

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?

1
  • I updatd the code to show my latest symfony2.1 efforts. Commented Aug 30, 2012 at 15:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Thanks, I already tried that, had no luck with that, too. I updated the code.
I now found the problem, it was a stupid error - the yml file was saved as UTF-8 with BOM which did not work. I saved it without BOM and the code continued. But now I'm facing another problem - I updated the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.