You are here

function beautytips_admin_validate in BeautyTips 7.2

Same name and namespace in other branches
  1. 6.2 beautytips.admin.inc \beautytips_admin_validate()

Validation for beautytips settings form

File

./beautytips.admin.inc, line 149
Beautytips settings form and submit action

Code

function beautytips_admin_validate($form, &$form_state) {
  $values = $form_state['values'];
  $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',
  ];
  $color_fields = [
    'bt-options-box-fill',
    'bt-options-css-color',
    'bt-options-box-shadowColor',
  ];
  $sanitize_strings = [
    'bt-options-cssClass',
    'bt-options-css-fontFamily',
    'bt-options-css-fontWeight',
  ];
  foreach ($integer_fields as $name) {
    if ($values[$name]) {
      if (!is_numeric($values[$name])) {
        form_set_error($name, 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['values'][$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_set_error($name, 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),
        ]));
      }
    }
  }
}