I have my validation.yml set-up in my bundles Resources/config directory. I enabled the use of the validator in my config.yml:
framework:
validation: { enabled: true, enable_annotations: false }
Resources/config/validation.yml:
MyBundle\AppBundle\Handler\SearchModel:
properties:
search:
- Length:
min: 2
max: 50
minMessage: "Your string must be at least {{ limit }} characters long"
maxMessage: "Your string cannot be longer than {{ limit }} characters long"
In my controller i'm doing:
$validator = $this->get('validator');
$errors = $validator->validate($searchModel);
I'm pretty sure (because I double checked) that property of the model to be validated is string(1) 1 char. (should be minimal 2!)
But a var_dump() of $errors is stating:
object(Symfony\Component\Validator\ConstraintViolationList)#327 (1) {
["violations":"Symfony\Component\Validator\ConstraintViolationList":private]=>
array(0) {
}
}
Thus, no errors are thrown. How can I solve this? I want the validator to work :)
Update
I got it to work (for now) with a work-around. I added a loadValidatorMetaData method to the (SearchModel) class to validate.
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata
->addPropertyConstraint(
'search',
new Assert\Length(
array('min' => 2, 'minMessage' => 'Error message.')
)
);
}
When checking $validator->validate($searchModel); it returns the error, thus validations are working. But not with the .yml file :(