You are here

public function CartLinksSettingsForm::validateForm in Ubercart 8.4

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

uc_cart_links/src/Form/CartLinksSettingsForm.php, line 106

Class

CartLinksSettingsForm
Configure general shopping cart settings for this site.

Namespace

Drupal\uc_cart_links\Form

Code

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

  // Check for properly formatted Cart Links in the restrictions textarea.
  $restrictions = trim($form_state
    ->getValue('uc_cart_links_restrictions'));
  if (!empty($restrictions)) {
    $data = array_map('trim', explode("\n", $restrictions));
    foreach ($data as $restriction) {

      // Ignore blank lines.
      if (preg_match('/^\\s*$/', $restriction)) {
        continue;
      }
      elseif (!$this->cartLinksValidator
        ->isValidSyntax('/cart/add/' . $restriction)) {
        $form_state
          ->setErrorByName('uc_cart_links_restrictions', $this
          ->t('Invalid syntax in Cart Links restriction "%restriction".', [
          '%restriction' => $restriction,
        ]));
      }
    }
  }

  // Check for properly formatted messages.
  $messages = trim($form_state
    ->getValue('uc_cart_links_messages'));
  if (!empty($messages)) {
    $data = array_map('trim', explode("\n", $messages));
    foreach ($data as $message) {

      // Ignore blank lines.
      if (preg_match('/^\\s*$/', $message)) {
        continue;
      }
      elseif (!preg_match('/^\\s*[1-9][0-9]*\\s*\\|\\s*\\S+.*$/', $message)) {
        $form_state
          ->setErrorByName('uc_cart_links_messages', $this
          ->t('Invalid Cart Links message "%message". Messages must be a numeric key followed by "|" followed by a value.', [
          '%message' => $message,
        ]));
        break;
      }
    }
  }
  return parent::validateForm($form, $form_state);
}