You are here

public function TextBase::validateConfigurationForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/TextBase.php \Drupal\webform\Plugin\WebformElement\TextBase::validateConfigurationForm()

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 WebformElementBase::validateConfigurationForm

File

src/Plugin/WebformElement/TextBase.php, line 318

Class

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

Namespace

Drupal\webform\Plugin\WebformElement

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('_webform_entity_element_validate_rendering_error_handler');

    // PHP: Convert JavaScript-escaped Unicode characters to PCRE escape
    // sequence format.
    // @see https://bytefreaks.net/programming-2/php-programming-2/php-convert-javascript-escaped-unicode-characters-to-html-hex-references
    $pcre_pattern = preg_replace('/\\\\u([a-fA-F0-9]{4})/', '\\x{\\1}', $properties['#pattern']);
    if (preg_match('{^(?:' . $pcre_pattern . ')$}u', NULL) === FALSE) {
      $form_state
        ->setErrorByName('pattern', $this
        ->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']) && empty($properties['#counter_minimum'])) {
    $form_state
      ->setErrorByName('counter_minimum', $this
      ->t('Counter minimum or maximum is required.'));
    $form_state
      ->setErrorByName('counter_maximum', $this
      ->t('Counter minimum or maximum is required.'));
  }
}