CodeIgniter

CodeIgniter Hack Form_Validation Callbacks

When im writting a Codeigniter 2 application I prefer to have most of my code in the libraries and leave the controller as blank as possible. This allows me to reuse code like forms in other sections of the site. eg you want the login for to be in 2 different places. However Codeigniter’s Form_valiation class does not allow for custom validation callbacks in a library. The only way to fix this is to hack the Form_validation class. I have created a modification that allows you to call a validation callback from any class.

Getting a Codeigniter Query Source

Using CodeIgniter’s framework for query binding is great but can make it hard to debug problems in your SQL statement. The solutions I have come up with is to use the compile_binds function. This will output your SQL statement after the parameters have been cleaned and added.

var_dump($this->db->compile_binds($sql, array($param1, $param2)));

CodeIgniter Extending Form_Validation

You can add generic validation functions to codeigniter by extending the Form_Validation. You do this by creating a file called /application/library/MY_Form_Validation.php.

If you load the Form_Validation library it will pick-up this class and use it instead. Because it extends the original class CI_Form_validation you still have access to all the normal functions.

Here is an example that adds 2 validation functions: