Skip to content

Commit

Permalink
Fix formatting of attribute string in validation error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Nov 6, 2024
1 parent 5987528 commit e511a36
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Forms/Rules/MaxValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
]
)
Expand All @@ -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(),
]
)
);
Expand Down
5 changes: 3 additions & 2 deletions src/Forms/Rules/MinValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
]
)
Expand All @@ -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(),
]
)
);
Expand Down
6 changes: 3 additions & 3 deletions tests/ValidationRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ 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);
});
}

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) {
Expand Down

0 comments on commit e511a36

Please sign in to comment.