加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 选项“约束”不存在

发布时间:2020-12-13 17:56:31 所属栏目:PHP教程 来源:网络整理
导读:我正在使用Symfony 2.6.我试图创建一个没有实体的表单,但得到以下错误: The option “constraints” does not exist. Known options are: “action”, “attr”,“auto_initialize”,“block_name”,“by_reference”,“compound”, “csrf_field_name”,“
我正在使用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 ConstraintsNotBlank(),],])
            ->add('recipient','email')
            ->add('message','textarea');
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $collectionConstraint = new ConstraintsCollection(array(
            'fields' => [
                'sender' => [
                    new ConstraintsNotBlank(),new ConstraintsEmail(),'recipient' => [
                    new ConstraintsNotBlank(),'message' => [
                    new ConstraintsNotBlank(),));

        $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'));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读