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.

ZaquPL
  • 789
  • 2
  • 12
  • 28
  • are you using annotations to specify constraints? Make sure you: http://symfony.com/doc/current/book/validation.html#validation-and-forms – mezod Jan 26 '15 at 00:35
  • do you know what actually function of `$form->isValid()`? Maybe you thinking that this function for checking input validation like javascript in view? – Jun Rikson Jan 26 '15 at 01: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 – Piotr Galas Jan 26 '15 at 06:40
  • Updated. I added source files. – ZaquPL Jan 26 '15 at 12:03

1 Answers1

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 }
Community
  • 1
  • 1
Grégory Elhaimer
  • 2,731
  • 1
  • 16
  • 21