Problem/Motivation
Some settings can't be empty, like the name Drupal shows for anonymous users.
Developers write this rule twice: in config schema, so that Drupal refuses to save an empty value, and again in the settings form, so that the form marks the field as required.
There are two problems with this.
First, the two can drift apart. If the form leaves out #required, it doesn't mark the field as required, and people only find out after they press save.
Second, various tools only read the config schema: an auto-generated settings form, a front end, a workflow tool like ECA/FlowDrop/Maestro, or an AI agent. A rule that only exists in the form code, such as #required, is invisible to them. When the form takes the rule from config schema, config schema becomes the one place to set it.
Proposed resolution
When a form element's #config_target points to a setting with a NotBlank constraint, and the element doesn't set #required itself, mark the element as required. Developers then write the rule once, in config schema, and we can remove #required from a number of forms.
This follows #3636978: Take a #config_target form element's #title and #description from its config schema, which takes a form element's #title and #description from config schema, and #3636979: Take a #config_target form element's #min, #max and #maxlength from its config schema constraints, which takes its #min, #max and #maxlength.
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, which goes into more detail about the challenges and opportunities.
Comments