表单模型绕过验证

时间:2012-12-24 11:58:17

标签: php validation symfony doctrine-orm

我有一个带有验证限制的用户模型MyProject\SecurityBundle\Entity\User。对于我的注册,我创建了MyProject\SecurityBundle\Form\Model\Registration

namespace MyProject\SecurityBundle\Form\Model;

use MyProject\SecurityBundle\Entity\User;

class Registration
{
    private $user;

    private $termsAccepted;

    public function setUser(User $user)
    {
        $this->user = $user;
        return $this;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function setTermsAccepted($termsAccepted)
    {
        $this->termsAccepted = (boolean)$termsAccepted;
        return $this;
    }

    public function getTermsAccepted()
    {
        return $this->termsAccepted;
    }
}

验证:

MyProject\SecurityBundle\Form\Model\Registration:
    properties:
        user:
            - Type: { type: MyProject\SecurityBundle\Entity\User }
        termsAccepted:
            - NotBlank: ~
    getters:
        termsAccepted:
            - "True": ~

此验证有效,但此用户实体的验证被绕过。有没有办法可以将验证隧道传输到我的注册模型中?

1 个答案:

答案 0 :(得分:0)

如果我在正确的地方进行了搜索,我会在文档中找到http://symfony.com/doc/2.0/reference/constraints/Valid.html这个,并使用解决方案:

MyProject\SecurityBundle\Form\Model\Registration:
    properties:
        user:
            - Type: { type: MyProject\SecurityBundle\Entity\User }
            - Valid: ~