This Question have no Text answers yet!

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



Entity primary key inside symfony collection


Define form as service in symfony 3.0


Symfony Form passing variable options from Collection to FormType


Nullable custom form entity with Symfony


optional DateType field with symfony forms

Symfony 3: Access a form from a handler

Error in redirection of user after REGISTRATION_SUCCESS event in FOSUserBundle

How to render form fields in Symfony dynamically (Forms generator)

Symfony form with prepopulated data

Remove constraint validation in DateType fields in Symfony

How to validate Symfony form input MoneyType?

Why do we need redirect after sending post request?

Symfony CollectionType regards deletion+creation as a modification of an item

EntityType Dual list Symfony Form




Video about Symfony 3 validation groups inside child entity ignored