Tuesday, October 1, 2013

Valid URL Format Checking in CodeIgniter 2.x Using custom/extended Form Valdiation Library

CodeIgniter lacks with URL validation. The only one that it provides is called prep_url which does nothing more then prepend "http://" to URLs if missing.

Create a file in the application/library directory called MY_Form_validation.php. Make the class name MY_Form_validation as well. Be sure that it extends CI_Form_validation, and that it calls the parent constructor. This will allow you to extend the built-in validation library and add additional validators to it.
Then in your code, you can simply add additional validators like:
$rules['URL']  = 'required|prep_url|valid_url_format|url_exists';

The two new validators are valid_url_format which checks for a properly formatted URL and url_exists which checks for a valid server. You should prefix the two with prep_url so that if they don’t enter a scheme the other validators won’t fail.
if (!defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Additional validations for URL format.
 *
 * @package      Module Creator
 * @subpackage  ThirdParty
 * @category    Libraries
 * @author  Anup Shakya 
 * @created 01/10/2013
 */
 
class MY_Form_validation extends CI_Form_validation{
     
   public function __construct()
   {
     parent::__construct();
    }  
                         
    /**
     * Validate URL format
     *
     * @access  public
     * @param   string
     * @return  string
     */
    function valid_url_format($str){
        $pattern = "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";
        if (!preg_match($pattern, $str)){
            $this->set_message('valid_url_format', 'The URL you entered is not correctly formatted.');
            return FALSE;
        }
 
        return TRUE;
    }       
 
    // --------------------------------------------------------------------
     
 
    /**
     * Validates that a URL is accessible. Also takes ports into consideration. 
     * Note: If you see "php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known" 
     *          then you are having DNS resolution issues and need to fix Apache
     *
     * @access  public
     * @param   string
     * @return  string
     */
    function url_exists($url){                                   
        $url_data = parse_url($url); // scheme, host, port, path, query
        if(!fsockopen($url_data['host'], isset($url_data['port']) ? $url_data['port'] : 80)){
            $this->set_message('url_exists', 'The URL you entered is not accessible.');
            return FALSE;
        }               
         
        return TRUE;
    }  
}
// END Form Validation Class

/* End of file My_Form_validation.php */
/* Location: ./application/libraries/My_Form_validation.php */
In this way, you can extend the Form_validation library without touching core library. Ref: http://brianistech.wordpress.com/2010/11/19/url-validation-in-codeigniter/

2 comments: