Symfony2+Doctrine - 验证实体的一对多集合 [英] Symfony2+Doctrine - Validating one-to-many collection of entities

查看:18
本文介绍了Symfony2+Doctrine - 验证实体的一对多集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a form to create a new entity. That entity has a collection of other entities that are also entered in that form.

I want to use the validation options of the entity in the collection to validate those entities but it does not work. The validation rules of the "main" entity (Person) are checked, but the validation rules of the entities in the addressList collection (Address) are not checked. When I input invalid information in the fields, the submitted form is successfully validated.

In this example, the annotation for street is not used on validation.

class Person 
{
    ...

    /**
     * @ORMOneToMany(targetEntity="Address", mappedBy="owner", cascade={"persist", "detach"})
     */
    protected $addressList;

    ....
}

class Address
{
    ...
    /**
     * @ORMManyToOne(targetEntity="Person", inversedBy="addressList")
     * @ORMJoinColumn(name="person_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $owner;

    /**
     * @ORMColumn(type="string", length=75)
     * @AssertMinLength(
     *     limit=3,
     *     message="Street must have atleast {{ limit }} characters."
     * )
     */
    protected $street;

    ...

}

How can I get the form to validate the supplied Address entities?

解决方案

I had the same problem but was solved with:

/**
 * @ORMOneToMany(
 *  targetEntity="Entity",
 *  mappedBy="mappedEntity",
 *  cascade={"persist" , "remove"}
 * )
 * @AssertValid
 */

这篇关于Symfony2+Doctrine - 验证实体的一对多集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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