You are here

public function SmsGatewayForm::validateForm in SMS Framework 8

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/SmsGatewayForm.php, line 244

Class

SmsGatewayForm
Form controller for SMS Gateways.

Namespace

Drupal\sms\Form

Code

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

  /** @var \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway */
  $sms_gateway = $this
    ->getEntity();
  if ($sms_gateway
    ->isNew()) {
    $sms_gateway = SmsGateway::create([
      'plugin' => $form_state
        ->getValue('plugin_id'),
    ]);
    $this
      ->setEntity($sms_gateway);
  }
  else {
    $sms_gateway
      ->getPlugin()
      ->validateConfigurationForm($form, $form_state);
  }
  $path_elements_parents = [
    [
      'incoming_messages',
      'push_path',
    ],
    [
      'delivery_reports',
      'push_path',
    ],
  ];
  foreach ($path_elements_parents as $parents) {
    $element = NestedArray::getValue($form, $parents);
    $path = $form_state
      ->getValue($parents);
    $path_length = Unicode::strlen($path);

    // Length must be more than 2 chars, including leading slash character.
    if ($path_length > 0) {
      if (Unicode::substr($path, 0, 1) !== '/') {
        $form_state
          ->setError($element, $this
          ->t("Path must begin with a '/' character."));
      }
      if ($path_length == 1) {
        $form_state
          ->setError($element, $this
          ->t("Not enough characters for path."));
      }
    }
  }
}