Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cba8073

Browse files
committed
Allow to configure the whole form options (e.g. validation_groups)
1 parent 9d5c0c2 commit cba8073

6 files changed

Lines changed: 49 additions & 29 deletions

File tree

doc/crud.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,19 @@ Templates and Form Options
227227
// default EasyAdmin form theme)
228228
->setFormThemes(['my_theme.html.twig', 'admin.html.twig'])
229229

230-
// options passed as second argument when creating the form with createFormBuilder()
230+
// this sets the options of the entire form (later, you can set the options
231+
// of each form type via the methods of their associated fields)
232+
// pass a single array argument to apply the same options for the new and edit forms
231233
->formOptions([
232234
'validation_groups' => ['Default', 'my_validation_group']
233235
]);
236+
237+
// pass two array arguments to apply different options for the new and edit forms
238+
// (pass an empty array argument if you want to apply no options to some form)
239+
->formOptions(
240+
['validation_groups' => ['my_validation_group']],
241+
['validation_groups' => ['Default'], '...' => '...'],
242+
);
234243
;
235244
}
236245

src/Config/Crud.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,10 @@ public function setFormThemes(array $themePaths): self
282282
return $this;
283283
}
284284

285-
public function setFormOptions(array $formOptions): self
285+
public function setFormOptions(array $newFormOptions, array $editFormOptions = null): self
286286
{
287-
$this->dto->setFormOptions($formOptions);
287+
$this->dto->setNewFormOptions(KeyValueStore::new($newFormOptions));
288+
$this->dto->setEditFormOptions(KeyValueStore::new($editFormOptions ?? $newFormOptions));
288289

289290
return $this;
290291
}

src/Contracts/Controller/CrudControllerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function persistEntity(EntityManagerInterface $entityManager, $entityInst
6868

6969
public function deleteEntity(EntityManagerInterface $entityManager, $entityInstance): void;
7070

71-
public function createEditForm(EntityDto $entityDto): FormInterface;
71+
public function createEditForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface;
7272

73-
public function createNewForm(EntityDto $entityDto): FormInterface;
73+
public function createNewForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface;
7474
}

src/Controller/AbstractCrudController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function edit(AdminContext $context)
197197
return new Response((int) $newValue);
198198
}
199199

200-
$editForm = $this->createEditForm($context->getEntity());
200+
$editForm = $this->createEditForm($context->getEntity(), $context->getCrud()->getEditFormOptions());
201201
$editForm->handleRequest($context->getRequest());
202202
if ($editForm->isSubmitted() && $editForm->isValid()) {
203203
// TODO:
@@ -264,7 +264,7 @@ public function new(AdminContext $context)
264264
$this->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
265265
$entityInstance = $context->getEntity()->getInstance();
266266

267-
$newForm = $this->createNewForm($context->getEntity());
267+
$newForm = $this->createNewForm($context->getEntity(), $context->getCrud()->getNewFormOptions());
268268
$newForm->handleRequest($context->getRequest());
269269
if ($newForm->isSubmitted() && $newForm->isValid()) {
270270
// TODO:
@@ -428,14 +428,14 @@ public function deleteEntity(EntityManagerInterface $entityManager, $entityInsta
428428
$entityManager->flush();
429429
}
430430

431-
public function createEditForm(EntityDto $entityDto): FormInterface
431+
public function createEditForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface
432432
{
433-
return $this->get(FormFactory::class)->createEditForm($entityDto);
433+
return $this->get(FormFactory::class)->createEditForm($entityDto, $formOptions);
434434
}
435435

436-
public function createNewForm(EntityDto $entityDto): FormInterface
436+
public function createNewForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface
437437
{
438-
return $this->get(FormFactory::class)->createNewForm($entityDto);
438+
return $this->get(FormFactory::class)->createNewForm($entityDto, $formOptions);
439439
}
440440

441441
/**

src/Dto/CrudDto.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace EasyCorp\Bundle\EasyAdminBundle\Dto;
44

55
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
6+
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
67

78
/**
89
* @author Javier Eguiluz <[email protected]>
@@ -38,7 +39,8 @@ final class CrudDto
3839
private $paginatorDto;
3940
private $overriddenTemplates;
4041
private $formThemes;
41-
private $formOptions;
42+
private $newFormOptions;
43+
private $editFormOptions;
4244
private $entityPermission;
4345

4446
public function __construct()
@@ -53,7 +55,8 @@ public function __construct()
5355
$this->searchFields = [];
5456
$this->showEntityActionsAsDropdown = false;
5557
$this->formThemes = ['@EasyAdmin/crud/form_theme.html.twig'];
56-
$this->formOptions = [];
58+
$this->newFormOptions = KeyValueStore::new();
59+
$this->editFormOptions = KeyValueStore::new();
5760
$this->overriddenTemplates = [];
5861
}
5962

@@ -252,14 +255,24 @@ public function setFormThemes(array $formThemes): void
252255
$this->formThemes = $formThemes;
253256
}
254257

255-
public function getFormOptions(): ?array
258+
public function getNewFormOptions(): ?KeyValueStore
256259
{
257-
return $this->formOptions;
260+
return $this->newFormOptions;
258261
}
259262

260-
public function setFormOptions(array $formOptions): void
263+
public function getEditFormOptions(): ?KeyValueStore
261264
{
262-
$this->formOptions = $formOptions;
265+
return $this->editFormOptions;
266+
}
267+
268+
public function setNewFormOptions(KeyValueStore $formOptions): void
269+
{
270+
$this->newFormOptions = $formOptions;
271+
}
272+
273+
public function setEditFormOptions(KeyValueStore $formOptions): void
274+
{
275+
$this->editFormOptions = $formOptions;
263276
}
264277

265278
public function getEntityPermission(): ?string

src/Factory/FormFactory.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace EasyCorp\Bundle\EasyAdminBundle\Factory;
44

55
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
6+
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
67
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
78
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudBatchActionFormType;
89
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType;
@@ -26,24 +27,20 @@ public function __construct(FormFactoryInterface $symfonyFormFactory, CrudUrlGen
2627
$this->crudUrlGenerator = $crudUrlGenerator;
2728
}
2829

29-
public function createEditForm(EntityDto $entityDto): FormInterface
30+
public function createEditForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface
3031
{
31-
$formTypeOptions = [
32-
'entityDto' => $entityDto,
33-
'attr' => ['id' => sprintf('edit-%s-form', $entityDto->getName())],
34-
];
32+
$formOptions->set('entityDto', $entityDto);
33+
$formOptions->set('attr.id', sprintf('edit-%s-form', $entityDto->getName()));
3534

36-
return $this->symfonyFormFactory->createNamedBuilder($entityDto->getName(), CrudFormType::class, $entityDto->getInstance(), $formTypeOptions)->getForm();
35+
return $this->symfonyFormFactory->createNamedBuilder($entityDto->getName(), CrudFormType::class, $entityDto->getInstance(), $formOptions->all())->getForm();
3736
}
3837

39-
public function createNewForm(EntityDto $entityDto): FormInterface
38+
public function createNewForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface
4039
{
41-
$formTypeOptions = [
42-
'entityDto' => $entityDto,
43-
'attr' => ['id' => sprintf('new-%s-form', $entityDto->getName())],
44-
];
40+
$formOptions->set('entityDto', $entityDto);
41+
$formOptions->set('attr.id', sprintf('new-%s-form', $entityDto->getName()));
4542

46-
return $this->symfonyFormFactory->createNamedBuilder($entityDto->getName(), CrudFormType::class, $entityDto->getInstance(), $formTypeOptions)->getForm();
43+
return $this->symfonyFormFactory->createNamedBuilder($entityDto->getName(), CrudFormType::class, $entityDto->getInstance(), $formOptions->all())->getForm();
4744
}
4845

4946
public function createBatchActionsForm(): FormInterface

0 commit comments

Comments
 (0)