I have the following validation:
'location.zip_code' => ['required', 'numeric', 'max:99999'],
It’s working as expected except if I submit my form with a numeric value preceeded by a space, like 12345
. A trailing space gets trimmed via the TrimStrings
middleware (presumably), but a leading space doesn’t?
This ends up causing this error:
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'zip_code' at row 1
I know I can change my save method to do something like, $validatedData = $this->validate();
and then handle each form input value, trimming this one in the process, but I’ve got a lot going on in the form, and it would be great if there was a way for this to just work the way it should work. Hoping someone has an idea.
2
Answers
I think the correct approach is to modify the
zip_code
field in my database to be an integer left padded to 5 digits to account for zip codes that start with 0. Similar to this:https://stackoverflow.com/a/3201014/4113027
In your database, the column ‘’zip_code’’ change the data type to VarChar or String