Skip to content

Commit

Permalink
bug #3193 Allow to configure the whole form options (e.g. validation_…
Browse files Browse the repository at this point in the history
…groups) (javiereguiluz)

This PR was squashed before being merged into the 3.0.x-dev branch.

Discussion
----------

Allow to configure the whole form options (e.g. validation_groups)

This fixes #3153.

@Pierstoval I tested this myself in a real app, but it'd be great if you could test it too in yours. Thanks!

Commits
-------

cba8073 Allow to configure the whole form options (e.g. validation_groups)
  • Loading branch information
javiereguiluz committed May 8, 2020
2 parents 9d5c0c2 + cba8073 commit cfa6b2e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 29 deletions.
11 changes: 10 additions & 1 deletion doc/crud.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,19 @@ Templates and Form Options
// default EasyAdmin form theme)
->setFormThemes(['my_theme.html.twig', 'admin.html.twig'])

// options passed as second argument when creating the form with createFormBuilder()
// this sets the options of the entire form (later, you can set the options
// of each form type via the methods of their associated fields)
// pass a single array argument to apply the same options for the new and edit forms
->formOptions([
'validation_groups' => ['Default', 'my_validation_group']
]);

// pass two array arguments to apply different options for the new and edit forms
// (pass an empty array argument if you want to apply no options to some form)
->formOptions(
['validation_groups' => ['my_validation_group']],
['validation_groups' => ['Default'], '...' => '...'],
);
;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Config/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,10 @@ public function setFormThemes(array $themePaths): self
return $this;
}

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

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/Controller/CrudControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function persistEntity(EntityManagerInterface $entityManager, $entityInst

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

public function createEditForm(EntityDto $entityDto): FormInterface;
public function createEditForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface;

public function createNewForm(EntityDto $entityDto): FormInterface;
public function createNewForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface;
}
12 changes: 6 additions & 6 deletions src/Controller/AbstractCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function edit(AdminContext $context)
return new Response((int) $newValue);
}

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

$newForm = $this->createNewForm($context->getEntity());
$newForm = $this->createNewForm($context->getEntity(), $context->getCrud()->getNewFormOptions());
$newForm->handleRequest($context->getRequest());
if ($newForm->isSubmitted() && $newForm->isValid()) {
// TODO:
Expand Down Expand Up @@ -428,14 +428,14 @@ public function deleteEntity(EntityManagerInterface $entityManager, $entityInsta
$entityManager->flush();
}

public function createEditForm(EntityDto $entityDto): FormInterface
public function createEditForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface
{
return $this->get(FormFactory::class)->createEditForm($entityDto);
return $this->get(FormFactory::class)->createEditForm($entityDto, $formOptions);
}

public function createNewForm(EntityDto $entityDto): FormInterface
public function createNewForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface
{
return $this->get(FormFactory::class)->createNewForm($entityDto);
return $this->get(FormFactory::class)->createNewForm($entityDto, $formOptions);
}

/**
Expand Down
25 changes: 19 additions & 6 deletions src/Dto/CrudDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EasyCorp\Bundle\EasyAdminBundle\Dto;

use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
Expand Down Expand Up @@ -38,7 +39,8 @@ final class CrudDto
private $paginatorDto;
private $overriddenTemplates;
private $formThemes;
private $formOptions;
private $newFormOptions;
private $editFormOptions;
private $entityPermission;

public function __construct()
Expand All @@ -53,7 +55,8 @@ public function __construct()
$this->searchFields = [];
$this->showEntityActionsAsDropdown = false;
$this->formThemes = ['@EasyAdmin/crud/form_theme.html.twig'];
$this->formOptions = [];
$this->newFormOptions = KeyValueStore::new();
$this->editFormOptions = KeyValueStore::new();
$this->overriddenTemplates = [];
}

Expand Down Expand Up @@ -252,14 +255,24 @@ public function setFormThemes(array $formThemes): void
$this->formThemes = $formThemes;
}

public function getFormOptions(): ?array
public function getNewFormOptions(): ?KeyValueStore
{
return $this->formOptions;
return $this->newFormOptions;
}

public function setFormOptions(array $formOptions): void
public function getEditFormOptions(): ?KeyValueStore
{
$this->formOptions = $formOptions;
return $this->editFormOptions;
}

public function setNewFormOptions(KeyValueStore $formOptions): void
{
$this->newFormOptions = $formOptions;
}

public function setEditFormOptions(KeyValueStore $formOptions): void
{
$this->editFormOptions = $formOptions;
}

public function getEntityPermission(): ?string
Expand Down
21 changes: 9 additions & 12 deletions src/Factory/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EasyCorp\Bundle\EasyAdminBundle\Factory;

use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudBatchActionFormType;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType;
Expand All @@ -26,24 +27,20 @@ public function __construct(FormFactoryInterface $symfonyFormFactory, CrudUrlGen
$this->crudUrlGenerator = $crudUrlGenerator;
}

public function createEditForm(EntityDto $entityDto): FormInterface
public function createEditForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface
{
$formTypeOptions = [
'entityDto' => $entityDto,
'attr' => ['id' => sprintf('edit-%s-form', $entityDto->getName())],
];
$formOptions->set('entityDto', $entityDto);
$formOptions->set('attr.id', sprintf('edit-%s-form', $entityDto->getName()));

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

public function createNewForm(EntityDto $entityDto): FormInterface
public function createNewForm(EntityDto $entityDto, KeyValueStore $formOptions): FormInterface
{
$formTypeOptions = [
'entityDto' => $entityDto,
'attr' => ['id' => sprintf('new-%s-form', $entityDto->getName())],
];
$formOptions->set('entityDto', $entityDto);
$formOptions->set('attr.id', sprintf('new-%s-form', $entityDto->getName()));

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

public function createBatchActionsForm(): FormInterface
Expand Down

0 comments on commit cfa6b2e

Please sign in to comment.