You are here

function _link_widget_validate in Link 5

1 call to _link_widget_validate()
link_widget in ./link.module
Implementation of hook_widget().

File

./link.module, line 466
Defines simple link field types.

Code

function _link_widget_validate(&$item, $delta, $field, $node, &$optional_field_found) {
  if ($item['url'] && !(isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url'] && !$field['required'])) {

    // Validate the link.
    if (link_validate_url(trim($item['url'])) == FALSE) {
      form_set_error($field['field_name'] . '][' . $delta . '][url', t('Not a valid URL.'));
    }

    // Require a title for the link if necessary.
    if ($field['title'] == 'required' && strlen(trim($item['title'])) == 0) {
      form_set_error($field['field_name'] . '][' . $delta . '][title', t('Titles are required for all links.'));
    }
  }

  // Require a link if we have a title.
  if ($field['url'] !== 'optional' && strlen($item['title']) > 0 && strlen(trim($item['url'])) == 0) {
    form_set_error($field['field_name'] . '][' . $delta . '][url', t('You cannot enter a title without a link url.'));
  }

  // In a totally bizzaro case, where URLs and titles are optional but the field is required, ensure there is at least one link.
  if ($field['url'] == 'optional' && $field['title'] == 'optional' && (strlen(trim($item['url'])) != 0 || strlen(trim($item['title'])) != 0)) {
    $optional_field_found = TRUE;
  }
}