Unless I've missed something, there is no existing way to check a value entered is a valid URL.

This patch provides a simple test to see if a textfield contains a valid URL using the core valid_url function.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nicholasThompson’s picture

btw, This patch was made against HEAD.

svendecabooter’s picture

Status: Needs review » Needs work

Hi Nicholas,

Thanks for your patch! Sorry for the late feedback.
Wouldn't it be better though to use valid_url($val, TRUE), as it seems like a more widely used use case to validate external URLs...
I'm not sure how the regex without $absolute = TRUE works, but it validates practically everything when i test it (including one word text entries).
Do you have a specific use case in mind where $absolute would have to be FALSE?

svendecabooter’s picture

Updated patches in attachment, using $absolute = TRUE.
I'll have to figure out what the consequences of all of this are at some point.

svendecabooter’s picture

Status: Needs work » Needs review
jibran’s picture

According to patch and http://drupal.org/node/822146#comment-3080360 I put this in my module and it works fine for me,

/**
 * Implementation of hook_webform_validation_validators().
 */
function my_module_webform_validation_validators() {
  return array(
  	'valid_url' => array(
      'name' => "Valid URL",
      'component_types' => array(
        'textfield',
        'hidden',
        ),
      'description' => t("Validates that the user-entered data is a valid URL using Drupal's built in !link.", 
      array(
      	'!link' => l(t('valid_url'), 'http://api.drupal.org/api/drupal/includes--common.inc/function/valid_url')
      	
      )),
    ),
  );
}
/**
 * Implementation of hook_webform_validation_validate().
 */
function my_module_webform_validation_validate($validator_name, $items, $components, $rule) {
  if ($items) {
  	switch ($validator_name) {
      case "valid_url":
        foreach ($items as $key => $val) {
          if (!valid_url($val, TRUE)) {
            $errors[$key] = t('%item does not appear to be a valid URL.', array('%item' => $components[$key]['name']));
          }
        }
        return $errors;
        break;
    }
  }
}
Liam Morland’s picture

Version: 6.x-1.3 » 7.x-1.x-dev
Liam Morland’s picture

Status: Needs review » Fixed
FileSize
1.38 KB

Fixed in 1d058b3.

Liam Morland’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
Status: Fixed » Patch (to be ported)
theunraveler’s picture

I think it is probably best not to include links to Drupal's API documentation in user-facing help text. The end user does not really care which of Drupal's API functions is used to validate the URL, and including a link to developer-oriented documentation is just confusing. Here's a patch against the latest 7.x-1.x that removes this part from the help text.

Aside from that, this looks good.

Liam Morland’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Patch (to be ported) » Needs review
Liam Morland’s picture

Liam Morland’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
Status: Fixed » Patch (to be ported)
Liam Morland’s picture

Thanks, theunraveler.

Liam Morland’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Issue summary: View changes
Status: Patch (to be ported) » Closed (fixed)

Drupal 6 is no longer supported.