You are here

public function LayoutOptionForm::validateForm in Bootstrap Layout Builder 1.x

Same name and namespace in other branches
  1. 2.x src/Form/LayoutOptionForm.php \Drupal\bootstrap_layout_builder\Form\LayoutOptionForm::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/LayoutOptionForm.php, line 131

Class

LayoutOptionForm
Form handler for the layout option entity forms.

Namespace

Drupal\bootstrap_layout_builder\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $layout_id = $this->routeMatch
    ->getParameter('blb_layout');
  if ($layout_id) {
    $layout = $this->entity
      ->getLayoutById($layout_id);
  }
  else {
    $layout = $this->entity
      ->getLayout();
  }
  $structure = $form_state
    ->getValue('structure');
  $structure = explode(' ', $structure);
  $invalid_structure = FALSE;

  // Make sure that all items are numbers.
  foreach ($structure as $col) {
    if (!is_numeric($col)) {
      $invalid_structure = TRUE;
      break;
    }
  }

  // Check the number of colmuns and the sum of the structure.
  if (count($structure) != $layout
    ->getNumberOfColumns() || array_sum($structure) != 12) {
    $invalid_structure = TRUE;
  }
  if ($invalid_structure) {
    $form_state
      ->setErrorByName('structure', $this
      ->t('Structure must be @cols numbers seperated by space and the sum of these numbers must equal 12!', [
      '@cols' => $layout
        ->getNumberOfColumns(),
    ]));
  }
}