You are here

public function CustomServicesEditForm::validateForm in Shorten URLs 8.2

Same name and namespace in other branches
  1. 8 modules/shorten_cs/src/Form/CustomServicesEditForm.php \Drupal\shorten_cs\Form\CustomServicesEditForm::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

modules/shorten_cs/src/Form/CustomServicesEditForm.php, line 157

Class

CustomServicesEditForm
Settings form.

Namespace

Drupal\shorten_cs\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $v = $form_state
    ->getValues();
  if (($v['type'] == 'xml' || $v['type'] == 'json') && empty($v['tag'])) {
    $form_state
      ->setErrorByName('type', t('An XML tag or JSON key is required for services with a response type of XML or JSON.'));
  }
  $exists = $this->database
    ->query("SELECT COUNT(sid) FROM {shorten_cs} WHERE name = :name", [
    ':name' => $v['name'],
  ])
    ->fetchField();
  if ($exists > 0) {
    $form_state
      ->setErrorByName('name', t('A service with that name already exists.'));
  }
  else {
    $all_services = $this->moduleHandler
      ->invokeAll('shorten_service');
    $all_services['none'] = t('None');
    foreach ($all_services as $key => $value) {
      if ($key == $v['name']) {
        $form_state
          ->setErrorByName('name', t('A service with that name already exists.'));
        break;
      }
    }
  }
}