简体   繁体   English

如何在 Symfony EasyAdmin3 中创建数据验证系统

[英]How to create a data validation system in Symfony EasyAdmin3

To put in context there is an editor who can modify his information according to his id and his information must be validated by the admin before persisting in the database .在上下文中,有一个编辑器可以根据他的 id 修改他的信息,并且他的信息必须在保存在数据库之前由管理员验证

I have in mind to create a temporary table to store the information that the editor will modify and the admin can access his information via the interface of EasyAdmin then as soon as the admin validates the information it will persist in the original table.我想创建一个临时表来存储编辑器将修改的信息,管理员可以通过 EasyAdmin 的界面访问他的信息,然后一旦管理员验证信息,它将保留在原始表中。

I did not find in the documentation the way to recover the information of the form of the edit page.我在文档中没有找到恢复编辑页面表单信息的方法。

I join the crud controller that I want to target:我加入了我想要定位的 crud 控制器:

class OperateurCrudController extends AbstractCrudController {类 OperateurCrudController 扩展了 AbstractCrudController {

private $crudUrlGenerator;
private $adminContextProvider;

public function __construct(AdminUrlGenerator $adminUrlGenerator, AdminContextProvider $adminContextProvider)
{
    $this->adminUrlGenerator = $adminUrlGenerator;
    $this->adminContextProvider = $adminContextProvider;

}

public static function getEntityFqcn(): string
{
    return Operateur::class;
}

public function configureFields(string $pageName): iterable
{
        return [
            IdField::new('id')->hideOnForm(),
            TextField::new('name', 'Nom'),
            TextEditorField::new('description', 'Description')->hideOnIndex(),
            TextareaField::new('address', 'Adresse'),
            TextField::new('city', 'Ville')->hideOnIndex(),
            TextField::new('postal_code', 'Code Postal')->hideOnIndex(),
            TextField::new('email', 'Email'),
            TelephoneField::new('phone', 'Téléphone'),
            UrlField::new('website', 'Site Web')->hideOnIndex(),
            TextEditorField::new('opening_hours', 'Horaires d\'Ouverture'),
            NumberField::new('latitude', 'Latitude')->hideOnIndex()->setNumDecimals(15),
            NumberField::new('longitude', 'Longitude')->hideOnIndex()->setNumDecimals(15),
            TextField::new('slug', 'Slug')->hideOnIndex()->setPermission('ROLE_ADMIN'),
            DateTimeField::new('created_at', 'Date Création')->onlyOnIndex(),
            DateTimeField::new('updated_at', 'Date Modification')->onlyOnIndex(),
            AssociationField::new('thematiques', 'Thématiques')

        ];

}

public function configureActions(Actions $actions): Actions
{
    $batchAction = Action::new('approve', 'Approuver', 'fa fa-user-check')
        ->linkToUrl('approveOperators');

    $updateOperator = Action::new('update', 'Enregistrer les modifications', 'fa fa-save')
        ->linkToCrudAction('saveOperator');


    if($this->isGranted('ROLE_ADMIN')){
        return $actions
            ->add(Crud::PAGE_INDEX, $batchAction)
            ->add(Crud:: PAGE_INDEX, Action::DETAIL)
            ->setPermission(Action::DELETE, 'ROLE_ADMIN')
            ->setPermission(Action::NEW, 'ROLE_ADMIN')
            ->setPermission(Action::EDIT, 'ROLE_ADMIN')
            ->setPermission($batchAction, 'ROLE_ADMIN');
    }

    if($this->isGranted('ROLE_EDITOR')){
        return $actions
            ->add(Crud:: PAGE_INDEX, Action::DETAIL)
            ->add(Crud::PAGE_EDIT, $updateOperator)
            ->setPermission(Action::DELETE, 'ROLE_ADMIN')
            ->setPermission(Action::NEW, 'ROLE_ADMIN')
            ->setPermission($updateOperator, 'ROLE_EDITOR')
        ->disable( Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE);
    }


}

public function approveOperators(): Response{

    $this->addFlash('notice', '<span style="color: green"><i class="fa fa-check"></i>Modification effecuté </span>');

    $url = $this->adminUrlGenerator
        ->setAction(Action::INDEX)
        ->generateUrl();


    return $this->redirect($url);


}



public function saveOperator(){
    //$this->addFlash('notice', '<span style="color: green"><i class="fa fa-check"></i>Modification pris en compte ! </span>');

    //Create my own save button in page edit

}


public function configureCrud(Crud $crud): Crud
{
    return $crud
        ->setEntityPermission('ROLE_EDITOR');
}



public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{

    $response = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
    if(!$this->isGranted('ROLE_ADMIN')){
        $response->where('entity.id = :id');
        $response->setParameter('id', $this->getUser()->getOperateur());
    }


    return $response;

}

public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
    parent::updateEntity($entityManager, $entityInstance); // TODO: Change the autogenerated stub
}

Thank you for reading感谢您阅读

you should use BeforeEntityPersistedEvent::class你应该使用 BeforeEntityPersistedEvent::class

It will autotrigger just before persisting an element in database它会在将元素持久化到数据库之前自动触发

class EasyAdminSubscriber extends AbstractController implements EventSubscriberInterface
{

  public function __construct( )
      {

      }

  public static function getSubscribedEvents(){

    return [
      BeforeEntityPersistedEvent::class => ['functionName']
    ];
  }

  public function functionName(BeforeEntityPersistedEvent $event){

    $entity=$event->getEntityInstance();

    $user = $this->getUser();
    $userRole = $user->getRole();
    $check = $this->checkRole($userRole);

    if ($entity instanceof yourEntity && $check== true) {

    
           //Do your magic with changing the value you want in the table you
       }
    }

} }

Sorry to no comment, i dont have enough reputation for it so i answer there.抱歉没有评论,我没有足够的声誉,所以我在那里回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM