From 96e8c8222bc665734f589fa7304ec97da04b9c80 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 6 Nov 2024 17:12:36 +0100 Subject: [PATCH 1/3] Remove getLabel in MoneyInput and use attribute in validation rules --- src/Forms/Components/MoneyInput.php | 14 -------------- src/Forms/Rules/MaxValueRule.php | 4 ++-- src/Forms/Rules/MinValueRule.php | 4 ++-- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index f6bfe40..fff2c0d 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -111,18 +111,4 @@ public function maxValue(mixed $value): static return $this; } - - public function getLabel(): string - { - if ($this->label instanceof Htmlable) { - return $this->label->toHtml(); - } - - return $this->evaluate($this->label) - ?? (string) str($this->getName()) - ->afterLast('.') - ->kebab() - ->replace(['-', '_'], ' ') - ->title(); - } } diff --git a/src/Forms/Rules/MaxValueRule.php b/src/Forms/Rules/MaxValueRule.php index d088599..8071c05 100644 --- a/src/Forms/Rules/MaxValueRule.php +++ b/src/Forms/Rules/MaxValueRule.php @@ -30,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}' => Str::of($this->component->getLabel())->title(), + '{attribute}' => $attribute, '{value}' => MoneyFormatter::formatAsDecimal($this->max, $currencyCode, $locale), ] ) @@ -41,7 +41,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void strtr( 'The {attribute} must be a valid numeric value.', [ - '{attribute}' => Str::title($this->component->getLabel()), + '{attribute}' => $attribute, ] ) ); diff --git a/src/Forms/Rules/MinValueRule.php b/src/Forms/Rules/MinValueRule.php index 8ec31c4..830403d 100644 --- a/src/Forms/Rules/MinValueRule.php +++ b/src/Forms/Rules/MinValueRule.php @@ -29,7 +29,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void strtr( 'The {attribute} must be at least {value}.', [ - '{attribute}' => ucwords($this->component->getLabel()), + '{attribute}' => $attribute, '{value}' => MoneyFormatter::formatAsDecimal($this->min, $currencyCode, $locale), ] ) @@ -40,7 +40,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void strtr( 'The {attribute} must be a valid numeric value.', [ - '{attribute}' => ucwords($this->component->getLabel()), + '{attribute}' => $attribute, ] ) ); From 5987528c882fb865d1d5360aac1682e58bf9c726 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 6 Nov 2024 17:15:00 +0100 Subject: [PATCH 2/3] Remove unused imports --- src/Forms/Components/MoneyInput.php | 1 - src/Forms/Rules/MaxValueRule.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index fff2c0d..5a94453 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -4,7 +4,6 @@ use Filament\Forms\Components\TextInput; use Filament\Support\RawJs; -use Illuminate\Contracts\Support\Htmlable; use Pelmered\FilamentMoneyField\Forms\Rules\MaxValueRule; use Pelmered\FilamentMoneyField\Forms\Rules\MinValueRule; use Pelmered\FilamentMoneyField\HasMoneyAttributes; diff --git a/src/Forms/Rules/MaxValueRule.php b/src/Forms/Rules/MaxValueRule.php index 8071c05..6a214e3 100644 --- a/src/Forms/Rules/MaxValueRule.php +++ b/src/Forms/Rules/MaxValueRule.php @@ -4,7 +4,6 @@ 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; From e511a36bcdbbffb4e0bc812f31d2d533ee2c950a Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 6 Nov 2024 17:29:05 +0100 Subject: [PATCH 3/3] 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) {