You are here

public function CustomServicesEditForm::validateForm in Shorten URLs 8

Same name and namespace in other branches
  1. 8.2 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 89

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 = db_query("SELECT COUNT(sid) FROM {shorten_cs} WHERE name = :name", array(
    ':name' => $v['name'],
  ))
    ->fetchField();
  if ($exists > 0) {
    $form_state
      ->setErrorByName('name', t('A service with that name already exists.'));
  }
  else {
    $all_services = \Drupal::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;
      }
    }
  }
}