Skip to content

Commit

Permalink
Merge pull request #64 from pelmered/remove-getLabel
Browse files Browse the repository at this point in the history
Remove getLabel in MoneyInput and use attribute in validation rules
  • Loading branch information
pelmered authored Nov 6, 2024
2 parents d7c2ebf + e511a36 commit dfcc7f1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 22 deletions.
15 changes: 0 additions & 15 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,18 +110,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();
}
}
4 changes: 2 additions & 2 deletions src/Forms/Rules/MaxValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}' => Str::of($attribute)->afterLast('.')->snake(' ')->title(),
'{value}' => MoneyFormatter::formatAsDecimal($this->max, $currencyCode, $locale),
]
)
Expand All @@ -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}' => 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}' => ucwords($this->component->getLabel()),
'{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}' => ucwords($this->component->getLabel()),
'{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 dfcc7f1

Please sign in to comment.