Issue #3395555: 🦺 add constraint configuration for...
Merge request reports
Activity
- Resolved by Elliot Ward
- Resolved by Elliot Ward
added 1 commit
125 125 color_hex: 126 126 type: string 127 127 label: 'Color' 128 constraints: 129 Regex: 130 pattern: '/^#[a-fA-F0-9]{6}$/' 131 message: ":%value is not a valid hexadecimal color." changed this line in version 3 of the diff
125 125 color_hex: 126 126 type: string 127 127 label: 'Color' 128 constraints: 129 Regex: 130 pattern: '/^#[a-fA-F0-9]{6}$/' Whilst 3 character HEX codes are a thing, Olivero does not currently accept them
function olivero_theme_settings_validate($form, FormStateInterface $form_state) { if (!preg_match('/^#[a-fA-F0-9]{6}$/', $form_state->getValue('base_primary_color'))) { $form_state->setErrorByName('base_primary_color', t('Colors must be 7-character string specifying a color hexadecimal format.')); } }
See also similar regex in
olivero_form_system_theme_settings_alter()
However, the only other place that color_hex schema type is used is the Image module. This uses a different hex valdation, Drupal\Component\Utility\Color::validateHex().
I think we first need to change Olivero to use Color::validateHex() and support 3 character hex codes, then wrap Color::validateHex() as a new constraint/validator and use that in this issue.
This was fixed in https://www.drupal.org/project/drupal/issues/3396018!
changed this line in version 8 of the diff
added 1 commit
added 436 commits
-
9affd898...62498926 - 435 commits from branch
project:11.x
- c53a475e - Merge remote-tracking branch 'origin/11.x' into 3395555-add-validation-constraints
-
9affd898...62498926 - 435 commits from branch
added 1 commit
- d8bd35a5 - Issues/3395555: Update Regex to match both 3 and 6 digit hex codes.
added 1 commit
- d6392833 - Issues/3395555: Update Regex to match both 3 and 6 digit hex codes.
added 1 commit
- e27350ab - Issues/3395555: Add test to validate hex code.
279 ]; 280 $valid_hex_codes = [ 281 '0F0', 282 '#F0F', 283 '#2ecc71', 284 '0074cc', 285 ]; 286 287 // Visit Olivero's theme settings page. 288 $this->drupalGet('admin/appearance/settings/olivero'); 289 290 // Test invalid hex color codes. 291 foreach ($invalid_hex_codes as $invalid_hex) { 292 $this->submitForm(['base_primary_color' => $invalid_hex], 'Save configuration'); 293 // Invalid hex codes should throw error. 294 $this->assertSession()->pageTextContains('"' . $invalid_hex . '" is not a valid hexadecimal color.'); changed this line in version 12 of the diff
added 1 commit
- a602660a - Issues/3395555: Use statusMessageContains method in tests.
126 126 color_hex: 127 127 type: string 128 128 label: 'Color' 129 constraints: 130 Regex: 131 # Regex copied from Color::validateHex() 132 pattern: '/^[#]?([0-9a-fA-F]{3}){1,2}$/' Very good question!
Multiple answers:
- Strong reason:
Color::validateHex()
would not allowNULL
, but this constraint does. (Anything in config can be marked optional, soNULL
must never trigger a validation error)Color::validateHex()
does not allow this. - Dreamy reason: The
Regex
constraint is something that an external tool (no matter whether that's something generating/validating Drupal config or a JSON:API/REST/GraphQL/… client) to also correctly validate this. (This is one of the original dreams if you look back at the original config schema issues.)
- Strong reason:
added 1 commit