我正在使用Symfony 2.6.我试图创建一个没有实体的表单,但得到以下错误:
The option “constraints” does not exist. Known options are: “action”,
“attr”,“auto_initialize”,“block_name”,“by_reference”,“compound”,
“csrf_field_name”,“csrf_message”,“csrf_protection”,“csrf_provider”,
“csrf_token_id”,“csrf_token_manager”,“data”,“data_class”,
“disabled”,“empty_data”,“error_bubbling”,“inherit_data”,
“intention”,“label”,“label_attr”,“label_format”,“mapped”,
“max_length”,“method”,“pattern”,“post_max_size_message”,
“property_path”,“read_only”,“required”,“translation_domain”,
“trim”,“virtual”.
class MessageType extends AbstractType { public function buildForm(FormBuilderInterface $builder,array $options) { $builder ->add('sender','text',[ 'constraints' => [ new Constraints\NotBlank(),],]) ->add('recipient','email') ->add('message','textarea'); } public function getName() { return 'message'; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $collectionConstraint = new Constraints\Collection(array( 'fields' => [ 'sender' => [ new Constraints\NotBlank(),new Constraints\Email(),'recipient' => [ new Constraints\NotBlank(),'message' => [ new Constraints\NotBlank(),)); $resolver->setDefaults([ 'validation_constraints' => $collectionConstraint,]); } }
仅使用setDefaultOptions不会显示错误,但它不起作用,不会验证字段.
可以看出,无论如何都试过了.我也尝试使用组件表单发件箱的文档,但得到相同的错误.
http://symfony.com/doc/current/components/form/introduction.html#form-validation
编辑
我也试过这种方式并得到同样的错误.
$form = $formFactory->createBuilder() ->add('task',array( 'constraints' => new NotBlank(),)) ->add('dueDate','date',array( 'constraints' => array( new NotBlank(),new Type('\DateTime'),) )) ->getForm();
‘constraints’选项是Validator扩展表单的一部分.我是怎么解决这个问题的:
$ValidatorExtension = new ValidatorExtension($validatorBuilder->getValidator()); $formRegistry = new FormRegistry([$csrfProvider,new CoreExtension(),$ValidatorExtension],Yii::$symfony->container->get('form.resolved_type_factory'));