The URL validation, using valid_url, passes on anything that could be an internal url (ie 'foobar') because no second param is passed to the valid_url function. This is fine if you want these to be valid. If however, you want to validate full urls, this is bad.

I've added a 2nd validation type called external_url that will require a full url for validation. While I was in there, I also corrected an error message. I've attached a patch.

There may be a better way to do this (passing a param on url), but this is the quickest way I could get it done.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Gidgidonihah’s picture

FileSize
694 bytes

I've written another patch with a different way of doing this. This would not add a new validation type, but you would pass a param to the url validation type.

Using '#rules' => array('url') would validate any internal url. Works the same as it does now.

Using '#rules' => array('url[full]') would require a full url to validate. With the way I've written the patch all that matters is that [something] appears after url. This could be hardened so it only works if full is passed.

Personally I like this method better. Additionally, it's backwards compatible. Meaning I applied the patch to the module in my environment and it works great. However if the module is updated and this goes away, I don't get errors, it just falls back to the current way of validating urls.

tr33m4n’s picture

Excellent, just what I was looking for

Cheers

barraponto’s picture

Title: url passes on anything that could be a relative url » URL rule accepts relative URLs
Version: 7.x-1.0 » 7.x-1.x-dev
FileSize
922 bytes

I'm not sure whether relative URL validation is actually useful. Nevertheless, I've written a patch that takes url[external] and validates external URLs.

pedrofaria’s picture

Thank you guys,

I have used both approaches and create a new patch.

1) I have changed error message as Gidgidonihah suggested.
2) Looking at valid_url doc, I found that they use absolute nomenclature instead of full or external.

please, take a look and tell me if OK for you.

So, I will commit to repo and make a new release!

Thanks in advance,

Pedro

pedrofaria’s picture

Sorry, I forgot the patch! ouch!

barraponto’s picture

Status: Needs review » Reviewed & tested by the community

Works fine.

pedrofaria’s picture

Assigned: Unassigned » pedrofaria
Status: Reviewed & tested by the community » Closed (fixed)

commited! I will release a new version, soon!