You are here

public function Sliders::validateConfigurationForm in Better Exposed Filters 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/better_exposed_filters/filter/Sliders.php \Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter\Sliders::validateConfigurationForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides BetterExposedFiltersWidgetBase::validateConfigurationForm

File

src/Plugin/better_exposed_filters/filter/Sliders.php, line 136

Class

Sliders
JQuery UI slider widget implementation.

Namespace

Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  parent::validateConfigurationForm($form, $form_state);

  // Max must be > min.
  $min = $form_state
    ->getValue('min');
  $max = $form_state
    ->getValue('max');
  if (!empty($min) && $max <= $min) {
    $form_state
      ->setError($form['max'], $this
      ->t('The slider max value must be greater than the slider min value.'));
  }

  // Step must have:
  // - No more than 5 decimal places.
  // - Slider range must be evenly divisible by step.
  $step = $form_state
    ->getValue('step');
  if (strlen(substr(strrchr((string) $step, '.'), 1)) > 5) {
    $form_state
      ->setError($form['step'], $this
      ->t('The slider step option for %name cannot have more than 5 decimal places.'));
  }

  // Very small step and a vary large range can go beyond the max value of
  // an int in PHP. Thus we look for a decimal point when casting the result
  // to a string.
  if (strpos((string) ($max - $min) / $step, '.')) {
    $form_state
      ->setError($form['step'], $this
      ->t('The slider range must be evenly divisible by the step option.'));
  }
}