From 4f6ea007a9788ad7cf37d70522e3259dfa7831cc Mon Sep 17 00:00:00 2001 From: lfortunier Date: Tue, 30 Aug 2022 11:03:35 +0200 Subject: [PATCH] add trad + retrieve admin validate behavior + fix encode_password uses path --- CHANGELOG.md | 9 ++++++++ config/bundle_prepend_config.yml | 2 +- src/Admin/AbstractAdmin.php | 38 ++++++++++++++++++++++++++++++++ translations/admin.fr.xlf | 4 ++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd65066..c7e7a82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ CHANGELOG for 1.x =================== +## v1.3.2 - (2022-08-30) + +### Added + +- Re add validate method on configureFormOption to ease form validation on the all object + +### Fixed + +- Fixed Namespace sonata admin extension for UserTrait ## v1.3.1 - (2022-08-10) diff --git a/config/bundle_prepend_config.yml b/config/bundle_prepend_config.yml index 3f1c1ea..21d1774 100644 --- a/config/bundle_prepend_config.yml +++ b/config/bundle_prepend_config.yml @@ -13,7 +13,7 @@ sonata_admin: extensions: admin.extension.encode_password: uses: - - Smart\AuthenticationBundle\Entity\User\UserTrait + - Smart\SonataBundle\Entity\User\UserTrait # Global Timezone Config # https://stackoverflow.com/a/26469662 diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 366016c..d2ad291 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -2,7 +2,10 @@ namespace Smart\SonataBundle\Admin; +use Sonata\AdminBundle\Admin\AdminInterface; use Sonata\AdminBundle\Route\RouteCollectionInterface; +use Sonata\Form\Validator\Constraints\InlineConstraint; +use Sonata\Form\Validator\ErrorElement; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Security\Core\User\UserInterface; @@ -95,4 +98,39 @@ public function smartIsGranted($attributes, ?object $object = null): bool { return true; } + + /** + * Since sonata 4 validate method is gone. Custom implementation for retrieve his behavior + * https://github.com/sonata-project/SonataAdminBundle/issues/6232 + */ + protected function configureFormOptions(array &$formOptions): void + { + parent::configureFormOptions($formOptions); + $admin = $this; + $formOptions['constraints'] = [ + new InlineConstraint([ + 'service' => $this, + 'method' => static function (ErrorElement $errorElement, object $object) use ($admin) { + /* @var AdminInterface $admin */ + + // This avoid the main validation to be cascaded to children + // The problem occurs when a model Page has a collection of Page as property + if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) { + return; + } + + $admin->validate($errorElement, $object); + }, + 'serializingWarning' => true, + ]) + ]; + } + + /** + * Override for validate object + */ + protected function validate(ErrorElement $errorElement, object $object): void + { + // do nothing + } } diff --git a/translations/admin.fr.xlf b/translations/admin.fr.xlf index 89ce7b1..25da6f6 100644 --- a/translations/admin.fr.xlf +++ b/translations/admin.fr.xlf @@ -19,6 +19,10 @@ list.action_view Voir + + list.label_id + # +