|
58 | 58 | use Symfony\Component\HttpFoundation\Request; |
59 | 59 | use Symfony\Component\HttpFoundation\Response; |
60 | 60 | use Symfony\Component\Routing\Attribute\Route; |
| 61 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
61 | 62 | use Symfony\Contracts\Translation\TranslatorInterface; |
62 | 63 |
|
63 | 64 | #[Route(path: '/label')] |
64 | 65 | class LabelController extends AbstractController |
65 | 66 | { |
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 | + ) |
67 | 70 | { |
68 | 71 | } |
69 | 72 |
|
@@ -120,15 +123,25 @@ public function generator(Request $request, ?LabelProfile $profile = null): Resp |
120 | 123 | goto render; |
121 | 124 | } |
122 | 125 |
|
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); |
127 | 140 | $this->em->flush(); |
128 | 141 | $this->addFlash('success', 'label_generator.profile_saved'); |
129 | 142 |
|
130 | 143 | return $this->redirectToRoute('label_dialog_profile', [ |
131 | | - 'profile' => $profile->getID(), |
| 144 | + 'profile' => $new_profile->getID(), |
132 | 145 | 'target_id' => (string) $form->get('target_id')->getData() |
133 | 146 | ]); |
134 | 147 | } |
|
0 commit comments