From e511a36bcdbbffb4e0bc812f31d2d533ee2c950a Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 6 Nov 2024 17:29:05 +0100 Subject: [PATCH] Fix formatting of attribute string in validation error messages --- src/Forms/Rules/MaxValueRule.php | 5 +++-- src/Forms/Rules/MinValueRule.php | 5 +++-- tests/ValidationRulesTest.php | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Forms/Rules/MaxValueRule.php b/src/Forms/Rules/MaxValueRule.php index 6a214e3..35e37c3 100644 --- a/src/Forms/Rules/MaxValueRule.php +++ b/src/Forms/Rules/MaxValueRule.php @@ -4,6 +4,7 @@ use Closure; use Illuminate\Contracts\Validation\ValidationRule; +use Illuminate\Support\Str; use Money\Exception\ParserException; use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput; use Pelmered\FilamentMoneyField\MoneyFormatter; @@ -29,7 +30,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void strtr( 'The {attribute} must be less than or equal to {value}.', [ - '{attribute}' => $attribute, + '{attribute}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(), '{value}' => MoneyFormatter::formatAsDecimal($this->max, $currencyCode, $locale), ] ) @@ -40,7 +41,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void strtr( 'The {attribute} must be a valid numeric value.', [ - '{attribute}' => $attribute, + '{attribute}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(), ] ) ); diff --git a/src/Forms/Rules/MinValueRule.php b/src/Forms/Rules/MinValueRule.php index 830403d..8d90464 100644 --- a/src/Forms/Rules/MinValueRule.php +++ b/src/Forms/Rules/MinValueRule.php @@ -4,6 +4,7 @@ use Closure; use Illuminate\Contracts\Validation\ValidationRule; +use Illuminate\Support\Str; use Money\Exception\ParserException; use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput; use Pelmered\FilamentMoneyField\MoneyFormatter; @@ -29,7 +30,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void strtr( 'The {attribute} must be at least {value}.', [ - '{attribute}' => $attribute, + '{attribute}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(), '{value}' => MoneyFormatter::formatAsDecimal($this->min, $currencyCode, $locale), ] ) @@ -40,7 +41,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void strtr( 'The {attribute} must be a valid numeric value.', [ - '{attribute}' => $attribute, + '{attribute}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(), ] ) ); diff --git a/tests/ValidationRulesTest.php b/tests/ValidationRulesTest.php index 1c4595a..cb43766 100644 --- a/tests/ValidationRulesTest.php +++ b/tests/ValidationRulesTest.php @@ -17,7 +17,7 @@ public function testMinValueRule(): void }); $rule->validate('amount', 'invalid', function ($message) { - $this->assertEquals('The Total Amount must be a valid numeric value.', $message); + $this->assertEquals('The Amount must be a valid numeric value.', $message); }); } @@ -25,8 +25,8 @@ public function testMaxValueRule(): void { $rule = new MaxValueRule(10000, new MoneyInput('amount')); - $rule->validate('amount', 30000, function ($message) { - $this->assertEquals('The Amount must be less than or equal to 100.00.', $message); + $rule->validate('totalAmount', 30000, function ($message) { + $this->assertEquals('The Total Amount must be less than or equal to 100.00.', $message); }); $rule->validate('amount', 'invalid', function ($message) {