Set a form_validation rule from a library in CodeIgniter (PHP)


Ok so i've got a big problem with <strong>CodeIgniter flexibility</strong>. I've many personal rules (like 20) targeting an input in a form within a huge project.

Everything work while I use the classic "callback_" system by CodeIgniter. I just put the methods in the same class than the form check and it checks it correctly.

My problem is :

1) I'd like to use all these rules in another form_validation in another controller without copy/paste the code ; <strong>we all know it's dirty/evil</strong>.

2) Ideally, i'd appreciate to put these rules in a big library, because it takes something like 800 lines and this is not a good idea to let it be in my controller ; as I said this project is quite huge.

It's 6 hours i'm looking for a solution and there's absolutely <strong>nothing clean</strong> :


I already have a <strong>MY_Form_Validation</strong> to put some general rules but i don't like the idea to mix my specific rules in a global class which will call it everytime vainly. Plus these rules contain many libraries, models, native CI core methods such as $this->input->post() which generate errors when I put everything in this MY_Form_Validation. Not the good solution :(

</li> <li>

I created a <strong>MY_Controller</strong> including a method named 'imports' which re-generate selected libraries methods within the controller (in PHP4 it was kind of the 'aggregate_methods' function if people are curious) ; the system works perfectly but CodeIgniter doesn't understand it. The methods can be called within the controller but it seems the framework check the $CI content to call the rules (Form_validation.php line 590 in '/system/') so it doesn't work at the end ; it's also hard to modify this core part, I prefered not touching it and gave up.

</li> </ul><blockquote>

$this->load->library('volt/lbl_validation'); $this->imports('Lbl_validation'); // Then you can call any $this->lbl_validation->method() with $this->method() in the controller

</blockquote> <ul><li>I tried to hack CI creating a customized form_validation within my library ('lbl_validation') ; the system was a bit messy but worked. The problem is when i came back to the CI form_validation system to show error messages, it was a true spaghetti-code which wasn't working that well. <strong>Not the good solution either</strong>.</li> </ul>

I also tried some other shitty solutions but i prefer not confess it.

Now i'm here in front of my computer asking myself <strong>why bad things happened to good people</strong>, why this is so hard to separate set_rules from the called methods in CodeIgniter, why they didn't plan ahead people could've needed to call libraries methods as rules. I don't know what to do and i'm hesitating to put a dumb require() somewhere and make it all dirty and messy like my desk right now.

Maybe, there's someone with a good dans clean solution. All my hope are turned to the StackOverFlow community ; someone ? A crazy CI geek ?

Thank you ;)


The only good, DRY way to handle validation is to put validation rules at the last resort before saving to the the database, in other words in the models. By doing this, the same rules can be used in any controller or library without being redefined.

The following idea is taken from Jamie Rumbelows excellent Codeigniter handbook:

Simply create an array in your model:

$validate = array( array( 'field' => 'username', 'label' => 'Username', 'rules' => 'required,trim' ), array( 'field' => 'password', 'label' => 'Password', 'rules' => 'required|min_length[8]' ) );

Then implement a method that you can use to validate your data prior to save()

function validate($data) { if (!empty($this->validate)) { foreach ($data as $key => $value) { $_POST[$key] = $value; } $this->load->library('form_validation'); $this->form_validation->set_rules($this->validate); return $this->form_validation->run(); } else { return TRUE; } }

Now, in your controllers you can use:

if ($this->user->validate($user)) save...


  • changing password in php/codeigniter
  • base controller and apply it to all existing controller
  • How to get the class from which a request originates in codeigniter
  • The android java class cannot recognize the R file
  • how to avoid the dependencies hell with unit test in angular 2+
  • Perl function name clash
  • JSF NumberFormatException with f:setPropertyActionListener
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • How to return DataSet (xsd) in WCF
  • Suppressing passwd when calling sqlplus from shell script
  • SetWindowsHookEx does not react on media keys
  • How to revert to previous XCode version?
  • Not able to aggregate on nested fields in elasticsearch
  • Django simple Captcha “No module named fields” error
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Why does access(2) check for real and not effective UID?
  • jQuery .attr() and value
  • Spring security and special characters
  • Azure Cloud Service Web Role web pages do not load
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Incrementing object id automatically JS constructor (static method and variable)
  • Where to put my custom functions in Wordpress?
  • Trying to switch camera back to front but getting exception
  • Numpy divide by zero. Why?
  • Revoking OAuth Access Token Results in 404 Not Found
  • AngularJs get employee from factory
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Why joiner is not used after Sequence generator or Update statergy
  • Memory offsets in inline assembly
  • Turn off referential integrity in Derby? is it possible?
  • unknown Exception android
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize