Skip to content

Commit a18ec37

Browse files
committed
Validate label profiles before creating them via the label controller, so that we do not create duplicate entries
This fixes issue #994
1 parent ced1662 commit a18ec37

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/Controller/LabelController.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@
5858
use Symfony\Component\HttpFoundation\Request;
5959
use Symfony\Component\HttpFoundation\Response;
6060
use Symfony\Component\Routing\Attribute\Route;
61+
use Symfony\Component\Validator\Validator\ValidatorInterface;
6162
use Symfony\Contracts\Translation\TranslatorInterface;
6263

6364
#[Route(path: '/label')]
6465
class LabelController extends AbstractController
6566
{
66-
public function __construct(protected LabelGenerator $labelGenerator, protected EntityManagerInterface $em, protected ElementTypeNameGenerator $elementTypeNameGenerator, protected RangeParser $rangeParser, protected TranslatorInterface $translator)
67+
public function __construct(protected LabelGenerator $labelGenerator, protected EntityManagerInterface $em, protected ElementTypeNameGenerator $elementTypeNameGenerator, protected RangeParser $rangeParser, protected TranslatorInterface $translator,
68+
private readonly ValidatorInterface $validator
69+
)
6770
{
6871
}
6972

@@ -120,15 +123,25 @@ public function generator(Request $request, ?LabelProfile $profile = null): Resp
120123
goto render;
121124
}
122125

123-
$profile = new LabelProfile();
124-
$profile->setName($form->get('save_profile_name')->getData());
125-
$profile->setOptions($form_options);
126-
$this->em->persist($profile);
126+
$new_profile = new LabelProfile();
127+
$new_profile->setName($form->get('save_profile_name')->getData());
128+
$new_profile->setOptions($form_options);
129+
130+
//Validate the profile name
131+
$errors = $this->validator->validate($new_profile);
132+
if (count($errors) > 0) {
133+
foreach ($errors as $error) {
134+
$form->get('save_profile_name')->addError(new FormError($error->getMessage()));
135+
}
136+
goto render;
137+
}
138+
139+
$this->em->persist($new_profile);
127140
$this->em->flush();
128141
$this->addFlash('success', 'label_generator.profile_saved');
129142

130143
return $this->redirectToRoute('label_dialog_profile', [
131-
'profile' => $profile->getID(),
144+
'profile' => $new_profile->getID(),
132145
'target_id' => (string) $form->get('target_id')->getData()
133146
]);
134147
}

0 commit comments

Comments
 (0)