From ca3bf7bf8d5b68d6fe723632443195b63dc6326e Mon Sep 17 00:00:00 2001 From: Jean-Louis Pirson Date: Wed, 26 Jan 2022 10:02:44 +0100 Subject: [PATCH] Fix form validation when locale is required (knp behaviors) (#369) --- src/Form/EventListener/TranslationsFormsListener.php | 5 ++++- src/Form/EventListener/TranslationsListener.php | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Form/EventListener/TranslationsFormsListener.php b/src/Form/EventListener/TranslationsFormsListener.php index aa4e1fd..e2b9a77 100644 --- a/src/Form/EventListener/TranslationsFormsListener.php +++ b/src/Form/EventListener/TranslationsFormsListener.php @@ -43,11 +43,14 @@ public function preSetData(FormEvent $event): void public function submit(FormEvent $event): void { + $form = $event->getForm(); + $formOptions = $form->getConfig()->getOptions(); + $data = $event->getData(); foreach ($data as $locale => $translation) { // Remove useless Translation object - if ((method_exists($translation, 'isEmpty') && $translation->isEmpty()) // Knp + if ((method_exists($translation, 'isEmpty') && $translation->isEmpty() && !\in_array($locale, $formOptions['required_locales'], true)) // Knp || empty($translation) // Default ) { $data->removeElement($translation); diff --git a/src/Form/EventListener/TranslationsListener.php b/src/Form/EventListener/TranslationsListener.php index 2e0f068..5098646 100644 --- a/src/Form/EventListener/TranslationsListener.php +++ b/src/Form/EventListener/TranslationsListener.php @@ -68,11 +68,14 @@ public function preSetData(FormEvent $event): void public function submit(FormEvent $event): void { + $form = $event->getForm(); + $formOptions = $form->getConfig()->getOptions(); + $data = $event->getData(); foreach ($data as $locale => $translation) { // Remove useless Translation object - if ((method_exists($translation, 'isEmpty') && $translation->isEmpty()) // Knp + if ((method_exists($translation, 'isEmpty') && $translation->isEmpty() && !\in_array($locale, $formOptions['required_locales'], true)) // Knp || empty($translation) // Default ) { $data->removeElement($translation);