You are here

public function WebformEntityElementsValidator::validate in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformEntityElementsValidator.php \Drupal\webform\WebformEntityElementsValidator::validate()

Validate webform elements.

Parameters

\Drupal\webform\WebformInterface $webform: A webform.

array $options: An array of validation rules to check.

Return value

array|null An array of error messages or NULL if the elements are valid.

Overrides WebformEntityElementsValidatorInterface::validate

File

src/WebformEntityElementsValidator.php, line 139

Class

WebformEntityElementsValidator
Webform elements validator.

Namespace

Drupal\webform

Code

public function validate(WebformInterface $webform, array $options = []) {
  $options += [
    'required' => TRUE,
    'yaml' => TRUE,
    'array' => TRUE,
    'names' => TRUE,
    'properties' => TRUE,
    'submissions' => TRUE,
    'variants' => TRUE,
    'hierarchy' => TRUE,
    'pages' => TRUE,
    'rendering' => TRUE,
  ];
  $this->webform = $webform;
  $this->elementsRaw = $webform
    ->getElementsRaw();
  $this->originalElementsRaw = $webform
    ->getElementsOriginalRaw();

  // Validate required.
  if ($options['required'] && ($message = $this
    ->validateRequired())) {
    return [
      $message,
    ];
  }

  // Validate contain valid YAML.
  if ($options['yaml'] && ($message = $this
    ->validateYaml())) {
    return [
      $message,
    ];
  }
  $this->elements = WebformYaml::decode($this->elementsRaw);
  $this->originalElements = WebformYaml::decode($this->originalElementsRaw);
  $this->elementKeys = [];
  if (is_array($this->elements)) {
    $this
      ->getElementKeysRecursive($this->elements, $this->elementKeys);
  }

  // Validate elements are an array.
  if ($options['array'] && ($message = $this
    ->validateArray())) {
    return [
      $message,
    ];
  }

  // Validate duplicate element name.
  if ($options['names']) {
    if ($messages = $this
      ->validateNames()) {
      return $messages;
    }
    elseif ($messages = $this
      ->validateDuplicateNames()) {
      return $messages;
    }
  }

  // Validate ignored properties.
  if ($options['properties'] && ($messages = $this
    ->validateProperties())) {
    return $messages;
  }

  // Validate submission data.
  if ($options['submissions'] && ($messages = $this
    ->validateSubmissions())) {
    return $messages;
  }

  // Validate variants data.
  if ($options['variants'] && ($messages = $this
    ->validateVariants())) {
    return $messages;
  }

  // Validate hierarchy.
  if ($options['hierarchy'] && ($messages = $this
    ->validateHierarchy())) {
    return $messages;
  }

  // Validate pages.
  if ($options['pages'] && ($messages = $this
    ->validatePages())) {
    return $messages;
  }

  // Validate rendering.
  if ($options['rendering'] && ($message = $this
    ->validateRendering())) {
    return [
      $message,
    ];
  }
  return NULL;
}