方法" XY" Callback约束的目标不存在

时间:2015-05-07 19:42:49

标签: php forms symfony

我正在使用Symfony 2.6。

那是我的实体User.php:

(...)
    /**
     * @var string
     * @Assert\Callback(methods={"validateFirstName"}, groups={"registration"})
     * @ORM\Column(name="firstName", type="string")
     */
    private $firstname;

    /**
     * @param ExecutionContextInterface $context
     * @Assert\Callback(methods={"validateFirstName"}, groups={"registration"})
     */
    public function validateFirstName(ExecutionContextInterface $context)
    {
        // somehow you have an array of "fake names"
        //$fakeNames = array(/* ... */);
        $firstname = $this->getFirstName();
        // check if the name is actually a fake name
        if ( "" == $firstname) {
            $context->buildViolation('This name sounds totally fake!')
                ->atPath('firstName')
                ->addViolation();
        }
    }
(...)

我得到了错误: 方法" validateFirstName" Callback约束的目标不存在

由于

,我真的不知道为什么
 * @Assert\Callback(methods={"validateFirstName"}

有人可以解释一下,如何用$ firstname连接方法?

1 个答案:

答案 0 :(得分:0)

我目前正在使用2.3,因此我无法直接为您查看此内容,但一眼就看起来您似乎并没有关注注释文档。

查看documentation,我认为您不需要在$ firstname属性上使用@Assert注释。

另外,使用验证器作为User.php中的方法查看文档,您应该在validateFirstName方法上使用@Assert \ Callback()。

E.g。

/**
 * @var string
 * @ORM\Column(name="firstName", type="string")
 */
private $firstname;

/**
 * @param ExecutionContextInterface $context
 * @Assert\Callback
 */
public function validateFirstName(ExecutionContextInterface $context)

根据文档将其简化并查看是否有效,然后在您经过验证后添加验证组。