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 Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=LeadRepository::class)
* @ORM\HasLifecycleCallbacks()
* @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
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
private $name;
/**
* @ORM\Column(type="bigint", unique=true, options={"unsigned"=true})
* @Assert\NotBlank
* @Assert\Length(
* min = 10,
* max = 10
* )
*/
private $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Email
*/
private $email;
And the full LeadCrudController used with EasyAdmin
<?php
namespace App\Controller\Admin;
use App\Entity\Lead;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TelephoneField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
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];
}
}
}
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']])
;
}