Skip to content

Commit

Permalink
Fix form validation when locale is required (knp behaviors) (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
grizzlylab authored Jan 26, 2022
1 parent 5da291b commit ca3bf7b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Form/EventListener/TranslationsFormsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/Form/EventListener/TranslationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ca3bf7b

Please sign in to comment.