Skip to content

Commit

Permalink
feat: max & min validation
Browse files Browse the repository at this point in the history
  • Loading branch information
npbreland committed Feb 14, 2024
1 parent 45b80ba commit b6bc755
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,49 @@ protected function prepare(MoneyInput $component): void
{
$formattingRules = MoneyFormatter::getFormattingRules($component->getLocale());
$this->prefix($formattingRules->currencySymbol);

if (config('filament-money-field.use_input_mask')) {
$this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')'));
}
}

public function minValue(mixed $min): static
{
$this->rule(static function (MoneyInput $component, mixed $state) use ($min) {
return function (string $attribute, mixed $value, \Closure $fail) use ($component, $state, $min) {

$value = MoneyFormatter::parseDecimal(
$state,
$component->getCurrency()->getCode(),
$component->getLocale()
);

if ($value < $min) {
$fail('The :attribute must be greater than or equal to ' . $min . '.');
}
};
});

return $this;
}

public function maxValue(mixed $max): static
{
$this->rule(static function (MoneyInput $component, mixed $state) use ($max) {
return function (string $attribute, mixed $value, \Closure $fail) use ($component, $state, $max) {

$value = MoneyFormatter::parseDecimal(
$state,
$component->getCurrency()->getCode(),
$component->getLocale()
);

if ($value > $max) {
$fail('The :attribute must be less than or equal to ' . $max . '.');
}
};
});

return $this;
}
}

0 comments on commit b6bc755

Please sign in to comment.