You are here

public function NodeTitleValidationAdminForm::validateForm in Node Title Validation 8

Same name and namespace in other branches
  1. 2.0.x src/Form/NodeTitleValidationAdminForm.php \Drupal\node_title_validation\Form\NodeTitleValidationAdminForm::validateForm()
  2. 1.0.x src/Form/NodeTitleValidationAdminForm.php \Drupal\node_title_validation\Form\NodeTitleValidationAdminForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/NodeTitleValidationAdminForm.php, line 175

Class

NodeTitleValidationAdminForm
Class NodeTitleValidationAdminForm.

Namespace

Drupal\node_title_validation\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Get available content types.
  $node_types = $this->entityTypeManager
    ->getStorage('node_type')
    ->loadMultiple();

  // Loop for each content type & validate min, max values.
  foreach ($node_types as $type) {

    // Get max characters count.
    $max = $form_state
      ->getValue([
      'max-' . $type
        ->get('type'),
    ]);

    // Get min characters count.
    $min = $form_state
      ->getValue([
      'min-' . $type
        ->get('type'),
    ]);

    // Validate min is less than max value.
    if ($min > $max) {
      $form_state
        ->setErrorByName('min-' . $type
        ->get('type'), $this
        ->t("Minimum length should not be more than Max length"));
    }

    // Get min word count.
    $min_wc = $form_state
      ->getValue('min-wc-' . $type
      ->get('type'));

    // Get max word count.
    $max_wc = $form_state
      ->getValue([
      'max-wc-' . $type
        ->get('type'),
    ]);

    // Validate min is less than max value.
    if (!empty($min_wc) && !empty($max_wc) && $min_wc > $max_wc) {
      $form_state
        ->setErrorByName('max-wc-' . $type
        ->get('type'), $this
        ->t("Minimum word count of title should not be more than Maximum word count"));
    }
  }
}