Symfony 3 validation groups inside child entity ignored

By : TeoM
Source: Stackoverflow.com
Question!

I have a simple form, with two fields, email and name. email is in Accounts entity and name in child entity: MedicalCenters

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')
                )
        );
    }
}

I would like to validate the fields with validation groups because in the MedicalCenters entity I have some other validation, for other situations:

Accounts Entity, validation works fine:

/**
     * @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 Entity, the validation groups are ignored and the error returned is the second one (address not blank):

/**
     * @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;

I don't understand why from the child entity is not validate only the name field, which belong to the group emailValidation? What am I doing wrong?

In the controller I have:

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

Thanks

By : TeoM


This video can help you solving your question :)
By: admin