Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions js/modules/Forms/RendererController.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,19 @@ export class GlpiFormRendererController

} catch (e) {
console.error(e);
glpi_toast_error(
__("Failed to submit form, please contact your administrator.")
);

// Always display the default error message
let errorMessage = __("Failed to submit form, please contact your administrator.");

// Try to extract and concatenate specific error messages from the response
if (e.responseJSON && e.responseJSON.errors && e.responseJSON.errors.length > 0) {
const specificErrors = e.responseJSON.errors
.map(msg => _.escape(msg))
.join("<br>");
errorMessage = `${errorMessage}<br><br><strong>${__("Details:")}</strong><br>${specificErrors}`;
}

glpi_toast_error(errorMessage);
} finally {
this.#enableActions();
submit.removeClass('btn-loading');
Expand Down
36 changes: 28 additions & 8 deletions src/Glpi/Controller/Form/SubmitAnswerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Glpi\Form\Form;
use Glpi\Http\Firewall;
use Glpi\Security\Attribute\SecurityStrategy;
use Psr\Log\LoggerInterface;
use Session;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -57,6 +58,8 @@ final class SubmitAnswerController extends AbstractController
{
use CanCheckAccessPolicies;

public function __construct(private LoggerInterface $logger) {}

#[SecurityStrategy(Firewall::STRATEGY_NO_CHECK)] // Some forms can be accessed anonymously
#[Route(
"/Form/SubmitAnswers",
Expand All @@ -76,16 +79,33 @@ public function __invoke(Request $request): Response
$form = $this->loadSubmittedForm($request);
$this->checkFormAccessPolicies($form, $request);

$answers = $this->saveSubmittedAnswers($form, $request);
$links = $answers->getLinksToCreatedItems();
try {
$answers = $this->saveSubmittedAnswers($form, $request);
$links = $answers->getLinksToCreatedItems();

if ($is_unauthenticated_user) {
AltchaManager::getInstance()->removeChallenge($altcha);
}
if ($is_unauthenticated_user) {
AltchaManager::getInstance()->removeChallenge($altcha);
}

return new JsonResponse([
'links_to_created_items' => $links,
]);
return new JsonResponse([
'links_to_created_items' => $links,
]);
} catch (\Throwable $th) {
$this->logger->error(
sprintf('An error occured during the form `%s` submission.', $form->getName()),
['exception' => $th]
);

$messages = [];
if (isset($_SESSION['MESSAGE_AFTER_REDIRECT'][ERROR])) {
$messages = $_SESSION['MESSAGE_AFTER_REDIRECT'][ERROR];
unset($_SESSION['MESSAGE_AFTER_REDIRECT'][ERROR]);
}

return new JsonResponse([
'errors' => $messages,
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

private function loadSubmittedForm(Request $request): Form
Expand Down