4

我有一个非常简单的表格,没有课程。我制作了一些带有约束选项的项目,但表单验证不起作用。
我已经阅读了几个地方(例如这里)我可以添加一个validation_constraint参数,它是一个\Symfony\Component\Validator\Constraints\Collection实例。

当我尝试时,我总是收到一条错误消息:

选项“validation_constraint”不存在。已知的选项是:... blabla

我的表格:

$collectionConstraint = new \Symfony\Component\Validator\Constraints\Collection(
            array(
                'customer' => new \Symfony\Component\Validator\Constraints\NotBlank(),
                'customer_address' => new \Symfony\Component\Validator\Constraints\NotBlank(),
                'customer_address_postal' => new \Symfony\Component\Validator\Constraints\NotBlank(),
                'paymentDeadline' => new \Symfony\Component\Validator\Constraints\Date(),
                'fulfillmentDate' => new \Symfony\Component\Validator\Constraints\Date(),

                'currency' => new \Symfony\Component\Validator\Constraints\Choice(array(
                    'choices' => $currency_entities
                )),

                'paymode' => new \Symfony\Component\Validator\Constraints\Choice(array(
                    'choices' => $paymode_entities
                ))
            )
        );

$form = $this->createFormBuilder(null,array(
            'validation_constraint' => $collectionConstraint
            ))
            ->add('customer','choice',array(
                'choice_list'=> $customer_choices, 
                'multiple' => false,
                'required' => true,
                'empty_value' => '',
                'attr'   =>  array(
                    'class'   => 'chosen large',
                )
            ))
            ->add('customer_address','choice',array(
                'multiple' => false,
                'required' => true,
                'empty_value' => '',
                'attr'   =>  array(
                    'class'   => 'chosen large'
                )
            ))
            ->add('customer_address_postal','choice',array(
                'multiple' => false,
                'required' => true,
                'empty_value' => '',
                'attr'   =>  array(
                    'class'   => 'chosen large'
                )
            ))
            ->add('paymentDeadline','date',array(
                'input' => 'datetime',
                'widget' => 'single_text',
                'required' => true,
                'attr'   =>  array(
                    'class'   => 'date-picker m-ctrl-medium',
                    'addon' => 'icon-calendar',
                )
            ))
            ->add('fulfillmentDate','date',array(
                'input' => 'datetime',
                'widget' => 'single_text',
                'required' => true,
                'attr'   =>  array(
                    'class'   => 'date-picker m-ctrl-medium',
                    'addon' => 'icon-calendar',
                )
            ))
            ->add('currency','choice',array(
                'required' => true,
                'choice_list' => $curreny_choices
            ))
            ->add('paymode','choice',array(
                'required' => true,
                'choice_list' => $paymode_choices

            ))
            ->add('subject','text',array(
                'required' => false,
                'attr' => array(
                    'class' => 'span8'
                )
            ))
            ->add('comment','textarea',array(
                'required' => false,
                'attr' => array(
                    'class' => 'span8',
                    'rows' => 5
                )
            ))
        ;

Symfony 版本是 2.3.3。

可能是什么问题呢?

4

1 回答 1

2

验证必须使用该选项应用于每个字段constraints,而不是表单构建器。
所以你的代码应该如下:

use \Symfony\Component\Validator\Constraints\NotBlank;
use \Symfony\Component\Validator\Constraints\Date;
use \Symfony\Component\Validator\Constraints\Choice;

$form = $this->createFormBuilder(null)
            ->add('customer','choice',array(
                'choice_list'=> $customer_choices, 
                'multiple' => false,
                'required' => true,
                'empty_value' => '',
                'attr'   =>  array(
                    'class'   => 'chosen large',
                ),
                'constraints' => new NotBlank()
            ))
            ->add('customer_address','choice',array(
                'multiple' => false,
                'required' => true,
                'empty_value' => '',
                'attr'   =>  array(
                    'class'   => 'chosen large'
                ),
                'constraints' => new NotBlank()
            ))
            ->add('customer_address_postal','choice',array(
                'multiple' => false,
                'required' => true,
                'empty_value' => '',
                'attr'   =>  array(
                    'class'   => 'chosen large'
                ),
                'constraints' => new NotBlank()
            ))
            ->add('paymentDeadline','date',array(
                'input' => 'datetime',
                'widget' => 'single_text',
                'required' => true,
                'attr'   =>  array(
                    'class'   => 'date-picker m-ctrl-medium',
                    'addon' => 'icon-calendar',
                ),
                'constraints' => new Date()
            ))
            ->add('fulfillmentDate','date',array(
                'input' => 'datetime',
                'widget' => 'single_text',
                'required' => true,
                'attr'   =>  array(
                    'class'   => 'date-picker m-ctrl-medium',
                    'addon' => 'icon-calendar',
                ),
                'constraints' => new Date()
            ))
            ->add('currency','choice',array(
                'required' => true,
                'choice_list' => $curreny_choices,
                'constraints' => new Choice(array(
                    'choices' => $currency_entities
                )),
            ))
            ->add('paymode','choice',array(
                'required' => true,
                'choice_list' => $paymode_choices,
                'constraints' => new Choice(array(
                    'choices' => $paymode_entities
                ))

            ))
            ->add('subject','text',array(
                'required' => false,
                'attr' => array(
                    'class' => 'span8'
                )
            ))
            ->add('comment','textarea',array(
                'required' => false,
                'attr' => array(
                    'class' => 'span8',
                    'rows' => 5
                )
            ))
        ;

注意:我use在顶部添加了一些语句以保持代码清晰。

http://symfony.com/doc/current/book/forms.html#adding-validation

于 2013-08-09T14:25:22.363 回答