The Form Validation Library allows you to use callbacks as rules. A callback is simply a function in your Controller code that is used in place, or along with, a rule.
Say, for example, you want to add the user's e-mail address to the database if they haven't sent you an e-mail before. We can do this with a callback.
Firstly though, let's create the database table. There will be three fields: an ID, the user's name, and the user's e-mail address.
CREATE TABLE IF NOT EXISTS `user_data` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 255 ) NOT NULL , `email` VARCHAR( 255 ) NOT NULL );
To add a callback into the rules, simply prefix the name of the function with callback_
. Here is the rule for the e-mail field, again, with our callback added:
$this->form_validation-> set_rules('email', 'Email Address', 'required|valid_email|callback_add_user|xss_clean');
Here you can see that we've added the rule callback_add_user...