使用 Symfony2 对实体进行级联验证 [英] Cascade validation on entities with Symfony2

查看:29
本文介绍了使用 Symfony2 对实体进行级联验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用验证器服务对实体执行级联验证.

I'm wondering how I can perform a cascade validation on an entity with the validator service.

使用:

$validator = $this->get('validator');
$errorList = $validator->validate($entity);

不对实体"子实体进行验证.

does not perform a validation of the "entity" children entities.

我很确定它可以完成,因为它可以使用表单验证(使用 'cascade_validation' => true).

I'm pretty sure it can be done since its possible using the form validation (using 'cascade_validation' => true).

推荐答案

我认为(默认情况下)不会对指向对象的属性进行验证.可以使用遍历选项为数组完成validate() 方法设置为 true.

I think that (by default) validation is not processed on attributes that points to objects. It can be done for arrays with the traverse option of the validate() method set to true.

要绕过此限制,您应该考虑使用有效约束.

To bypass this limitation, you should consider using a Valid Constraint.

检查这个很好解释的例子使用Valid Constraint 以及在父对象上处理验证的地方也会检查子对象的验证规则.

Check this well explained example of using the Valid Constraint and where processing validation on the parent object also checks the child's object validation rules.

有效约束还提供了一个遍历选项,可用于检查指向对象数组的属性.

The Valid Constraint also provides a traverse option that you can use to check attributes that point to an array of objects.

这篇关于使用 Symfony2 对实体进行级联验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆