Symfony:不推荐使用cascade_validation选项

时间:2016-06-14 16:38:25

标签: php symfony

我正在尝试将我的项目从symfony2升级到symfony3。我想摆脱这个弃用警告

The "cascade_validation" option is deprecated since version 2.8 and will be removed in 3.0. Use "constraints" with a Valid constraint instead."

以下是我的代码

->add('student_name', 'collection', array(
                'entry_type' => TextType::class,
                'allow_add' => true,
                'cascade_validation' => true,
                'options' => array(
                    'required' => false
                )
            ))

我可以删除此行'cascade_validation' => true而不会造成任何麻烦吗?或者symfony3中的等效代码是什么?

3 个答案:

答案 0 :(得分:14)

在Symfony3中,您必须在父实体中使用@Assert\Valid约束。您可以删除FormType类中的行'cascade_validation' => true

class Author
{
    /**
     * @Assert\Valid
     */
    protected $address;
}

http://symfony.com/doc/current/reference/constraints/Valid.html

答案 1 :(得分:12)

只需替换

'cascade_validation' => true, 

with 

'constraints' => new \Symfony\Component\Validator\Constraints\Valid(),

答案 2 :(得分:0)

错误消息告诉您对实体进行验证,但是从您的代码中我不确定您的实体是什么。 此链接说明了Valid annotation for an Entity

但你应该能够删除" cascade_validation' =>真"从你的形式。我担心你还有其他错误。

尝试一下,看看会发生什么。 您可能还想查看此Validation tutorial以供参考。