You are here

public function TextBase::validateConfigurationForm in YAML Form 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides YamlFormElementBase::validateConfigurationForm

File

src/Plugin/YamlFormElement/TextBase.php, line 174

Class

TextBase
Provides a base 'text' (field) class.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  parent::validateConfigurationForm($form, $form_state);
  $properties = $this
    ->getConfigurationFormProperties($form, $form_state);

  // Validate #pattern's regular expression.
  // @see \Drupal\Core\Render\Element\FormElement::validatePattern
  // @see http://stackoverflow.com/questions/4440626/how-can-i-validate-regex
  if (!empty($properties['#pattern'])) {
    set_error_handler('_yamlform_entity_element_validate_rendering_error_handler');
    if (preg_match('{^(?:' . $properties['#pattern'] . ')$}', NULL) === FALSE) {
      $form_state
        ->setErrorByName('pattern', t('Pattern %pattern is not a valid regular expression.', [
        '%pattern' => $properties['#pattern'],
      ]));
    }
    set_error_handler('_drupal_error_handler');
  }

  // Validate #counter_maximum.
  if (!empty($properties['#counter_type']) && empty($properties['#counter_maximum'])) {
    $form_state
      ->setErrorByName('counter_maximum', t('Counter maximum is required.'));
  }
}