Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 294618

Validatable simple config: introduce ValidatableConfigFormBase, the validatable subclass of ConfigFormBase

$
0
0

Problem/Motivation

Follow-up for the ancient #1921996: Convert system_config_form() to implement FormInterface as a base class.!

Extracted from #2164373-16: [META] Untie config validation from form validation — enables validatable Recipes, decoupled admin UIs … from >4 years ago.

The CDN contrib module has been using this successfully for >4 years: #2969065: Use typed config validation constraints for validation of cdn.settings simple config.

It's currently very difficult to get started with using validation constraints for Simple Configuration. Defining the validation constraints is one thing (and simpler to test since https://www.drupal.org/project/config_inspector/releases/2.1.1), but then how to use them?

Steps to reproduce

N/A

Proposed resolution

Introduce abstract class ValidatableConfigFormBase extends ConfigFormBase, where the only required method is:

  /**
   * Maps the form values from form state onto the given editable Config.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param \Drupal\Core\Config\Config $config
   *   The configuration being edited.
   *
   * @return \Drupal\Core\Config\Config
   *   The updated configuration object.
   */
  abstract protected static function mapFormValuesToConfig(FormStateInterface $form_state, Config $config): Config;

and often you'd also need to override the default

  /**
   * Maps the given violation constraint property path to a form name.
   *
   * @param string $config_name
   *   The name of the Config whose property path triggered a validation error.
   * @param string $property_path
   *   The property path that triggered a validation error.
   *
   * @return string
   *   The corresponding form name.
   */
  protected static function mapConfigPropertyPathToFormElementName(string $config_name, string $property_path) : string {
    // A default implementation, which is suitable when the configuration is
    // mapped 1:1 to form elements.
    return str_replace('.', '][', $property_path);
  }

Remaining tasks

Review.

User interface changes

None.

API changes

None. Only addition.

Data model changes

None.

Release notes snippet

TBD


Viewing all articles
Browse latest Browse all 294618

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>