You are here

public function ClientsideValidationjQuerySettingsForm::validateForm in Clientside Validation 8

Same name and namespace in other branches
  1. 8.2 clientside_validation_jquery/src/Form/ClientsideValidationjQuerySettingsForm.php \Drupal\clientside_validation_jquery\Form\ClientsideValidationjQuerySettingsForm::validateForm()
  2. 3.0.x clientside_validation_jquery/src/Form/ClientsideValidationjQuerySettingsForm.php \Drupal\clientside_validation_jquery\Form\ClientsideValidationjQuerySettingsForm::validateForm()
  3. 2.0.x clientside_validation_jquery/src/Form/ClientsideValidationjQuerySettingsForm.php \Drupal\clientside_validation_jquery\Form\ClientsideValidationjQuerySettingsForm::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

clientside_validation_jquery/src/Form/ClientsideValidationjQuerySettingsForm.php, line 92

Class

ClientsideValidationjQuerySettingsForm
Class ClientsideValidationjQuerySettingsForm.

Namespace

Drupal\clientside_validation_jquery\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  if ($form_state
    ->getErrors()) {
    return;
  }
  $values = $form_state
    ->getValues();

  // Validate if library exists if use CDN is set to false.
  if (empty($values['use_cdn'])) {
    $library_exists = file_exists('libraries/jqueryvalidate/dist/jquery.validate.js');
    if (empty($library_exists)) {
      $form_state
        ->setErrorByName('use_cdn', $this
        ->t('Please make sure JS is available in Drupal Libraries. Check README in module folder for more details.'));
    }
  }

  // Validate if the CDN url is proper.
  $cdn_url = $values['cdn_base_url'] . 'jquery.validate.min.js';
  if (file_get_contents($cdn_url) === FALSE) {
    $form_state
      ->setErrorByName('cdn_base_url', $this
      ->t('CDN URL seems invalid. @file not accessible on @url. Use the URL in this format @format.', [
      '@file' => 'jquery.validate.min.js',
      '@url' => $cdn_url,
      '@format' => '//cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/',
    ]));
  }
}