【问题标题】:The option "constraints" does not exist选项“约束”不存在
【发布时间】:2015-04-23 13:56:54
【问题描述】:

我正在使用 Symfony 2.6。我正在尝试创建一个没有实体的表单,但出现以下错误:

“约束”选项不存在。已知的选项是:“动作”, “attr”、“auto_initialize”、“block_name”、“by_reference”、“compound”、 “csrf_field_name”、“csrf_message”、“csrf_protection”、“csrf_provider”、 “csrf_token_id”、“csrf_token_manager”、“数据”、“data_class”、 “禁用”、“empty_data”、“error_bubbling”、“inherit_data”、 “意图”、“标签”、“标签属性”、“标签格式”、“映射”、 “最大长度”、“方法”、“模式”、“post_max_size_message”、 “property_path”、“read_only”、“必需”、“translation_domain”、 “修剪”、“虚拟”。

    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(),
                    new Constraints\Email(),
                ],
                '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', 'text', array(
        'constraints' => new NotBlank(),
    ))
    ->add('dueDate', 'date', array(
        'constraints' => array(
            new NotBlank(),
            new Type('\DateTime'),
        )
    ))
    ->getForm();

【问题讨论】:

  • 我觉得你正在尝试做的事情是完全有效的。由于这里没有响铃,我会在buildForm 中设置一个断点并尝试关注Symfony 看看它为什么会失败......
  • 您确定此错误来自您尝试添加到sender 文本输入的constraints 选项,而不是来自代码中的其他位置吗?我也看不出有什么问题。
  • 是的,我明白。我完全按照 Symfony 文档中的方式进行操作,但仍然出现同样的错误。

标签: php forms symfony


【解决方案1】:

“约束”选项是验证器扩展表单的一部分。我是如何解决这个问题的:

$ValidatorExtension = new ValidatorExtension($validatorBuilder->getValidator());

$formRegistry = new FormRegistry([$csrfProvider, new CoreExtension(), $ValidatorExtension], Yii::$symfony->container->get('form.resolved_type_factory'));

【讨论】:

【解决方案2】:

“约束”选项是ValidatorExtension 的一部分,它不是核心表单扩展的一部分。 TypeTestCase 它只加载核心表单扩展。添加以下代码可解决此问题。

protected function getExtensions()
{
    return [new ValidatorExtension(Validation::createValidator())];
}

更多信息https://symfony.com/doc/current/form/unit_testing.html#adding-custom-extensions

【讨论】:

    【解决方案3】:

    这是一个很老的问题,我才发现它,所以问题仍然会时不时出现。

    我确实喜欢文档: https://symfony.com/doc/current/components/form.html

    $validator = Validation::createValidator();
    
        $formFactory = Forms::createFormFactoryBuilder()
            ...
            ->addExtension(new ValidatorExtension($validator))
            ...
            ->getFormFactory();
    

    当然不要忘记使用:

    use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
    

    【讨论】:

      猜你喜欢
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      相关资源
      最近更新 更多