AutoValidate v0.01 - El Forum - 07-18-2008
[eluser]Pygon[/eluser]
The more and more I played with Struts, the more I LOVED the idea of being able to automatically validate the input without needing to call functions, and being able to separate validation rules into files, as well as having a sort of tiered validation that allows validation at a controller or method level (or both).
As I toyed with a similar implementation for CI, I found that full automation wasn't the best solution, so I went for simplification:
NOTE: If you have not fixed index.php faux-absolute-junk it does for apppath/basepath, you will probably need to disable the security, and this hasn't exactly been tested in this way.
File: /application/libraries/Autovalidate.php
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Autovalidate {
var $CI;
var $_validation_dir;
var $_validation_suffix = "_validation";
var $_validation_security = TRUE;
function Autovalidate()
{
$this->CI = &get;_instance();
if( empty($this->CI->validation) ) $this->CI->load->library('validation');
$dir = $this->CI->config->item('validation_directory');
if(empty($dir)) $dir = "validation/";
$this->set_validation_dir($dir);
$security = $this->CI->config->item('validation_security');
if($security) $this->_validation_security = $security;
$suffix = $this->CI->config->item('validation_suffix');
if($suffix) $this->_validation_suffix = $suffix;
log_message('debug', "Validation Class Initialized");
}
function set_validation_dir($dir,$append=false)
{
if(empty($dir)) return;
$dir = str_replace('\\','/',$dir);
$dir = $append ? realpath($this->_validation_dir . $dir) : realpath(APPPATH . $dir);
$this->_validation_dir = str_replace('\\','/',$dir);
if(empty($this->_validation_dir) || !is_dir($this->_validation_dir)){
show_error("Validation Error: The setting for validation directory is incorrect or the directory is missing.");
} else if($this->_validation_security && strstr($this->_validation_dir,APPPATH) === FALSE) {
show_error("Validation Security: This validation directory is incorrect or outside the application directory.");
}
}
function validate_input($controller, $method=null){
//Try base controller validation
if( $this->_load_validation($controller . $this->_validation_suffix . EXT) === FALSE ) return false;
//Try method specific validation
if( !empty($method) && $this->_load_validation($controller.'_'.$method . $this->_validation_suffix . EXT) === FALSE ) return false;
return true;
}
function _load_validation($file)
{
if( file_exists($this->_validation_dir.$file) ){
include_once($this->_validation_dir.$file);
if(!empty($rules)) $this->CI->validation->set_rules($rules);
if(!empty($fields)) $this->CI->validation->set_fields($fields);
return $this->CI->validation->run();
}
return true;
}
}
/application/config/config.php (optional)
Code: /*
|--------------------------------------------------------------------------
| Validation Directory
|--------------------------------------------------------------------------
|
| This is the directory (in the application folder) to locate the validation
| files in. Should end with a '/'.
|
*/
$config['validation_directory'] = 'validation/';
/*
|--------------------------------------------------------------------------
| Validation Security
|--------------------------------------------------------------------------
|
| This flag enables or disables the validation security which verifies
| that the directory for validation files is not outside the application
| directory.
|
*/
$config['validation_security'] = TRUE;
/*
|--------------------------------------------------------------------------
| Validation File Suffix
|--------------------------------------------------------------------------
|
| This sets the suffix of the files that will be used for validation
| (in case you choose to put these files inside your controller directory).
|
*/
$config['validation_suffix'] = "_validation";
set_validation_dir - sets the directory to use for validation files. Useful if you want to set a different directory for a controller or method. Setting the second parameter to TRUE will append the directory to the existing.
Example:
Code: set_validation_dir("validate/");
// equates to path/to/application/validate/
set_validation_dir("controller/",TRUE);
//equates to path/to/application/validate/controller/
validate_input - takes two parameters (controller name, method name [optional]). Calling this will automatically load the validation files and run the validation. First "controller" validation will be loaded, then "controller_method" validation if passed. Returns TRUE if passed, FALSE if failed. Errors are available in the standard fashion by calling $this->validation library (which is autoloaded if it does not exist when loading autovalidate).
Example Usage (default configuration)
Code: // application/controller/test.php
//a standard CI controller with autovalidate loaded.
function submit()
{
if(!$this->autovalidate->validate_input('test','submit')){
//test_form.html is a simple form with a text field for submission
$this->load->view('test_form.html');
echo 'Failed Validation.<br />';
return;
}
echo 'Passed!';
}
// application/validation/test_validation.php
$rules['testing'] = "min_length[5]|max_length[128]";
$fields['testing'] = 'Testing';
// application/validation/test_submit_validation.php
$rules['testing'] = "required";
AutoValidate v0.01 - El Forum - 07-18-2008
[eluser]Pygon[/eluser]
But, why?
The automation of the validation allows you to avoid writing validation rules in each of your controllers. Also, if you have a default set of validation rules based on the input name, you can avoid re-writing the rules:
Code: // application/validation/default_validation.php
$rules['username'] = "min_length[5]|max_length[12]";
$rules['password'] = "min_length[6]|max_length[10]";
$rules['passconf'] = "min_length[6]|max_length[10]|matches[password]";
$rules['email'] = "required|valid_email";
By calling validate_input('default') first, you can reuse these validation rules in any controller, simply adding controller_method_validation.php for the methods that need to set certain fields as required for that method, creating a normalized input throughout your website.
Please let me know if you have any problems.
|