FormType验证symfony2

时间:2015-08-12 18:00:47

标签: validation symfony

你能帮助我吗?我找不到解决方案

<?php

/**
* Description of ContactType
*
* @author Thamer
*/

namespace Common\ContactBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;

class ContactType extends AbstractType {

public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder
        ->add('name', 'text', array(
                'constraints' => array(
                    new Length(array('min' => 3)),
                    new NotBlank()
                )
            )
        )
        ->add('email', 'email', array(
                new NotBlank()
            )
        )
        ->add('tel', 'number', array(
                'constraints' => array(
                    new Length(array('min' => 8)),
                    new NotBlank()
                )
            )
        )
        ->add('message', 'textarea', array(
                'constraints' => array(
                    new Length(array('min' => 10)),
                    new NotBlank()
                )
            )
        )
        ->add('recaptcha', 'ewz_recaptcha')
    ;
}

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

}

错误是:

  

选项“0”不存在。定义的选项是:“行动”,   “allow_extra_fields”,“attr”,“auto_initialize”,“block_name”,   “by_reference”,“cascade_validation”,“复合”,“约束”,   “csrf_field_name”,“csrf_message”,“csrf_protection”,“csrf_provider”,   “csrf_token_id”,“csrf_token_manager”,“data”,“data_class”,   “disabled”,“empty_data”,“error_bubbling”,“error_mapping”,   “extra_fields_message”,“inherit_data”,“意图”,   “invalid_message”,“invalid_message_parameters”,“标签”,   “label_attr”,“label_format”,“mapped”,“max_length”,“method”,   “pattern”,“post_max_size_message”,“property_path”,“read_only”,   “required”,“translation_domain”,“trim”,“validation_groups”,   “虚拟”。 500内部服务器错误 - UndefinedOptionsException

1 个答案:

答案 0 :(得分:4)

在你的行中:

->add('email', 'email', array(
            new NotBlank()
        )
    )

您正在传递new NotBlank(),但它应该在约束选项中:

->add('email', 'email', array(
         'constraints' => array(
            new NotBlank()
          )
       )
    )