moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.56k stars 439 forks source link

This field is attempting to submit the form on page load for some reason #788

Closed hyphen81 closed 3 months ago

hyphen81 commented 3 months ago

First off, thanks for this package, I think it will be a huge help to me. I'm running into an issue where it seems to be submitting my form as soon as the page loads. In my use case (livewire component) there's not a model initially, so i get a failure on an insert statement to my model table. If I comment out the MoneyInput field, it works as expected.

    public function form(Form $form): Form
    {
        return $form->schema([
            Section::make()->schema([

    ...my other fields...

                MoneyInput::make('total')
                    ->required()
                    // ->live(debounce: 500)
                    ->minValue(0)
                    ->afterStateUpdated(function (?string $state, ?string $old) {

                        Log::debug($state);

                        $this->item->total = $this->toInteger($state);
                        $this->item->save();
                    }),
            ])->compact()

        ])->statePath('data')
            ->model($this->item);
    }

I thought live() might be causing it, so I commented that out and I'm still getting the same behavior.

If I comment out the afterStateUpdated method, it works, but when I log out $state on form submit, it appears to be the float representation and I need to get my values converted to integers before storing them in the DB. Is there some auto-magic way to do that that I missed somewhere?

image

frederikbosch commented 3 months ago

We are not a support desk.