You are here

class ClientsideValidationjQuerySettingsForm 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
  2. 3.0.x clientside_validation_jquery/src/Form/ClientsideValidationjQuerySettingsForm.php \Drupal\clientside_validation_jquery\Form\ClientsideValidationjQuerySettingsForm
  3. 2.0.x clientside_validation_jquery/src/Form/ClientsideValidationjQuerySettingsForm.php \Drupal\clientside_validation_jquery\Form\ClientsideValidationjQuerySettingsForm

Class ClientsideValidationjQuerySettingsForm.

Hierarchy

Expanded class hierarchy of ClientsideValidationjQuerySettingsForm

1 string reference to 'ClientsideValidationjQuerySettingsForm'
clientside_validation_jquery.routing.yml in clientside_validation_jquery/clientside_validation_jquery.routing.yml
clientside_validation_jquery/clientside_validation_jquery.routing.yml

File

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

Namespace

Drupal\clientside_validation_jquery\Form
View source
class ClientsideValidationjQuerySettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'clientside_validation_jquery_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  public function getEditableConfigNames() {
    return [
      'clientside_validation_jquery.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this
      ->config('clientside_validation_jquery.settings');
    $form['use_cdn'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use JS from CDN'),
      '#description' => $this
        ->t('CDN is used by default if JS not added into libraries.'),
      '#default_value' => $config
        ->get('use_cdn'),
    ];
    $form['cdn_base_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('CDN Base URL'),
      '#description' => $this
        ->t('CDN to use (along with version in URL). E.g. @url', [
        '@url' => '//cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/',
      ]),
      '#required' => TRUE,
      '#default_value' => $config
        ->get('cdn_base_url'),
    ];
    $form['validate_all_ajax_forms'] = [
      '#type' => 'select',
      '#options' => [
        1 => $this
          ->t('Yes'),
        2 => $this
          ->t('No'),
      ],
      '#title' => $this
        ->t('Validate all forms before AJAX submit'),
      '#description' => $this
        ->t('All forms with class "cv-validate-before-ajax" will be validated by default'),
      '#required' => TRUE,
      '#default_value' => $config
        ->get('validate_all_ajax_forms'),
    ];
    $form['force_validate_on_blur'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Validate on Blur/focusout'),
      '#default_value' => $config
        ->get('force_validate_on_blur'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('clientside_validation_jquery.settings');
    $config
      ->set('use_cdn', $form_state
      ->getValue('use_cdn'));
    $config
      ->set('cdn_base_url', $form_state
      ->getValue('cdn_base_url'));
    $config
      ->set('validate_all_ajax_forms', $form_state
      ->getValue('validate_all_ajax_forms'));
    $config
      ->set('force_validate_on_blur', $form_state
      ->getValue('force_validate_on_blur'));
    $config
      ->save();
    return parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  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/',
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClientsideValidationjQuerySettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ClientsideValidationjQuerySettingsForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ClientsideValidationjQuerySettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ClientsideValidationjQuerySettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ClientsideValidationjQuerySettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.