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

fix: respect complex language strings when using validation #9201

Open
wants to merge 3 commits into
base: develop
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
30 changes: 14 additions & 16 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,26 +919,24 @@ protected function getErrorMessage(
): string {
$param ??= '';

$args = [
'field' => ($label === null || $label === '') ? $field : lang($label),
'param' => (! isset($this->rules[$param]['label'])) ? $param : lang($this->rules[$param]['label']),
'value' => $value ?? '',
];

// Check if custom message has been defined by user
if (isset($this->customErrors[$field][$rule])) {
$message = lang($this->customErrors[$field][$rule]);
} elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
$message = lang($this->customErrors[$originalField][$rule]);
} else {
// Try to grab a localized version of the message...
// lang() will return the rule name back if not found,
// so there will always be a string being returned.
$message = lang('Validation.' . $rule);
return lang($this->customErrors[$field][$rule], $args);
}
if (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
return lang($this->customErrors[$originalField][$rule], $args);
}

$message = str_replace('{field}', ($label === null || $label === '') ? $field : lang($label), $message);
$message = str_replace(
'{param}',
(! isset($this->rules[$param]['label'])) ? $param : lang($this->rules[$param]['label']),
$message
);

return str_replace('{value}', $value ?? '', $message);
// Try to grab a localized version of the message...
// lang() will return the rule name back if not found,
// so there will always be a string being returned.
return lang('Validation.' . $rule, $args);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,30 @@ public function testTranslatedLabelWithCustomErrorMessage(): void
$this->assertSame('The Foo Bar Translated field is very short.', $this->validation->getError('foo'));
}

public function testTranslatedLabelWithCustomErrorMessageAndComplexLanguageString(): void
{
// Lithuanian language used as an example
$rules = [
'foo' => [
'label' => 'Lauko pavadinimas',
'rules' => 'min_length[5]',
'errors' => [
'min_length' => '{param, plural,
=0 {Lauke „{field}" negali būti mažiau nei nulis ženklų}
=1 {Lauke „{field}" negali būti mažiau nei vienas ženklas}
one {Lauke „{field}" negali būti mažiau nei # ženklas}
few {Lauke „{field}" negali būti mažiau nei # ženklai}
other {Lauke „{field}" negali būti mažiau nei # ženklų}
}',
],
],
];

$this->validation->setRules($rules, []);
$this->validation->run(['foo' => 'abc']);
$this->assertSame('Lauke „Lauko pavadinimas" negali būti mažiau nei 5 ženklų', $this->validation->getError('foo'));
}

public function testTranslatedLabelTagReplacement(): void
{
$data = ['Username' => 'Pizza'];
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.5.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Deprecations
Bugs Fixed
**********

- **Validation:** Fixed a bug where complex language strings were not properly handled.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.
Loading