The string "abcdef" is supposed to be accepted as a valid hexadecimal color value. However, after adding an "Example Color RGB" field to any content type, on the field configuration page (eg. /admin/structure/types/manage/page/fields/node.page.field_chosen_color) once a default value is trying to be submitted, back-end validation rejects the form submission with this error message:
"Color must be a 6-digit hexadecimal value, suitable for CSS."
This comes from the validate() function in file field_example/src/Plugin/Field/FieldWidget/TextWidget.php at line :50. Therefore I suggest we should update the regex pattern to be more precise based on these solutions. Proposed regex pattern can be tested here.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 3053670-3.patch | 1.17 KB | valthebald |
| iss-3053669-color-code-validation-1.patch | 820 bytes | baluertl |
Comments
Comment #2
baluertlNow Drupal core 8.7.4 release has a built-in utility method
Drupal\Component\Utility\Color::validateHex(), I think this module should rely on that instead of implementing own logic separately.Comment #3
valthebaldActually, Color::validateHex() is in the core since 8.0.0, so it makes sense to use it.
Another alternative to regex is ctype_xdigit
Some other (minor) adjustments:
- CSS understands 6- or 3--digit codes, change the error message
- Use single quotes in error message
- Use strict comparison in strlen()
Comment #4
valthebaldCrediting Balu Ertl for the suggested solution
Comment #6
valthebald