0
votes

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 :(

1
Start with the example in the docs: symfony.com/doc/current/book/…. Replace 'pretty sure' with absolutely sure by testing with a new instance of your search model. It's possible the cache needs clearing. And your validation file lives in a Symfony 2 bundle which is loaded in your AppKernel? - Cerad
Thanks for the answer. I retested it, cleared the cache for the right env. Also my validation.yml looks correct. Still nothing. Is it possible that it only works for models in the entity directory? - Faiawuks
It will work for all objects. Double check the fully qualified name. Maybe a typo in the namespace? - Cerad
MyBundle\AppBundle is a bit suspicious. I would have expected MyProject\AppBundle - Cerad
Are you sure MyBundle\AppBundle is the correct namespace? - Cerad

1 Answers

0
votes

I setup a little test case and got no errors. I added a NotBlank constraint and got an error when search was blank.

Acme\DemoBundle\Command\SearchModel:
properties:
    search:
        - Length:
            min: 2
            max: 5
            minMessage: "Your string must be at least {{ limit }} characters long"
            maxMessage: "Your string cannot be longer than {{ limit }} characters long"
        - NotBlank: ~

I then set my search to one char

class SearchModel
{
    public $search = 'a';
}

And it triggered the min length error.

I read through the Length docs: http://symfony.com/doc/current/reference/constraints/Length.html. Sure seems like a zero length string should trigger an error.

In any event, add a NotBlank constraint and you should be good to go.

I checked the code:

namespace Symfony\Component\Validator\Constraints;

class LengthValidator extends ConstraintValidator
{

public function validate($value, Constraint $constraint)
{
    if (null === $value || '' === $value) {
        return;
    }

So I guess this is what is known as an undocumented 'feature'. A string length of zero is not considered to be less than min! Be curious to see if there is a bug report on this. But again, NotBlank will work. You can always adjust the error message so the user will know the limits.