php 忽略子实体内的Symfony 3验证组

aurhwmvo  于 2023-03-21  发布在  PHP
关注(0)|答案(2)|浏览(90)

我有一个简单的表单,有两个字段,email和name。email在Accounts实体中,name在child实体中:医疗中心

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;

class AccountsMedicalCentersType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email')
            ->add('name', TextType::class, array(
                'mapped' => false
            ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array(
                'validation_groups' => array('emailValidation','Default')
                )
        );
    }
}

我想用验证组验证字段,因为在MedicalCenters实体中,我有一些其他验证,用于其他情况:
帐户实体,验证工作正常:

/**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=100, nullable=true, unique=true)
     * @Assert\NotBlank(groups={"emailValidation"})
     * @Assert\Email(groups={"emailValidation"})
     *
     */
    protected $email;
 /**
     * @var \MedicalCenters
     *
     * @ORM\ManyToOne(targetEntity="MedicalCenters")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="medical_centers_id", referencedColumnName="id")
     * })
     *
     * @Assert\Valid
     *
     */
    private $medicalCenters;

MedicalCenters实体,验证组被忽略,返回的错误是第二个错误(地址非空):

/**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=300, nullable=true)
     * @Assert\NotBlank(message="name not blank", groups={"emailValidation"})
     *
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="text", length=65535, nullable=true)
     * @Assert\NotBlank(message="address not blank")
     */
    private $address;

我不明白为什么从子实体是不验证只有名称字段,这属于组emailValidation?我做错了什么?
在控制器中,我有:

$form = $this->createForm(
                AccountsMedicalCentersType::class,
                $currentAccount,
                array(
                    'validation_groups' => array(
                        'emailValidation',
                        'Default'
                    )
                )
            );

谢谢

6l7fqoea

6l7fqoea1#

添加选项cascade_validation就可以了,但是这个选项已经被弃用了。使用Valid约束来验证子对象。来自Symfony文档:
此约束用于对作为属性嵌入到要验证的对象上的对象启用验证。这允许您验证对象和与之关联的所有子对象。(来源:https://symfony.com/doc/current/reference/constraints/Valid.html

use Symfony\Component\Validator\Constraints\Valid;

class AccountsMedicalCentersType extends AbstractType
{
    ....

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => array('emailValidation','Default')
            'constraints' => array(
                new Valid()
            )
        ));
    }
}
xlpyo6sf

xlpyo6sf2#

用途

/**
     * @Assert\Valid
     */

对于你的对象whta你也想被验证。

相关问题