Do I need to validate the length of the field?

For example, the nickname of the user in the mysql table field is stored as varchar. The length of the varchar, as I recall, 255 bytes.
Do I need to check that the length of the nickname is more than 255 characters (we will assume that the size of each symbol is 1 byte) and output to the user a message that "the length of the nickname is more than 255 characters"?
Or not to pay for the length of attention and keep the field in the database, and error (by the way, mysql will give an error if you try to record more data than possible?) prompt you with a message that "could not complete your request"?
If I should check whether it is necessary to do it generally for every text field? For example, the size of the field of type bigtext?
July 4th 19 at 22:54
3 answers
July 4th 19 at 22:56
Of course it is necessary to do for the user , if there is "could Not complete your request", he will think that the site is outside , but if "allowed nick length is more than 255 characters" then it will be that much clearer!
July 4th 19 at 22:58
> by the way, mysql will give an error if you try to write more data than can?

No, mysql will not give an error (from JhaoDa UPD: if you turned off strict mode (STRICT_TRANS_TABLES)). He simply cut the string to the desired length, in your case, up to 255 characters.

So the answer is Yes, check the length of the string you need to do, or simply if you exceed 255 characters, you lose the data.
mysql will shorten the deadline only if you turned off strict mode (STRICT_TRANS_TABLES). - Penelope91 commented on July 4th 19 at 23:01
: Yes, thank you for the correction. Used only in strict mode. I understand the default STRICT_TRANS_TABLES always disabled? - Cade_Bradt commented on July 4th 19 at 23:04
or else it will give an error, right?
Do you know some convenient package for such validation? Something like this:
'field' => 'required|type_varchar'
I wish this rule was not sehardcore for a specific database, and was a little smarter. When using mysql, was taken as the size of the varchar field in mysql when postgres, the size of the varchar field in postgres (assuming it is not 255 bytes).
I would also like to take into account the size of the utf-8 characters. - leland_Lindgren76 commented on July 4th 19 at 23:07
: well sort off, Yes. - Penelope91 commented on July 4th 19 at 23:10
: data must be validated according to the logic requirements of the project, not the database. Validator larval understands utf-8. - Penelope91 commented on July 4th 19 at 23:13
July 4th 19 at 23:00
JS check what the user enters, will be much nicer handling errors, but in php I would also check made.

Find more questions by tags LaravelPHPValidation