You are here

function twitterfield_widget_validate in TwitterField 6

An #element_validate callback for the twitterfield_widget field type.

1 string reference to 'twitterfield_widget_validate'
twitterfield_elements in ./twitterfield.module
Implementation of hook_elements().

File

./twitterfield.module, line 159
TwitterField: Defines a CCK widget and display formatters for Twitter items.

Code

function twitterfield_widget_validate($element, &$form_state) {
  $value = $element['#value']['value'];
  if (empty($value)) {
    return;
  }
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $allowed_types = $field['widget']['allowed_types'];
  $allowed_types_count = count(array_filter($allowed_types));
  $error = '';
  if ($allowed_types_count == 0) {

    // We must be on the settings page, checking the default value,
    // and the allowed types failed validation.
    return;
  }
  elseif ($allowed_types_count == 1) {
    if ($allowed_types['username'] && !_twitterfield_is_username($value)) {
      $error = t('Only Usernames are allowed, and must be prepended with "@".');
    }
    elseif ($allowed_types['list'] && !_twitterfield_is_list($value)) {
      $error = t('Only Lists are allowed, and must be in the format "@username/list".');
    }
    elseif ($allowed_types['hashtag'] && !_twitterfield_is_hashtag($value)) {
      $error = t('Only Hashtags are allowed, and must be prepended with "#".');
    }
    elseif ($allowed_types['search'] && !_twitterfield_is_search($value)) {
      $error = t('Only search strings are allowed.');
    }
  }
  else {
    if (!$allowed_types['username'] && _twitterfield_is_username($value)) {
      $error = t('Usernames are not allowed.');
    }
    elseif (!$allowed_types['list'] && _twitterfield_is_list($value)) {
      $error = t('Lists are not allowed.');
    }
    elseif (!$allowed_types['hashtag'] && _twitterfield_is_hashtag($value)) {
      $error = t('Hashtags are not allowed.');
    }
    elseif (!$allowed_types['search'] && _twitterfield_is_search($value)) {
      $error = t('Search strings are not allowed.');
    }
  }
  if ($error) {
    form_error($element['value'], $error);
  }
}