function ConfigEditor::validateForm

Same name in other branches
  1. 4.x src/Form/ConfigEditor.php \Drupal\devel\Form\ConfigEditor::validateForm()

Overrides FormBase::validateForm

File

src/Form/ConfigEditor.php, line 127

Class

ConfigEditor
Edit config variable form.

Namespace

Drupal\devel\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) : void {
    $value = $form_state->getValue('new');
    // Try to parse the new provided value.
    try {
        $parsed_value = Yaml::decode($value);
        // Config::setData needs array for the new configuration and
        // a simple string is valid YAML for any reason.
        if (is_array($parsed_value)) {
            $form_state->setValue('parsed_value', $parsed_value);
        }
        else {
            $form_state->setErrorByName('new', $this->t('Invalid input'));
        }
    } catch (InvalidDataTypeException $e) {
        $form_state->setErrorByName('new', $this->t('Invalid input: %error', [
            '%error' => $e->getMessage(),
        ]));
    }
}