Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove getLabel in MoneyInput and use attribute in validation rules #64

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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