Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Refactor frontend formFailure to handle precognitive and fetch exceptions #10376

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
10 changes: 8 additions & 2 deletions src/Http/Controllers/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ private function validateContentType($request, $form)
* The steps for a failed form submission.
*
* @param array $params
* @param array $submission
* @param array $errors
* @param string $form
* @return Response|RedirectResponse
*/
private function formFailure($params, $errors, $form)
{
if (request()->ajax() || request()->wantsJson()) {
$request = request();

if ($request->ajax()) {
return response([
'errors' => (new MessageBag($errors))->all(),
'error' => collect($errors)->map(function ($errors, $field) {
Expand All @@ -116,6 +118,10 @@ private function formFailure($params, $errors, $form)
], 400);
}

if ($request->isPrecognitive() || $request->wantsJson()) {
throw ValidationException::withMessages($errors);
}

$redirect = Arr::get($params, '_error_redirect');

$response = $redirect ? redirect($redirect) : back();
Expand Down
45 changes: 45 additions & 0 deletions tests/Tags/Form/FormCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,4 +932,49 @@ public function it_removes_any_uploaded_assets_when_a_listener_throws_a_validati

Storage::disk('avatars')->assertMissing('avatar.jpg');
}

#[Test]
public function it_renders_exceptions_thrown_during_json_requests_as_standard_laravel_errors()
{
Event::listen(function (\Statamic\Events\FormSubmitted $event) {
throw ValidationException::withMessages(['some' => 'error']);
});

$response = $this
->postJson('/!/forms/contact', [
'name' => 'Name',
'email' => '[email protected]',
'message' => 'This is a message',
]);

$json = $response->json();

$this->assertArrayHasKey('message', $json);
$this->assertArrayHasKey('errors', $json);
$this->assertSame($json['errors'], ['some' => ['error']]);
}

#[Test]
public function it_renders_exceptions_thrown_during_xml_http_requests_in_statamic_error_format()
{
Event::listen(function (\Statamic\Events\FormSubmitted $event) {
throw ValidationException::withMessages(['some' => 'error']);
});

$response = $this
->withHeaders([
'X-Requested-With' => 'XMLHttpRequest',
])
->postJson('/!/forms/contact', [
'name' => 'Name',
'email' => '[email protected]',
'message' => 'This is a message',
]);

$json = $response->json();

$this->assertArrayHasKey('error', $json);
$this->assertArrayHasKey('errors', $json);
$this->assertSame($json['error'], ['some' => 'error']);
}
}
Loading