From 357db304324bfc364baba60ea142390ce3526973 Mon Sep 17 00:00:00 2001 From: David ALLIX <517753+webda2l@users.noreply.github.com> Date: Mon, 5 Jan 2026 15:22:44 +0000 Subject: [PATCH 1/2] Fix #402 --- src/Form/Type/TranslationsFormsType.php | 2 +- src/Form/Type/TranslationsType.php | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Form/Type/TranslationsFormsType.php b/src/Form/Type/TranslationsFormsType.php index ee48caf..4347274 100644 --- a/src/Form/Type/TranslationsFormsType.php +++ b/src/Form/Type/TranslationsFormsType.php @@ -27,7 +27,7 @@ * locale_labels: array|null, * theming_granularity: string, * form_options: array, - * form_type: string, + * form_type: class-string<\Symfony\Component\Form\FormTypeInterface>, * ... * } * diff --git a/src/Form/Type/TranslationsType.php b/src/Form/Type/TranslationsType.php index 9f9b90d..716f402 100644 --- a/src/Form/Type/TranslationsType.php +++ b/src/Form/Type/TranslationsType.php @@ -156,12 +156,14 @@ private function buildKnp(FormBuilderInterface $builder, array $options): void ])->all(); foreach ($options['enabled_locales'] as $locale) { + $required = \in_array($locale, $options['required_locales'], true); + $localeFormBuilder = $builder->create($locale, FormType::class, [ 'data_class' => $options['translation_class'], - 'setter' => static fn (...$args) => self::knpLocaleSetter($locale, ...$args), // @phpstan-ignore argument.unpackNonIterable, argument.type + 'setter' => static fn (...$args) => self::knpLocaleSetter($locale, $required, ...$args), // @phpstan-ignore argument.unpackNonIterable, argument.type // LocaleExtension options process 'label' => $options['locale_labels'][$locale] ?? null, - 'required' => \in_array($locale, $options['required_locales'], true), + 'required' => $required, 'block_name' => ('field' === $options['theming_granularity']) ? 'locale' : null, ]); @@ -234,13 +236,13 @@ private function buildGedmo(FormBuilderInterface $builder, array $options): void * @param Collection $translationColl * @param ?\Stub\KnpTranslation $translation */ - private static function knpLocaleSetter(string $locale, Collection $translationColl, ?object $translation): void + private static function knpLocaleSetter(string $locale, bool $required, Collection $translationColl, ?object $translation): void { if (null === $translation) { return; } - if ($translation->isEmpty()) { + if ($translation->isEmpty() && !$required) { $translationColl->removeElement($translation); return; From 043886f44c0bf72407bf2c8757d55bacc811f864 Mon Sep 17 00:00:00 2001 From: David ALLIX <517753+webda2l@users.noreply.github.com> Date: Mon, 5 Jan 2026 15:33:10 +0000 Subject: [PATCH 2/2] Fix --- tests/Form/Type/GedmoTranslationsTypeTest.php | 4 ++++ tests/Form/Type/KnpTranslationsTypeTest.php | 4 ++++ tests/Form/Type/TranslationsFormsTypeTest.php | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/tests/Form/Type/GedmoTranslationsTypeTest.php b/tests/Form/Type/GedmoTranslationsTypeTest.php index 8b0bc59..9925e2d 100644 --- a/tests/Form/Type/GedmoTranslationsTypeTest.php +++ b/tests/Form/Type/GedmoTranslationsTypeTest.php @@ -12,7 +12,9 @@ namespace A2lix\TranslationFormBundle\Tests\Form\Type; use A2lix\AutoFormBundle\Form\Type\AutoType; +use A2lix\TranslationFormBundle\Form\Extension\LocaleExtension; use A2lix\TranslationFormBundle\Form\Type\TranslationsType; +use A2lix\TranslationFormBundle\LocaleProvider\SimpleLocaleProvider; use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\Product; use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\ProductTranslation; use A2lix\TranslationFormBundle\Tests\Form\TypeTestCase; @@ -27,6 +29,8 @@ * @internal */ #[CoversClass(TranslationsType::class)] +#[CoversClass(LocaleExtension::class)] +#[CoversClass(SimpleLocaleProvider::class)] #[AllowMockObjectsWithoutExpectations] // https://github.com/symfony/symfony/issues/62669 final class GedmoTranslationsTypeTest extends TypeTestCase { diff --git a/tests/Form/Type/KnpTranslationsTypeTest.php b/tests/Form/Type/KnpTranslationsTypeTest.php index 608548f..292bc5c 100644 --- a/tests/Form/Type/KnpTranslationsTypeTest.php +++ b/tests/Form/Type/KnpTranslationsTypeTest.php @@ -12,7 +12,9 @@ namespace A2lix\TranslationFormBundle\Tests\Form\Type; use A2lix\AutoFormBundle\Form\Type\AutoType; +use A2lix\TranslationFormBundle\Form\Extension\LocaleExtension; use A2lix\TranslationFormBundle\Form\Type\TranslationsType; +use A2lix\TranslationFormBundle\LocaleProvider\SimpleLocaleProvider; use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\Company; use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\CompanyTranslation; use A2lix\TranslationFormBundle\Tests\Form\TypeTestCase; @@ -27,6 +29,8 @@ * @internal */ #[CoversClass(TranslationsType::class)] +#[CoversClass(LocaleExtension::class)] +#[CoversClass(SimpleLocaleProvider::class)] #[AllowMockObjectsWithoutExpectations] // https://github.com/symfony/symfony/issues/62669 final class KnpTranslationsTypeTest extends TypeTestCase { diff --git a/tests/Form/Type/TranslationsFormsTypeTest.php b/tests/Form/Type/TranslationsFormsTypeTest.php index 7040f37..11abb31 100644 --- a/tests/Form/Type/TranslationsFormsTypeTest.php +++ b/tests/Form/Type/TranslationsFormsTypeTest.php @@ -11,7 +11,10 @@ namespace A2lix\TranslationFormBundle\Tests\Form\Type; +use A2lix\TranslationFormBundle\Form\Extension\LocaleExtension; use A2lix\TranslationFormBundle\Form\Type\TranslationsFormsType; +use A2lix\TranslationFormBundle\Form\Type\TranslationsType; +use A2lix\TranslationFormBundle\LocaleProvider\SimpleLocaleProvider; use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\Company; use A2lix\TranslationFormBundle\Tests\Fixtures\Entity\CompanyMediaLocale; use A2lix\TranslationFormBundle\Tests\Fixtures\Form\CompanyMediaType; @@ -27,6 +30,9 @@ * @internal */ #[CoversClass(TranslationsFormsType::class)] +#[CoversClass(TranslationsType::class)] +#[CoversClass(LocaleExtension::class)] +#[CoversClass(SimpleLocaleProvider::class)] #[AllowMockObjectsWithoutExpectations] // https://github.com/symfony/symfony/issues/62669 final class TranslationsFormsTypeTest extends TypeTestCase {