Skip to content

Commit

Permalink
Merge pull request #74 from remiebelingmerifond/bugfix/min-and-max-value
Browse files Browse the repository at this point in the history
Fix Closures in MaxValue and MinValue
  • Loading branch information
pelmered authored Dec 4, 2024
2 parents 580e031 + b76ec6c commit 1549291
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@
use Pelmered\FilamentMoneyField\Forms\Rules\MaxValueRule;
use Pelmered\FilamentMoneyField\Forms\Rules\MinValueRule;
use Pelmered\FilamentMoneyField\MoneyFormatter\MoneyFormatter;
use Closure;

class MoneyInput extends TextInput
{
use HasMoneyAttributes;

protected ?string $symbolPlacement = null;

/**
* @var scalar | Closure | null
*/
protected $maxValue = null;

/**
* @var scalar | Closure | null
*/
protected $minValue = null;

protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -99,14 +110,30 @@ public function symbolPlacement(string|\Closure|null $symbolPlacement = null): s

public function minValue(mixed $value): static
{
$this->rule(new MinValueRule((int) $this->evaluate($value), $this));
$this->minValue = $value;

$this->rule(
static function (MoneyInput $component) {
$value = $component->getMinValue();
return new MinValueRule($value, $component);
},
static fn (MoneyInput $component): bool => filled($component->getMinValue())
);

return $this;
}

public function maxValue(mixed $value): static
{
$this->rule(new MaxValueRule((int) $this->evaluate($value), $this));
$this->maxValue = $value;

$this->rule(
static function (MoneyInput $component) {
$value = $component->getMaxValue();
return new MaxValueRule($value, $component);
},
static fn (MoneyInput $component): bool => filled($component->getMaxValue())
);

return $this;
}
Expand Down

0 comments on commit 1549291

Please sign in to comment.