Skip to content

Commit bb5f925

Browse files
authored
fix: respect complex language strings when using validation (#9201)
* fix: respect complex language strings when using validation * cs fix * changelog update
1 parent 417cbd2 commit bb5f925

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

system/Validation/Validation.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -918,26 +918,24 @@ protected function getErrorMessage(
918918
): string {
919919
$param ??= '';
920920

921+
$args = [
922+
'field' => ($label === null || $label === '') ? $field : lang($label),
923+
'param' => (! isset($this->rules[$param]['label'])) ? $param : lang($this->rules[$param]['label']),
924+
'value' => $value ?? '',
925+
];
926+
921927
// Check if custom message has been defined by user
922928
if (isset($this->customErrors[$field][$rule])) {
923-
$message = lang($this->customErrors[$field][$rule]);
924-
} elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
925-
$message = lang($this->customErrors[$originalField][$rule]);
926-
} else {
927-
// Try to grab a localized version of the message...
928-
// lang() will return the rule name back if not found,
929-
// so there will always be a string being returned.
930-
$message = lang('Validation.' . $rule);
929+
return lang($this->customErrors[$field][$rule], $args);
930+
}
931+
if (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
932+
return lang($this->customErrors[$originalField][$rule], $args);
931933
}
932934

933-
$message = str_replace('{field}', ($label === null || $label === '') ? $field : lang($label), $message);
934-
$message = str_replace(
935-
'{param}',
936-
(! isset($this->rules[$param]['label'])) ? $param : lang($this->rules[$param]['label']),
937-
$message
938-
);
939-
940-
return str_replace('{value}', $value ?? '', $message);
935+
// Try to grab a localized version of the message...
936+
// lang() will return the rule name back if not found,
937+
// so there will always be a string being returned.
938+
return lang('Validation.' . $rule, $args);
941939
}
942940

943941
/**

tests/system/Validation/ValidationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,30 @@ public function testTranslatedLabelWithCustomErrorMessage(): void
13511351
$this->assertSame('The Foo Bar Translated field is very short.', $this->validation->getError('foo'));
13521352
}
13531353

1354+
public function testTranslatedLabelWithCustomErrorMessageAndComplexLanguageString(): void
1355+
{
1356+
// Lithuanian language used as an example
1357+
$rules = [
1358+
'foo' => [
1359+
'label' => 'Lauko pavadinimas',
1360+
'rules' => 'min_length[5]',
1361+
'errors' => [
1362+
'min_length' => '{param, plural,
1363+
=0 {Lauke „{field}" negali būti mažiau nei nulis ženklų}
1364+
=1 {Lauke „{field}" negali būti mažiau nei vienas ženklas}
1365+
one {Lauke „{field}" negali būti mažiau nei # ženklas}
1366+
few {Lauke „{field}" negali būti mažiau nei # ženklai}
1367+
other {Lauke „{field}" negali būti mažiau nei # ženklų}
1368+
}',
1369+
],
1370+
],
1371+
];
1372+
1373+
$this->validation->setRules($rules, []);
1374+
$this->validation->run(['foo' => 'abc']);
1375+
$this->assertSame('Lauke „Lauko pavadinimas" negali būti mažiau nei 5 ženklų', $this->validation->getError('foo'));
1376+
}
1377+
13541378
public function testTranslatedLabelTagReplacement(): void
13551379
{
13561380
$data = ['Username' => 'Pizza'];

user_guide_src/source/changelogs/v4.5.6.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Bugs Fixed
4141

4242
- **Routing:** Fixed a TypeError in `str_replace()` when `Routing::$translateURIDashes` is set to `true` and a route is defined using a closure.
4343

44+
- **Validation:** Fixed a bug where complex language strings were not properly handled.
45+
4446
See the repo's
4547
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
4648
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)