textInput live error

i have textinput which has live(). this input is numberic also in db it is int but when i add some number and next remove it has error
Unsupported operand types: string / float
Unsupported operand types: string / float
becouse of its empty string when i remove (i think) how i can fix it?
9 Replies
awcodes
awcodes9mo ago
Trying adding a cast for it on the model to force it into an integer when saving it to the db. I might be misunderstanding your question though.
gigiloouu
gigiloouuOP9mo ago
TextInput::make('amount_percent') ->minValue(0) ->afterStateUpdated(function (Forms\Get $get, Forms\Set $set) { self::updateTotals($get, $set); }) ->live() I have text input which is live() this input is numberic also in db its INT and when i write some numbers its works okay but when i remove numbers and its empty i have error
Unsupported operand types: string / float
Unsupported operand types: string / float
awcodes
awcodes9mo ago
Ah. updateTotals() will probably need to check for a null or empty value if it’s performing calculations.
gigiloouu
gigiloouuOP9mo ago
i cant find updateTotals in documentation i thought that i should do that but i dont know how its will be better to chcek as i sew in documentation there is some Rules which works for this thing like live(onBlur:ture) but i want validation for numbers like if its not numbers dont use live or something like that idk
awcodes
awcodes9mo ago
Update totals isn’t part of core. That’s a function somewhere on your class. At least that’s how it looks to me. You can certainly use ->rules() to provide custom validation rules if you need to.
gigiloouu
gigiloouuOP9mo ago
im trying to do this more then 1 hour.. i sew only onblur rules for live can u hel pme how i can make validation that if value is not > then 0 dont use live i want use live only when there is numbers and not null or empty becouse when its empty i have error
awcodes
awcodes9mo ago
you can't change the live behavior on demand like that, at least to my knowledge. What you can do is force the value to be 0 if it is empty. something like:
->afterStateUpdated(function ($state) {
if ($state === '') {
return 0;
}
})
->afterStateUpdated(function ($state) {
if ($state === '') {
return 0;
}
})
Prosp30
Prosp309mo ago
I did the following Lets say in db field name is unit_price I make a hidden field named unit_price. Thats the one that gets updated. Then I use a presentational field named unitPrice, thats the one where user inputs some numbers. Once the user is done, the field gets formatted the way I want it (currency) and before the formatting, I set the numeric value of unit_price. Also I check in afterStateUpdate on unitPrice if the field is empty, less than 0 etc and set both fields to 0. Not the greatest solutiom but it works. For edit page, just use mutateFormBeforeFill and set unitPrice value from unit_price Also, I use fkoatval on mutateFormDataBeforeSave on unit_price just to be sure its the right type 😁
gigiloouu
gigiloouuOP9mo ago
yep thanks
if (!empty($value)) {
self::updateTotals($get, $set);
}
if (!empty($value)) {
self::updateTotals($get, $set);
}
` this worked
Want results from more Discord servers?
Add your server