From 8d5de791922052102f5d3886cd32f1b0c29ae81a Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 26 Sep 2023 16:12:23 +0200 Subject: [PATCH] add 'inline_form_validation' option to validate inline forms with custom algo --- src/core/Controller/Admin/Crud/CrudController.php | 10 ++++++++++ src/core/Crud/Field/Field.php | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/core/Controller/Admin/Crud/CrudController.php b/src/core/Controller/Admin/Crud/CrudController.php index 9d28edd..bb9df19 100644 --- a/src/core/Controller/Admin/Crud/CrudController.php +++ b/src/core/Controller/Admin/Crud/CrudController.php @@ -139,6 +139,7 @@ abstract class CrudController extends AdminController $builder = $this->createFormBuilder($entity); $callback = $configuration->getFields($context)[$label]['options']['inline_form'] ?? null; + $validationCallback = $configuration->getFields($context)[$label]['options']['inline_form_validation'] ?? null; if (null === $callback) { throw $this->createNotFoundException(); @@ -169,12 +170,21 @@ abstract class CrudController extends AdminController ); $form->handleRequest($fakeRequest); + + if (null !== $validationCallback) { + call_user_func_array($validationCallback, [$entity, $form, $request]); + } + $session->remove($lastRequestId); } if ($request->isMethod('POST')) { $form->handleRequest($request); + if (null !== $validationCallback) { + call_user_func_array($validationCallback, [$entity, $form, $request]); + } + if ($form->isValid()) { if (null !== $beforeUpdate) { call_user_func_array($beforeUpdate, [$entity, $form, $request]); diff --git a/src/core/Crud/Field/Field.php b/src/core/Crud/Field/Field.php index f978d42..2bffa91 100644 --- a/src/core/Crud/Field/Field.php +++ b/src/core/Crud/Field/Field.php @@ -37,6 +37,7 @@ abstract class Field 'href_attr' => [], 'attr' => [], 'inline_form' => null, + 'inline_form_validation' => null, ]); $resolver->setRequired('view'); @@ -46,6 +47,7 @@ abstract class Field $resolver->setAllowedTypes('attr', 'array'); $resolver->setAllowedTypes('href', ['null', 'string', 'callable']); $resolver->setAllowedTypes('inline_form', ['null', 'callable']); + $resolver->setAllowedTypes('inline_form_validation', ['null', 'callable']); $resolver->setAllowedTypes('href_attr', ['array', 'callable']); $resolver->setAllowedTypes('raw', 'boolean'); $resolver->setAllowedTypes('property_builder', ['null', 'callable']);