Livewire: Empty nullable numeric field raises mysql exception

Created on 2 Nov 2020  路  3Comments  路  Source: livewire/livewire

Description

I have a integer()->nullable() field in my database. I wire:model an input for the user to put in a number or leave it blank. And I validate this property with nullable|integer. But when the user tries to update the field with an empty value, they get this error:

General error: 1366 Incorrect integer value: '' for column 'amount'

Because livewire passes this as an empty string instead of null.

I'm not sure why having untrimmed empty strings instead of null is the desired behaviour in Livewire.
This issue has been raised before, but we haven't got an explanation why this is.
https://github.com/livewire/livewire/issues/823

Most helpful comment

Please reference my response from #823

Not trimming strings and converting to null is the desired behavior of Livewire.

You can add traits to bypass this and that's what I would recommend if you want to re-introduce this behavior.

Closing this. Thanks!

There are 2 reasons for doing this:
A) If we kept Laravel's trim strings middleware, it would modify Livewire data at a network request level, which would cause errors because Livewire uses checksum to ensure data preservation. Therefore we CANT use Laravel's TrimStrings/EmptyStirngsToNull things as normal

B) I personally feel like it's more predicatable to NOT mutate empty strings into nulls at a data level. This kind of makes sense as a default for Laravel, but imagine scenarios where you are using @entangle with Alpine data and doing some kind of string manipuation with that data (.toUpperCase or something). If Livewire heavy-handedly changed all empty strings to null, you would get a .toUpperCase doesn't exist on null type thing. Additionally, that behavior is only effecting the root level of data, so nested data WOULDN"T behave this way which is even more confusing for people.

Anyhow, those are the reasons. Please take my earlier advice and use hooks, traits, or manual code to make that happen for yourself.

Thanks.

All 3 comments

Same issue here.
How empty string values can be converted to null?

Please reference my response from #823

Not trimming strings and converting to null is the desired behavior of Livewire.

You can add traits to bypass this and that's what I would recommend if you want to re-introduce this behavior.

Closing this. Thanks!

There are 2 reasons for doing this:
A) If we kept Laravel's trim strings middleware, it would modify Livewire data at a network request level, which would cause errors because Livewire uses checksum to ensure data preservation. Therefore we CANT use Laravel's TrimStrings/EmptyStirngsToNull things as normal

B) I personally feel like it's more predicatable to NOT mutate empty strings into nulls at a data level. This kind of makes sense as a default for Laravel, but imagine scenarios where you are using @entangle with Alpine data and doing some kind of string manipuation with that data (.toUpperCase or something). If Livewire heavy-handedly changed all empty strings to null, you would get a .toUpperCase doesn't exist on null type thing. Additionally, that behavior is only effecting the root level of data, so nested data WOULDN"T behave this way which is even more confusing for people.

Anyhow, those are the reasons. Please take my earlier advice and use hooks, traits, or manual code to make that happen for yourself.

Thanks.

Very nice. This sort of knowledge should be in the docs I think.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stefro picture stefro  路  3Comments

pmartelletti picture pmartelletti  路  3Comments

connecteev picture connecteev  路  4Comments

cloudstudio picture cloudstudio  路  3Comments

randohinn picture randohinn  路  3Comments