From d609fdc321779037bd8c9d1b11bb460a02212b28 Mon Sep 17 00:00:00 2001 From: michalsn Date: Mon, 23 Sep 2024 14:09:11 +0200 Subject: [PATCH 1/3] fix: respect complex language strings when using validation --- system/Validation/Validation.php | 21 ++++++++----------- tests/system/Validation/ValidationTest.php | 24 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index a4d817d2a9b4..4ccef2c637c2 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -919,26 +919,23 @@ 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]); + return lang($this->customErrors[$field][$rule], $args); } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) { - $message = lang($this->customErrors[$originalField][$rule]); + return lang($this->customErrors[$originalField][$rule], $args); } 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('Validation.' . $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); } /** diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 13991dc45916..dae3e995a11f 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -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']; From 47bbcedfe23778c520a23adf58dc88f31167339a Mon Sep 17 00:00:00 2001 From: michalsn Date: Mon, 23 Sep 2024 14:28:34 +0200 Subject: [PATCH 2/3] cs fix --- system/Validation/Validation.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 4ccef2c637c2..61dc87894076 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -928,14 +928,15 @@ protected function getErrorMessage( // Check if custom message has been defined by user if (isset($this->customErrors[$field][$rule])) { return lang($this->customErrors[$field][$rule], $args); - } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) { + } + if (null !== $originalField && isset($this->customErrors[$originalField][$rule])) { return lang($this->customErrors[$originalField][$rule], $args); - } 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. - return lang('Validation.' . $rule, $args); } + + // 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); } /** From 2bb985c3c17cb11472bb66dde3a61d95e57efc53 Mon Sep 17 00:00:00 2001 From: michalsn Date: Wed, 25 Sep 2024 08:26:02 +0200 Subject: [PATCH 3/3] changelog update --- user_guide_src/source/changelogs/v4.5.6.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.5.6.rst b/user_guide_src/source/changelogs/v4.5.6.rst index 33e3e5a1a0ab..60b0b131bfdd 100644 --- a/user_guide_src/source/changelogs/v4.5.6.rst +++ b/user_guide_src/source/changelogs/v4.5.6.rst @@ -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 `_ for a complete list of bugs fixed.