Commit 039e23cb by Julien Jorry

ADMIN : Fix bug sur l'ajout d'une cotisation en emlc => fix success message appear with error

parent 849f4176
......@@ -237,7 +237,7 @@ services:
admin.all.cotisations:
class: App\Admin\CotisationAdmin
arguments: [~, App\Entity\Cotisation, ~]
arguments: [~, App\Entity\Cotisation, 'App\Controller\CRUD\CRUDController']
tags:
- name: sonata.admin
manager_type: orm
......@@ -274,7 +274,7 @@ services:
admin.adherent.cotisations:
class: App\Admin\CotisationAdherentAdmin
arguments: [~, App\Entity\CotisationAdherent, ~]
arguments: [~, App\Entity\CotisationAdherent, 'App\Controller\CRUD\CRUDController']
tags:
- name: sonata.admin
manager_type: orm
......@@ -303,7 +303,7 @@ services:
admin.prestataire.cotisations:
class: App\Admin\CotisationPrestataireAdmin
arguments: [~, App\Entity\CotisationPrestataire, ~]
arguments: [~, App\Entity\CotisationPrestataire, 'App\Controller\CRUD\CRUDController']
tags:
- name: sonata.admin
manager_type: orm
......
......@@ -12,7 +12,6 @@ use App\Utils\OperationUtils;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
......@@ -261,7 +260,6 @@ class CotisationAdmin extends AbstractAdmin
} catch (BalanceInsufficientException $e) {
$em->getConnection()->rollBack();
$this->getConfigurationPool()->getContainer()->get('session')->getFlashBag()->clear();
$this->getConfigurationPool()->getContainer()->get('session')->getFlashBag()->add('error', $e->getMessage());
return null;
}
......
......@@ -4,11 +4,129 @@ namespace App\Controller\CRUD;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\RedirectResponse;
class CRUDController extends Controller
{
/**
* Create action.
*
* @throws AccessDeniedException If access is not granted
*
* @return Response
*/
public function createAction()
{
$request = $this->getRequest();
// the key used to lookup the template
$templateKey = 'edit';
$this->admin->checkAccess('create');
$class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass());
if ($class->isAbstract()) {
return $this->renderWithExtraParams(
'@SonataAdmin/CRUD/select_subclass.html.twig',
[
'base_template' => $this->getBaseTemplate(),
'admin' => $this->admin,
'action' => 'create',
],
null
);
}
$newObject = $this->admin->getNewInstance();
$preResponse = $this->preCreate($request, $newObject);
if (null !== $preResponse) {
return $preResponse;
}
$this->admin->setSubject($newObject);
$form = $this->admin->getForm();
$form->setData($newObject);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$isFormValid = $form->isValid();
// persist if the form was valid and if in preview mode the preview was approved
if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
/** @phpstan-var T $submittedObject */
$submittedObject = $form->getData();
$this->admin->setSubject($submittedObject);
$this->admin->checkAccess('create', $submittedObject);
try {
$newObject = $this->admin->create($submittedObject);
if ($this->isXmlHttpRequest()) {
return $this->handleXmlHttpRequestSuccessResponse($request, $newObject);
}
// ADD FOR KOHINOS => test if object newly created is null to send flash message success !
if (null != $newObject) {
$this->addFlash(
'sonata_flash_success',
$this->trans(
'flash_create_success',
['%name%' => $this->escapeHtml($this->admin->toString($newObject))],
'SonataAdminBundle'
)
);
}
// redirect to edit mode
return $this->redirectTo($newObject);
} catch (ModelManagerException $e) {
$this->handleModelManagerException($e);
$isFormValid = false;
}
}
// show an error message if the form failed validation
if (!$isFormValid) {
if ($this->isXmlHttpRequest() && null !== ($response = $this->handleXmlHttpRequestErrorResponse($request, $form))) {
return $response;
}
$this->addFlash(
'sonata_flash_error',
$this->trans(
'flash_create_error',
['%name%' => $this->escapeHtml($this->admin->toString($newObject))],
'SonataAdminBundle'
)
);
} elseif ($this->isPreviewRequested()) {
// pick the preview template if the form was valid and preview was requested
$templateKey = 'preview';
$this->admin->getShow();
}
}
$formView = $form->createView();
// set the theme for the current Admin Form
$this->setFormTheme($formView, $this->admin->getFormTheme());
// NEXT_MAJOR: Remove this line and use commented line below it instead
$template = $this->admin->getTemplate($templateKey);
// $template = $this->templateRegistry->getTemplate($templateKey);
return $this->renderWithExtraParams($template, [
'action' => 'create',
'form' => $formView,
'object' => $newObject,
'objectId' => null,
], null);
}
// /**
// * @inheritdoc
// */
......@@ -119,4 +237,14 @@ class CRUDController extends Controller
return parent::batchActionDelete($query);
}
/**
* Sets the admin form theme to form view. Used for compatibility between Symfony versions.
*/
private function setFormTheme(FormView $formView, ?array $theme = null): void
{
$twig = $this->get('twig');
$twig->getRuntime(FormRenderer::class)->setTheme($formView, $theme);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment