You are here

public function BeautytipsConfigForm::validateForm in BeautyTips 8

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

src/Form/BeautytipsConfigForm.php, line 204

Class

BeautytipsConfigForm
Beautytips admin settings form.

Namespace

Drupal\beautytips\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $integer_fields = [
    'bt-options-box-strokeWidth',
    'bt-options-box-cornerRadius',
    'bt-options-box-spikeGirth',
    'bt-options-box-spikeLength',
    'bt-options-box-shadowBlur',
  ];
  $pixel_fields = [
    'bt-options-box-width',
    'bt-options-box-padding',
    'bt-options-css-fontSize',
  ];
  foreach ($integer_fields as $name) {
    if ($values[$name]) {
      if (!is_numeric($values[$name])) {
        $form_state
          ->setErrorByName($name, $this
          ->t('You need to enter a numeric value for <em>@name</em>', [
          '@name' => str_replace([
            'bt-options-box-',
            'bt-options-css-',
          ], '', $name),
        ]));
      }
      else {
        $form_state
          ->setValue($name, round($values[$name]));
      }
    }
  }
  foreach ($pixel_fields as $name) {
    if ($values[$name]) {
      $unit = substr($values[$name], -2, 2);
      $value = str_replace([
        'px',
        ' ',
        'em',
      ], '', $values[$name]);
      if (!is_numeric($value) || !$value && $value != 0 || !in_array($unit, [
        'px',
        'em',
      ])) {
        $form_state
          ->setErrorByName($name, $this
          ->t('You need to enter a numeric value for <em>@name</em>, followed by <em>px</em>', [
          '@name' => str_replace([
            'bt-options-box-',
            'bt-options-css-',
          ], '', $name),
        ]));
      }
    }
  }
  parent::validateForm($form, $form_state);
}