1 link.module link_field_element_validate(&$element, &$form_state, $complete_form)

Link field element validation.

File

core/modules/link/link.module, line 951
Defines simple link field types.

Code

function link_field_element_validate(&$element, &$form_state, $complete_form) {
  $value = $element['#value'];
  // Special validations when the values are not the default and when url has a
  // value. Sub-elements marked as 'required' are validated by core.
  if (!empty($value['url'])) {
    // Validate the URL.
    if (!empty($element['#link_field_options']['validate_url'])) {
      $url_valid = link_validate_url(trim($value['url']));
      if ($url_valid === FALSE) {
        form_error($element['url'], t('The value %value provided for %field is not a valid URL.', array(
          '%value' => trim($value['url']),
          '%field' => $element['#title'],
        )));
      }
    }

    // Require title if title_mode == required.
    if ($element['#link_field_options']['title_mode'] == 'required' && strlen(trim($value['title'])) == 0) {
      form_error($element['title'], t('Titles are required for all links.'));
    }

    // Check the submitted link type matches the required link_type.
    if ($element['#link_field_options']['link_type'] != LINK_BOTH) {
      $url_type = link_validate_url(trim($value['url']));
      if (($element['#link_field_options']['link_type'] == LINK_INTERNAL && $url_type === LINK_EXTERNAL) || ($element['#link_field_options']['link_type'] == LINK_EXTERNAL && in_array($url_type, array(LINK_INTERNAL, LINK_FRONT), TRUE))) {
        $field_label = array('%field' => $element['#title']);
        $error = ($element['#link_field_options']['link_type'] == LINK_INTERNAL) ? t('Only internal URLs are allowed in %field.', $field_label) : t('Only external URLs are allowed in %field.', $field_label);
        form_error($element['url'], $error);
      }
    }
  }

  // Require a link if we have a title.
  if ($element['#link_field_options']['url_mode'] !== 'optional' && strlen(isset($value['title']) ? $value['title'] : '') > 0 && strlen(trim($value['url'])) == 0) {
    form_error($element['title'], t('You cannot enter a title without a link url.'));
  }

  // Prevent unexpected data from being placed in the attributes column.
  if (isset($value['attributes']) && is_string($value['attributes'])) {
    form_error($element, t('String values are not acceptable for attributes.'));
  }
}