Search code examples
phpvalidationurlcakephp

Override URL validation rule to tolerate whitespaces at ends of URL


I would like to override the standard URL validation rule to make it more tolerant of a whitespace character before or after the URL. Basically use the trim() function on the url before passing it to the standard URL validation handler.

I know I need to override that rule but I'm not exactly where and how I need to do it.

(Plus, the CakePHP API and book documentation are currently offline. Upgrades, I know...)


Solution

  • Why would you wanna do that? Simply make sure all posted data is always trimmed. Thats cleaner and more secure, anyway.

    I have a component doing that in beforeFilter:

    /** DATA PREPARATION **/
    if (!empty($controller->data) && !Configure::read('DataPreparation.notrim')) {
        $controller->data = $this->trimDeep($controller->data);
    }
    

    The trimDeep method:

    /**
     * @static
     */
    function trimDeep($value) {
        $value = is_array($value) ? array_map(array(&$this, 'trimDeep'), $value) : trim($value);
        return $value;
    }