I want to use validation groups with EasyAdmin 3.x when creating a new User to validate every field.
I have a User entity with name, phone, and email fields and use @UniqueEntity in a “creation” group, so I can update the User in a form I created outside from EasyAdmin. That works just fine, but I can’t manage to find how to tell EasyAdmin to use the Default group and my creation group when I am using the new action.
Here is the simplified User entity with the constraints I have
use DoctrineORMMapping as ORM; use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity; use SymfonyComponentValidatorConstraints as Assert; /** * @ORMEntity(repositoryClass=LeadRepository::class) * @ORMHasLifecycleCallbacks() * @UniqueEntity( * fields="cellphone", * message="Ya existe un usuario con este número telefónico", * groups={"creation"} * ) * @UniqueEntity( * fields="email", * message="Ya existe un usuario con este email", * groups={"creation"} * ) */ class Lead { /** * @ORMId() * @ORMGeneratedValue() * @ORMColumn(type="integer") */ private $id; /** * @ORMColumn(type="string", length=255) * @AssertNotBlank */ private $name; /** * @ORMColumn(type="bigint", unique=true, options={"unsigned"=true}) * @AssertNotBlank * @AssertLength( * min = 10, * max = 10 * ) */ private $phone; /** * @ORMColumn(type="string", length=255, nullable=true) * @AssertEmail */ private $email;
And the full LeadCrudController used with EasyAdmin
<?php namespace AppControllerAdmin; use AppEntityLead; use EasyCorpBundleEasyAdminBundleConfigAction; use EasyCorpBundleEasyAdminBundleConfigActions; use EasyCorpBundleEasyAdminBundleConfigCrud; use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController; use EasyCorpBundleEasyAdminBundleFieldAssociationField; use EasyCorpBundleEasyAdminBundleFieldDateTimeField; use EasyCorpBundleEasyAdminBundleFieldEmailField; use EasyCorpBundleEasyAdminBundleFieldIntegerField; use EasyCorpBundleEasyAdminBundleFieldTelephoneField; use EasyCorpBundleEasyAdminBundleFieldTextField; class LeadCrudController extends AbstractCrudController { public static function getEntityFqcn(): string { return Lead::class; } public function configureCrud(Crud $crud): Crud { return $crud ->setEntityLabelInSingular('Lead') ->setEntityLabelInPlural('Lead') ->setSearchFields(['id', 'name', 'cellphone', 'email']) ->setDefaultSort(['createdAt' => 'DESC']) ; } public function configureActions(Actions $actions): Actions { return $actions ->disable('delete') ->add(Crud::PAGE_INDEX, Action::DETAIL) ->setPermission(Action::DETAIL, 'ROLE_ADMIN') ->setPermission(Action::INDEX, 'ROLE_ADMIN') ->setPermission(Action::EDIT, 'ROLE_ADMIN') ->setPermission(Action::SAVE_AND_RETURN, 'ROLE_ADMIN') ->setPermission(Action::SAVE_AND_CONTINUE, 'ROLE_ADMIN') ; } public function configureFields(string $pageName): iterable { $name = TextField::new('name'); $cellphone = TelephoneField::new('cellphone'); $email = EmailField::new('email') ->formatValue( function ($value) { return is_null($value) ? '' : $value; } ) ; $createdAt = DateTimeField::new('createdAt'); $pincheck = AssociationField::new('pincheck') ->formatValue( function ($value) { return is_null($value) ? '' : $value; } ) ; $stamps = AssociationField::new('stamps'); $id = IntegerField::new('id', 'ID'); if (Crud::PAGE_INDEX === $pageName) { return [$name, $cellphone, $email, $createdAt]; } if (Crud::PAGE_DETAIL === $pageName) { return [$id, $name, $cellphone, $email, $createdAt, $pincheck, $stamps]; } if (Crud::PAGE_NEW === $pageName || Crud::PAGE_EDIT === $pageName) { $name->setLabel('Nombre y apellido'); $cellphone->setLabel('Teléfono (solo números)'); $email->setLabel('Email (opcional)'); } if (Crud::PAGE_NEW === $pageName) { return [$name, $cellphone, $email]; } if (Crud::PAGE_EDIT === $pageName) { return [$name, $cellphone, $email]; } } }
Advertisement
Answer
I found the solution in the EasyAdmin git issues: [EA3] add validation groups #3690
I had to add the setFormOptions method to the crud configuration
// LeadCrudController public function configureCrud(Crud $crud): Crud { return $crud ... ->setFormOptions(['validation_groups' => ['Default', 'creation']], ['validation_groups' => ['Default', 'creation']]) ; }