0

I create and render my form using form builder but it seems that validation is disabled or work not properly. I keep my validation rules in validation.yml file. Method $form->isValid() always return true, and $form->getErrorsAsString() doesn't show any errors (only [FieldName1]: No errors, [FieldName2]: No errors ... etc).

I create form in this way:

$form = $this->createForm( new CategoryType(), new Category() );

Then I send it to a twig.

What could be the reason? How do I enable validation?

----- UPDATE -------------------------

I create a form in this way:

public function indexAction()
{
    return $this->render('MyBundle:MyView:index.html.twig', array(
        "form" => $this->createForm( new CategoryType(), new Category() )->createView()
    ));
}

CategoryType is very simple:

class CategoryType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {

       $builder ->add('name', 'text')
                ->add('categoryId', 'entity', array(
                    'class' => 'MyBundle:Category',
                    'property' => 'name',
                ));
    }

    public function getName() {
        return 'my_form_name';
    }
}

And of course validation.yml:

MyBundle/Form/Type/CategoryType:
    properties:
    - name:
        - NotBlank: ~
        - Length:
            min: 3
            max: 30

MyBundle/Entity/Category:
    properties:
    - name:
        - NotBlank: ~
        - Length:
            min: 3
            max: 30

I do not know exactly which version should I use, but both of them doesn't work.

4
  • are you using annotations to specify constraints? Make sure you: symfony.com/doc/current/book/… Commented Jan 26, 2015 at 0:35
  • do you know what actually function of $form->isValid()? Maybe you thinking that this function for checking input validation like javascript in view? Commented Jan 26, 2015 at 1:15
  • 1
    You should show us where and how you invoke the validators. Also how you process the request? Do you invoke $form->handleRequest($request)? There could be a lot of reason why your validation doesn't work. Please more details Commented Jan 26, 2015 at 6:40
  • Updated. I added source files. Commented Jan 26, 2015 at 12:03

1 Answer 1

1

I think this topic will help you.

You must check that your validator configuration within config.yml is defined as below:

framework:
    validation: { enabled: true, enable_annotations: false }
Sign up to request clarification or add additional context in comments.

2 Comments

Still the same problem.
How do you handle the request ? Since we don't know how you treat the form POST request we can't help you further. In the code you presented us, only the form rendering is shown.

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.