-
Notifications
You must be signed in to change notification settings - Fork 26
MY Validation Callbacks into Models
Category:Libraries::Validation | Category:Libraries::Community
Setting Up: [code] $this->validation->set_rules(array( 'username' => 'trim|required|callback_users_model->is_unique[username]', 'password' => 'trim|required|matches[confirm]', ));
[/code]
The callback function in users_model [code] /** * Validation callback **/ function is_unique($value, $field) { $this->validation->set_message('users_model->is_unique', "The %s {$value} is not available. Try a different username"); return (bool)(!$this->findBy("{$field} = '{$value}'")); }
[/code]
This is a more advanced version of the previous callbacks into models extension. You may pass the calling object to the Validation class. ie: $this->validation->run($this); If not it wil use the CI super object by default.
application/libraries/MY_Validation.php [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
- MY_Validation extension
- Allows callback functions into Models
- Usage:
- Pass the caller to the validation class
- $this->validation->run($this);
- Version 0.3 (c) Wiredesignz 2008-04-24 */
class MY_Validation extends CI_Validation { function get_fields() { return $this->_fields; }
/**
* Run the Validator
*
* This function does all the work.
*
* @access public
* @return bool
*/
function run(&$parent = NULL)
{
// Do we even have any data to process? Hmm?
if (count($_POST) == 0 OR count($this->_rules) == 0)
{
return FALSE;
}
isset($parent) OR $parent = $this->CI;