Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#5 | Absolute URLS validation-1460690-4.patch | 857 bytes | pedrofaria |
#3 | 1460690-fapi_validation-url-rule-accepts-relative-urls.patch | 922 bytes | barraponto |
#1 | full_url.patch | 694 bytes | Gidgidonihah |
external_url.patch | 1.03 KB | Gidgidonihah |
Comments
Comment #1
Gidgidonihah CreditAttribution: Gidgidonihah commentedI'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.
Comment #2
tr33m4n CreditAttribution: tr33m4n commentedExcellent, just what I was looking for
Cheers
Comment #3
barraponto CreditAttribution: barraponto commentedI'm not sure whether relative URL validation is actually useful. Nevertheless, I've written a patch that takes url[external] and validates external URLs.
Comment #4
pedrofaria CreditAttribution: pedrofaria commentedThank 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
Comment #5
pedrofaria CreditAttribution: pedrofaria commentedSorry, I forgot the patch! ouch!
Comment #6
barraponto CreditAttribution: barraponto commentedWorks fine.
Comment #7
pedrofaria CreditAttribution: pedrofaria commentedcommited! I will release a new version, soon!