Problem/Motivation
A setting's limits can be written in two places: as a constraint like Range or Length in its config schema, and as #min, #max or #maxlength on its settings form element.
There are at least two problems with only the form having the constraints:
- Config written without the form, by a recipe, a config import or code, can't be properly validated.
- The constraints can't be accessed by other users inside or outside of Drupal, which is one of the things Rosetta is trying to fix.
This issue was split out of #3635315: [Prototype] Write a setting's rules once, in config schema, and use them for validation, forms and the published JSON Schema where I go into a lot more detail about these challenges.
Take the site slogan as an example. SiteInformationForm reads:
$form['site_information']['site_slogan'] = [
'#type' => 'textfield',
'#title' => $this->t('Slogan'),
'#config_target' => 'system.site:slogan',
'#description' => $this->t("How this is used depends on your site's theme."),
'#maxlength' => 255,
];
While the schema reads:
system.site:
type: config_object
label: 'Site information'
mapping:
slogan:
type: label
label: 'Slogan'
And:
label:
type: string
label: 'Optional label'
translatable: true
constraints:
Regex:
pattern: '/([^\PC])/u'
match: false
message: 'Labels are not allowed to span multiple lines or contain control characters.'
While its config schema gives slogan the type label, which comes with some constraints, it does not currently provide a length limit.
The fix for the slogan that takes two steps: a Length constraint in its config schema, which fits #3443432: Add validation constraints to system.site, and then this issue, so the form can drop its #maxlength.
Comments