SYMFONY 2将验证移至实体,并与相关实体进行验证



我有上诉实体;SITETARIFF,用户实体;关税,现场实体如何将验证转移到实体。验证复杂:与相关实体。我试图写一个回调(在实体上)=>没有结果。

    $appeal = new Appeal();
    $form = $this->createFormBuilder($appeal);
    if ($request->isMethod('POST'))
    {
        $form->bind($request);
        $appeal->setUser($user); //addRelation
        $appeal->appeals_count = $value; //set value to public field
        if ($site_tariff_id = $form->getData()->site_tariff_id)
        {
            $siteTariff = $em->find('', $site_tariff_id);
            $appeal->setSiteTariff($siteTariff); //addRelation
        }
        else
        {
            //addError, break
        }                      

        if ($appeal->getSiteTariff()->getSite()->getUser()->getId() != $user->getId())
        {
            //addError
        }
        if ($appeal->getSiteTariff()->getTariff()->getAppealsNumber() <= $appeal->appeals_count)
        {
            //addError
        }
        if ($form->isValid())
        {
            $em->persist($appeal);
        }
    }

在回调下,我在调用$this->getSiteTariff()后没有得到$ siteTariff对象,Symfony写道:" non-Object "

    public function isSiteTariffValid(ExecutionContext $context)
    {
        var_dump($this->getSiteTariff()->getId());    
    }

您的验证代码应在您的实体内部,而不是在控制器内部。请参阅文档:http://symfony.com/doc/current/referent/constraints/callback.html

相关内容

  • 没有找到相关文章

最新更新