Validation of local urls

Published on and tagged with cakephp  validation

While working on NoseRub I noticed (once again) that the validation of urls in the form of “http://somename.localhost” fails when using CakePHP’s built-in url validation rule.

To make such urls pass the validation we have to write a custom validation rule. As PHP (>= 5.2.0) already provides a filter for urls we can use this filter in our validation rule. And so the code in our model looks like:

public $validate = array('url' => array('rule' => array('validateUrl')));

public function validateUrl($data) {
    return (filter_var($data['url'], FILTER_VALIDATE_URL)) ? true : false;

I hope this workaround is useful for some of you.

PS: If you are a German-speaking baker, a new blog called cakery (started by Siegfried Hirsch, the admin of the German CakePHP Google Group) could be interesting for you.

4 comments baked

  • Mark Story

    hey daniel, just a heads up, many of the links on are broken.

    all aren’t working for me.

  • Siegfried

    HeyDaniel, thanks a lot for linking. My openID questions are not forgotten but a little bit postponed. Will contact you again.

  • David Thalmann

    Never heard of the function filter_var() function, thanx for your post! But just saw this:

    “It seems that all FILTER_VALIDATE_URL is doing is calling parse_url(), which makes it effectively useless since parse_url() only fails on really malformed urls.

    $url = 'http://...';
    var_dump(filter_var($url, FILTER_VALIDATE_URL));

    Will display: string(10) “http://…”

    None of the flags help either, so you’re better off with regular expressions to validate a url.” From

  • cakebaker

    @all: Thanks for your comments!

    @Mark: It seems like Dirk already fixed the links, at least now they work ;-) Only the NoseRub blog is currently offline as it got “hacked” yesterday… It will be back soon. Anyway, thanks for the heads up!

    @Siegfried: No problem, it is good to have a German CakePHP blog :)

    @David: Thanks for this hint. I think the “problem” is that something like http://. is in fact a valid url. So in practice you probably have to perform some additional checks to ensure a stricter url format…

  • bahis siteleri

    Look there for more:

    […]Excellent blog here! Also your site so much up fast![…]

  • Acemlesme

    …Check this out

    […]I am not positive where you are getting your info, however good topic.[…]

  • bahis siteleri

    …Check this out

    […]you made running a blog glance[…]

  • sportotobet

    …Take a look for more Information on that topic

    […] What host are you the use of? Can I am getting your associate link for your host? I desire my website loaded up as fast as yours lol[…]

  • Google


    Usually posts some pretty interesting stuff like this. If you are new to this site.

  • Google


    Usually posts some extremely fascinating stuff like this. If you are new to this site.

  • mp3 songs

    mp3 songs

    […]here are some hyperlinks to websites that we link to for the reason that we assume they are worth visiting[…]

  • aflampornhd


    […]Wonderful story, reckoned we could combine a couple of unrelated information, nevertheless seriously really worth taking a appear, whoa did a single study about Mid East has got additional problerms at the same time […]

  • adult tube websites

    adult tube websites

    […]Wonderful story, reckoned we could combine several unrelated information, nevertheless really worth taking a look, whoa did 1 find out about Mid East has got far more problerms as well […]

  • djswoope


    […]the time to read or take a look at the content material or websites we have linked to below the[…]

  • hawaii professional inspections

    hawaii professional inspections

    […]usually posts some incredibly exciting stuff like this. If you’re new to this site[…]

  • home real estate inspections hawaii

    home real estate inspections hawaii

    […]we prefer to honor quite a few other internet sites around the net, even if they aren’t linked to us, by linking to them. Beneath are some webpages really worth checking out[…]

  • iPad


    […]Sites of interest we have a link to[…]

  • sexy and elegant lingerie

    sexy and elegant lingerie

    […]please take a look at the web-sites we comply with, like this 1, because it represents our picks in the web[…]

  • hawaii property Inspection

    hawaii property Inspection

    […]that would be the end of this article. Here you will come across some websites that we think you will value, just click the hyperlinks over[…]

  • video production services

    video production services

    […]we like to honor lots of other net sites on the internet, even though they aren’t linked to us, by linking to them. Under are some webpages really worth checking out[…]

  • w88


    […]Here is an excellent Weblog You might Find Intriguing that we Encourage You[…]

Bake a comment

(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License