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.

Comments

Balu Ertl created an issue. See original summary.

baluertl’s picture

Now 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.

valthebald’s picture

StatusFileSize
new1.17 KB

Actually, 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()

valthebald’s picture

Crediting Balu Ertl for the suggested solution

  • dfa3f6f committed on 8.x-1.x
    Issue #3053670 by Balu Ertl, valthebald: Incorrect validation of...
valthebald’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.