5

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

1
  • you should provide a example of how you are using the form, i.e where the isValid() call is made. Commented Mar 27, 2016 at 10:40

1 Answer 1

4

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(...);
Sign up to request clarification or add additional context in comments.

Comments

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.