While setting up a doctrine entity to be used alongside a Symfony form, I want to validate that the given string size is not going to exceed the column size of the database. Notice I use the word size and not length.
Symfony ships with a number of validation constraints, in particular the Length Constraint. However, looking at it's documentation
The charset to be used when computing value's length. The grapheme_strlen PHP function is used if available. If not, the mb_strlen PHP function is used if available. If neither are available, the strlen PHP function is used.
It uses grapheme_strlen
or mb_strlen
which return the visual length of the string and NOT the actual size. When dealing with non ASCII strings, the actual size is a factor. For example most Asian languages use 3 bytes for each character.
The doctrine documentation states nothing about the @Column(length)
annotation which leads me to believe it's a byte count. So, If I declare a column of length 64 and a constraint of max-size 64, there is going to be a problem.
Now, I could use a Callback Constraint or even create a custom constraint. But, is there a better way? How should strings received from form fields have their size validated before being persisted to the database?