You are here

function autofill_settings_form_validate in Autofill 6

Settings form validate handler to ensure a hexadecimal color value.

1 string reference to 'autofill_settings_form_validate'
autofill_form in ./autofill.admin.inc
Implementation of Autofill settings form.

File

./autofill.admin.inc, line 75
Adminstrative page callbacks for Autofill

Code

function autofill_settings_form_validate($form, &$form_state) {

  // Validate default text color field.
  if (!empty($form_state['values']['autofill_colour_default'])) {
    if (!preg_match('/^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/', $form_state['values']['autofill_colour_default'])) {
      form_error($form, t('%colour-default Text color must be a hexadecimal color value like %color.', array(
        '%color' => '#ccc',
        '%colour-default' => $form_state['values']['autofill_colour_default'],
      )));
    }
  }

  // Validate text color field.
  if (!empty($form_state['values']['autofill_colour_active'])) {
    if (!preg_match('/^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/', $form_state['values']['autofill_colour_active'])) {
      form_error($form, t('%colour-active Text color must be a hexadecimal color value like %color.', array(
        '%color' => '#333',
        '%colour-active' => $form_state['values']['autofill_colour_active'],
      )));
    }
  }

  // Validate ID field.
  $fields = $form_state['values']['fields'];
  if (!empty($fields)) {
    foreach ($fields as $delta => $field) {

      // Check if ID field uses the correct CSS ID or class identifier.
      if (!empty($fields[$delta]['id']) && $fields[$delta]['id'][0] != '#' && $fields[$delta]['id'][0] != '.') {
        form_error($form, t('%field-id doesn\'t contain a CSS class or ID identifier (e.g. <em>#specific</em> for <em>id="specific"</em> and <em>.general</em> for <em>class="general"</em>).', array(
          '%field-id' => $fields[$delta]['id'],
        )));
      }

      // Check if either value or pre populate value is specified.
      if (!empty($fields[$delta]['id']) && empty($fields[$delta]['value']) && empty($fields[$delta]['pre'])) {
        form_error($form, t('%field-id field must contain either a <em>Empty value</em> or <em>Pre-populate value</em>.', array(
          '%field-id' => $fields[$delta]['id'],
        )));
      }
    }
  }
}