在 EasyAdmin 3.x 上使用验证组

Using validation groups on EasyAdmin 3.x

我想在创建新用户时使用 EasyAdmin 3.x 的验证组来验证每个字段。

我有一个用户实体 namephoneemail 字段并在“创建”组中使用@UniqueEntity,这样我就可以在 EasyAdmin 外部创建的表单中更新用户。这工作得很好,但我无法找到如何告诉 EasyAdmin 在我使用 new[=26 时使用默认组 我的创建组=]行动。

这是带有我的约束的简化用户实体

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;

以及与 EasyAdmin 一起使用的完整 LeadCrudController

<?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];
        }
    }
}

我在 EasyAdmin git 问题中找到了解决方案:[EA3] add validation groups #3690

我必须将 setFormOptions 方法添加到 crud 配置

// LeadCrudController
public function configureCrud(Crud $crud): Crud
{
    return $crud
        ...
        ->setFormOptions(['validation_groups' => ['Default', 'creation']], ['validation_groups' => ['Default', 'creation']])
    ;
}