TeoM
TeoM

Reputation: 105

Symfony 3 validation groups inside child entity ignored

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

Upvotes: 1

Views: 3134

Answers (2)

Are7even
Are7even

Reputation: 11

Use

    /**
     * @Assert\Valid
     */

for ur object whta u want to be validated too.

Upvotes: 0

Martijn Gastkemper
Martijn Gastkemper

Reputation: 667

Adding the option cascade_validation will do the trick, but that one is deprecated. Use the Valid constraint to validate sub objects. From the Symfony documentation:

This constraint is used to enable validation on objects that are embedded as properties on an object being validated. This allows you to validate an object and all sub-objects associated with it. (source: 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()
            )
        ));
    }
}

Upvotes: 2

Related Questions