diff --git a/docs/developers/notifications.md b/docs/developers/notifications.md index 421d8a0d..fb00574f 100644 --- a/docs/developers/notifications.md +++ b/docs/developers/notifications.md @@ -14,6 +14,7 @@ namespace App\Controller; use App\Controller\BaseController; use App\Entity\Ticket; use App\Repository\TicketRepository; +use App\Utils\ConstraintErrorsFormatter; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -44,7 +45,7 @@ class TicketsController extends BaseController $errors = $validator->validate($ticket); if (count($errors) > 0) { - $error = implode(' ', $this->formatErrors($errors)); + $error = implode(' ', ConstraintErrorsFormatter::format($errors)); $this->addFlash('error', $error); return $this->redirectToRoute('ticket', [ 'uid' => $ticket->getUid(), diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php index dd041b90..2723b137 100644 --- a/src/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -9,7 +9,6 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Exception\MethodNotAllowedException; -use Symfony\Component\Validator\ConstraintViolationListInterface; class BaseController extends AbstractController { @@ -27,26 +26,6 @@ protected function renderBadRequest(string $view, array $parameters = [], Respon return $this->render($view, $parameters, $response); } - /** - * @return array - */ - protected function formatErrors(ConstraintViolationListInterface $errors): array - { - $formattedErrors = []; - foreach ($errors as $error) { - $property = $error->getPropertyPath(); - if (isset($formattedErrors[$property])) { - $formattedErrors[$property] = implode( - ' ', - [$formattedErrors[$property], $error->getMessage()], - ); - } else { - $formattedErrors[$property] = $error->getMessage(); - } - } - return $formattedErrors; - } - protected function isPathRedirectable(string $path): bool { $router = $this->container->get('router'); diff --git a/src/Controller/MailboxesController.php b/src/Controller/MailboxesController.php index b023769b..9ac6e3dc 100644 --- a/src/Controller/MailboxesController.php +++ b/src/Controller/MailboxesController.php @@ -13,6 +13,7 @@ use App\Repository\MailboxEmailRepository; use App\Security\Encryptor; use App\Service\Sorter\MailboxSorter; +use App\Utils\ConstraintErrorsFormatter; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Messenger\MessageBusInterface; @@ -135,7 +136,7 @@ public function create( 'username' => $username, 'password' => $password, 'folder' => $folder, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } @@ -236,7 +237,7 @@ public function update( 'encryption' => $encryption, 'username' => $username, 'folder' => $folder, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/Organizations/TicketsController.php b/src/Controller/Organizations/TicketsController.php index 07652a73..d585f533 100644 --- a/src/Controller/Organizations/TicketsController.php +++ b/src/Controller/Organizations/TicketsController.php @@ -18,6 +18,7 @@ use App\SearchEngine\TicketFilter; use App\SearchEngine\TicketSearcher; use App\Service\ActorsLister; +use App\Utils\ConstraintErrorsFormatter; use App\Utils\Time; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface; @@ -296,7 +297,7 @@ public function create( 'priority' => $priority, 'allUsers' => $allUsers, 'techUsers' => $techUsers, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } @@ -321,7 +322,7 @@ public function create( 'priority' => $priority, 'allUsers' => $allUsers, 'techUsers' => $techUsers, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/OrganizationsController.php b/src/Controller/OrganizationsController.php index 8148cf24..c1c3681b 100644 --- a/src/Controller/OrganizationsController.php +++ b/src/Controller/OrganizationsController.php @@ -9,6 +9,7 @@ use App\Entity\Organization; use App\Repository\OrganizationRepository; use App\Service\Sorter\OrganizationSorter; +use App\Utils\ConstraintErrorsFormatter; use App\Utils\Time; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Response; @@ -138,7 +139,7 @@ public function create( 'organizations' => $organizations, 'name' => $name, 'selectedParentUid' => $selectedParentUid, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } @@ -199,7 +200,7 @@ public function update( return $this->renderBadRequest('organizations/edit.html.twig', [ 'organization' => $organization, 'name' => $name, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/PreferencesController.php b/src/Controller/PreferencesController.php index fe69e4b6..deeaf56c 100644 --- a/src/Controller/PreferencesController.php +++ b/src/Controller/PreferencesController.php @@ -7,6 +7,7 @@ namespace App\Controller; use App\Repository\UserRepository; +use App\Utils\ConstraintErrorsFormatter; use App\Utils\Locales; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; @@ -71,7 +72,7 @@ public function update( 'colorScheme' => $colorScheme, 'locale' => $locale, 'availableLanguages' => Locales::getSupportedLanguages(), - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php index 783f1149..64c7389b 100644 --- a/src/Controller/ProfileController.php +++ b/src/Controller/ProfileController.php @@ -7,6 +7,7 @@ namespace App\Controller; use App\Repository\UserRepository; +use App\Utils\ConstraintErrorsFormatter; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; @@ -91,7 +92,7 @@ public function update( return $this->renderBadRequest('profile/edit.html.twig', [ 'name' => $name, 'email' => $email, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/RolesController.php b/src/Controller/RolesController.php index 89e3fcbd..3c2fd842 100644 --- a/src/Controller/RolesController.php +++ b/src/Controller/RolesController.php @@ -9,6 +9,7 @@ use App\Entity\Role; use App\Repository\RoleRepository; use App\Service\Sorter\RoleSorter; +use App\Utils\ConstraintErrorsFormatter; use App\Utils\Time; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; @@ -122,7 +123,7 @@ public function create( 'name' => $name, 'description' => $description, 'permissions' => $permissions, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } @@ -217,7 +218,7 @@ public function update( 'name' => $name, 'description' => $description, 'permissions' => $permissions, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/Tickets/MessagesController.php b/src/Controller/Tickets/MessagesController.php index 94627666..26b24d77 100644 --- a/src/Controller/Tickets/MessagesController.php +++ b/src/Controller/Tickets/MessagesController.php @@ -14,6 +14,7 @@ use App\Repository\OrganizationRepository; use App\Repository\TicketRepository; use App\Service\TicketTimeline; +use App\Utils\ConstraintErrorsFormatter; use App\Utils\Time; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface; @@ -131,7 +132,7 @@ public function create( 'statuses' => $statuses, 'isSolution' => $isSolution, 'isConfidential' => $isConfidential, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } @@ -165,7 +166,7 @@ public function create( 'statuses' => $statuses, 'isSolution' => $isSolution, 'isConfidential' => $isConfidential, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/Tickets/PriorityController.php b/src/Controller/Tickets/PriorityController.php index 1d8ad450..bb7e491c 100644 --- a/src/Controller/Tickets/PriorityController.php +++ b/src/Controller/Tickets/PriorityController.php @@ -9,6 +9,7 @@ use App\Controller\BaseController; use App\Entity\Ticket; use App\Repository\TicketRepository; +use App\Utils\ConstraintErrorsFormatter; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -89,7 +90,7 @@ public function update( 'urgency' => $urgency, 'impact' => $impact, 'priority' => $priority, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/Tickets/StatusController.php b/src/Controller/Tickets/StatusController.php index 991b9868..c04eab2a 100644 --- a/src/Controller/Tickets/StatusController.php +++ b/src/Controller/Tickets/StatusController.php @@ -9,6 +9,7 @@ use App\Controller\BaseController; use App\Entity\Ticket; use App\Repository\TicketRepository; +use App\Utils\ConstraintErrorsFormatter; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -82,7 +83,7 @@ public function update( 'ticket' => $ticket, 'status' => $status, 'statuses' => $statuses, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/Tickets/TitleController.php b/src/Controller/Tickets/TitleController.php index 30cf0744..2173149a 100644 --- a/src/Controller/Tickets/TitleController.php +++ b/src/Controller/Tickets/TitleController.php @@ -9,6 +9,7 @@ use App\Controller\BaseController; use App\Entity\Ticket; use App\Repository\TicketRepository; +use App\Utils\ConstraintErrorsFormatter; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -78,7 +79,7 @@ public function update( return $this->renderBadRequest('tickets/title/edit.html.twig', [ 'ticket' => $ticket, 'title' => $title, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Controller/Tickets/TypeController.php b/src/Controller/Tickets/TypeController.php index 9982d652..4eeec41c 100644 --- a/src/Controller/Tickets/TypeController.php +++ b/src/Controller/Tickets/TypeController.php @@ -9,6 +9,7 @@ use App\Controller\BaseController; use App\Entity\Ticket; use App\Repository\TicketRepository; +use App\Utils\ConstraintErrorsFormatter; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -54,7 +55,7 @@ public function update( $errors = $validator->validate($ticket); if (count($errors) > 0) { - $error = implode(' ', $this->formatErrors($errors)); + $error = implode(' ', ConstraintErrorsFormatter::format($errors)); $this->addFlash('error', $error); return $this->redirectToRoute('ticket', [ 'uid' => $ticket->getUid(), diff --git a/src/Controller/UsersController.php b/src/Controller/UsersController.php index da16c686..083148f2 100644 --- a/src/Controller/UsersController.php +++ b/src/Controller/UsersController.php @@ -11,6 +11,7 @@ use App\Repository\UserRepository; use App\Service\Sorter\OrganizationSorter; use App\Service\Sorter\UserSorter; +use App\Utils\ConstraintErrorsFormatter; use App\Utils\Time; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; @@ -116,7 +117,7 @@ public function create( 'email' => $email, 'name' => $name, 'organizationUid' => $organizationUid, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } @@ -210,7 +211,7 @@ public function update( 'email' => $email, 'name' => $name, 'organizationUid' => $organizationUid, - 'errors' => $this->formatErrors($errors), + 'errors' => ConstraintErrorsFormatter::format($errors), ]); } diff --git a/src/Utils/ConstraintErrorsFormatter.php b/src/Utils/ConstraintErrorsFormatter.php new file mode 100644 index 00000000..e654d74b --- /dev/null +++ b/src/Utils/ConstraintErrorsFormatter.php @@ -0,0 +1,38 @@ + + */ + public static function format(ConstraintViolationListInterface $errors): array + { + $formattedErrors = []; + foreach ($errors as $error) { + $property = $error->getPropertyPath(); + if (isset($formattedErrors[$property])) { + $formattedErrors[$property] = implode( + ' ', + [$formattedErrors[$property], $error->getMessage()], + ); + } else { + $formattedErrors[$property] = $error->getMessage(); + } + } + return $formattedErrors; + } +}