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

Bake a comment




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

© daniel hofstetter. Licensed under a Creative Commons License