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] Fix localized error messages on forms when previous URL is incorrect #11194

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions src/Forms/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function create()
}

$knownParams = array_merge(static::HANDLE_PARAM, [
'redirect', 'error_redirect', 'allow_request_redirect', 'csrf', 'files', 'js',
'redirect', 'error_redirect', 'allow_request_redirect', 'csrf', 'files', 'js', 'original_url',
]);

$action = $this->params->get('action', $form->actionUrl());
Expand All @@ -104,7 +104,9 @@ public function create()
}
$attrs = $this->runHooks('attrs', ['attrs' => $attrs, 'data' => $data])['attrs'];

$params = [];
$params = [
'original_url' => request()->url(),
];

if ($redirect = $this->getRedirectUrl()) {
$params['redirect'] = $this->parseRedirect($redirect);
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/FrontendFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function validateResolved()
// If this was submitted from a front-end form, we want to use the appropriate language
// for the translation messages. If there's no previous url, it was likely submitted
// directly in a headless format. In that case, we'll just use the default lang.
$site = ($previousUrl = session()->previousUrl()) ? Site::findByUrl($previousUrl) : null;
$site = ($previousUrl = $this->input('_original_url')) ? Site::findByUrl($previousUrl) : null;

return $this->withLocale($site?->lang(), fn () => parent::validateResolved());
}
Expand Down
59 changes: 59 additions & 0 deletions tests/Tags/Form/FormCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Form;
use Statamic\Facades\Site;
use Statamic\Statamic;

class FormCreateTest extends FormTestCase
Expand Down Expand Up @@ -53,6 +55,16 @@ public function it_renders_form_with_redirects_to_anchor()
$this->assertStringContainsString('<input type="hidden" name="_error_redirect" value="http://localhost#form" />', $output);
}

#[Test]
public function it_renders_original_url()
{
Request::swap(Request::create('http://localhost/contact'));

$output = $this->tag('{{ form:contact }}');

$this->assertStringContainsString('<input type="hidden" name="_original_url" value="http://localhost/contact" />', $output);
}

#[Test]
public function it_dynamically_renders_fields_array()
{
Expand Down Expand Up @@ -804,6 +816,53 @@ public function it_can_render_an_inline_error_when_multiple_rules_fail()
$this->assertEquals($expectedInline, $inlineErrors[1]);
}

#[Test]
public function it_uses_original_url_to_localize_error_messages()
{
trans()->addLines([
'validation.required' => 'Das :attribute Feld ist erforderlich.',
], 'de');

Site::setSites([
'default' => [
'name' => 'English',
'url' => '/',
'locale' => 'en_US',
],
'german' => [
'name' => 'German',
'url' => '/de/',
'locale' => 'de_DE',
],
]);

$this->assertEmpty(Form::find('contact')->submissions());

$this
->post('/!/forms/contact', [
'_original_url' => 'http://localhost/de/contact',
'email' => '[email protected]',
'message' => '',
])
->assertSessionHasErrors(['message'], null, 'form.contact')
->assertLocation('/');

$this->assertEmpty(Form::find('contact')->submissions());

$output = $this->tag(<<<'EOT'
{{ form:contact }}
{{ errors }}
<p class="error">{{ value }}</p>
{{ /errors }}
<p class="inline-error">{{ error:name }}</p>
{{ /form:contact }}
EOT
);

$this->assertStringContainsString('<p class="error">Das Message Feld ist erforderlich.</p>', $output);
$this->assertStringNotContainsString('<p class="error">The Message field is required.</p>', $output);
}

#[Test]
public function it_fetches_form_data()
{
Expand Down