Earlier this year, I talked about why we validate maximum length on form request fields that will be saved to the database.
This is great for avoiding database exceptions or truncated data, but can we go a step further and make this even better?
A simple improvement is to also use the maxlength
attribute on the HTML input element.
This will prevent the user from entering more characters than the maximum length, only to get a validation error when submitting.
Of course, on its own maxlength
is not sufficient, but it's a nice additional UX improvement on top of the enforced server-side validation logic.
Here to help,
Joel
P.S. The Mastering Laravel Validation Rules book is a great resource for better leveraging the power of validation to write more reliable Laravel apps.