fliim
fliim

Reputation: 2199

Validation constraints not working on my Symfony3 project

I couldn't make my validation constraints $form->isValid() works. The book name is always valid even if I put a name with length less than 10...

On my AppBundle\Resources\config\validation\book.yml

AppBundle\Entity\Book:
properties:
    bookName:
        - NotBlank: ~
        - Length:
            min: 10

neither from the formBuilder

$builder->add('bookName', TextType::class, array(
            'constraints' => new Length(array('min' => 10))))

Here is my framework.validation config

framework:
    validation: ~

and the default config is

framework:
    validation:
        enabled: true
        enable_annotations: false

Need help please.

Thanks

Upvotes: 5

Views: 2388

Answers (1)

fliim
fliim

Reputation: 2199

I solved my problem.

It was because I used a Form Class and I set a validation_groups like below:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => array('creation'),
        'data_class'        => 'AppBundle\Entity\Book',
    ));
}

And I didn't specified it on the formBuilder options.

$form = $this->createForm(BookType::class, $book);
if ($form->isValid()) {
    ...
}

Solution 1: is to remove the definition of the validation groups from the Form class default option.

Solution 2: is to add the validation group for the validation:

$form = $this->createFormBuilder($book, array(
    'validation_groups' => array('creation'),
))->add(...);

Upvotes: 4

Related Questions