You are here

function imagefield_extended_options_validate in ImageField Extended 6.3

Same name and namespace in other branches
  1. 6.4 imagefield_extended.admin.inc \imagefield_extended_options_validate()

To parse a newline selection list into options.

1 string reference to 'imagefield_extended_options_validate'
imagefield_extended_admin_settings_form in ./imagefield_extended.admin.inc
Menu callback. Used to define what field types will be available when creating an imagefield with additional fields CCK field.

File

./imagefield_extended.admin.inc, line 39
Administration related functions.

Code

function imagefield_extended_options_validate($element, &$form_state) {

  // Defined as static to preserve state between the two validation callbacks.
  static $used_keys = array();
  $options = array();
  $reserved_keys = array(
    'description',
    'alt',
    'title',
  );
  $rows = array_filter(explode("\n", trim($element['#value'])));
  foreach ($rows as $option) {
    $option = trim($option);
    if (strlen($option)) {
      if (preg_match('/^([^|]+)\\|(.*)$/', $option, $matches)) {
        $matches[1] = trim($matches[1]);
        $matches[2] = trim($matches[2]);
        if (empty($matches[1]) || empty($matches[2])) {
          form_set_error(implode('][', $element['#array_parents']), t('Each option must be a key / value pair seperated by a pipe (|).'));
          return;
        }
        elseif (!preg_match('/^[a-z_]{1}[a-z0-9_]*$/', $matches[1])) {
          form_set_error(implode('][', $element['#array_parents']), t('The specified key contains one or more illegal characters. Spaces or any other special characters except an underscore (_) are not allowed. The first letter can not be a number.'));
          return;
        }
        elseif (in_array($matches[1], $reserved_keys)) {
          form_set_error(implode('][', $element['#array_parents']), t('The specified key %key is a reserved keyword.', array(
            '%key' => $matches[1],
          )));
          return;
        }
        elseif (in_array($matches[1], $used_keys)) {
          form_set_error(implode('][', $element['#array_parents']), t('The specified key %key is already in use. The combined keys for both the checkboxes and text options must be unique.', array(
            '%key' => $matches[1],
          )));
          return;
        }
        $used_keys[] = $matches[1];
        $options[] = $matches[1] . '|' . $matches[2];
      }
      else {
        form_error($element, t('Each option must be a key / value pair seperated by a pipe (|).'));
        return;
      }
    }
  }
  form_set_value($element, implode("\n", $options), $form_state);
}