Optional embed form in Symfony 2

前端 未结 5 1930
予麋鹿
予麋鹿 2021-01-16 11:00

I have two entities in my system: Person and Phone as the following code.

class Person
{
    /**
     * @ORM\\Id
     * @ORM\\Colum         


        
5条回答
  •  生来不讨喜
    2021-01-16 11:35

    frankie567's answer was useful to me. For symfony >= 3.0:

    /**
     *
     * @Assert\Callback()
     */
    public function validatePhone(ExecutionContextInterface $context)
    {
        if (/* Fields are not empty */)
        {
            $validator = $context->getValidator();
    
            $validator->inContext($context)
                ->atPath('phone')
                // you can pass custom validation instead of null, like new Assert\Valid()
                ->validate($this->getPhone(), null, array('phone_validation'));   
        }
    }
    

提交回复
热议问题