Welcome Guest, Not a member yet? Register   Sign In
Form validation and form_validation.php
#1

In the  How to Save Your Rules section of the Form Validation documentation it states

Quote:Your validation rule file will be loaded automatically and used when you call the run() method.

The "validation rule file" is, as I'm sure you all know: application/config/form_validation.php

But I'm not finding how or where this loading occurs. I'm probably suffering from male pattern blindness but I cannot find it. Does anyone know where in the core source code this happens?
Reply
#2

(10-11-2017, 10:26 AM)dave friend Wrote: In the  How to Save Your Rules section of the Form Validation documentation it states

Quote:Your validation rule file will be loaded automatically and used when you call the run() method.

The "validation rule file" is, as I'm sure you all know: application/config/form_validation.php

But I'm not finding how or where this loading occurs. I'm probably suffering from male pattern blindness but I cannot find it. Does anyone know where in the core source code this happens?

make the file yourself and load it maybe.

To store your validation rules, simply create a file named form_validation.php in your application/config/ folder.
Reply
#3

Using config/form_validation.php should only be done for sites that are expected to be super small. If the site isn't small, you'll quickly outgrow the single file. I made a dir in /config called "form_validation" and put my files in there. When I need it, I load it just like any other config file, and then add the rules with:


PHP Code:
$this->form_validation->set_rulesconfig_item('this_specific_set_of_rules') ); 
Reply
#4

@skunkbad
That is a really good idea. On a recent site I decided to be really strict with myself and started putting all the validation rules into the normal form_validation config file. It soon became so huge I was worried that loading the entire array just to get a couple of elements was starting to be an issue, so put all the validation rules back into the libraries.

However, doing as you suggest here is a great idea. Will try it out. Thank you.

Paul.
Reply
#5

(10-12-2017, 05:01 AM)PaulD Wrote: On a recent site I decided to be really strict with myself and started putting all the validation rules into the normal form_validation config file.

Implying that the config file is something like the officially proper way to do it?
It's not. Don't fall into that trap.
Reply
#6

My question (restated) was:

How does the Form_validaion library automatically find and load the configuration file form_validation.php?

The question was prompted by curiosity rather than any need to actually accomplish some development task.

The answer, if anyone is interested, is that it happens during the library's instantiation within the loader class. The protected function _ci_init_library() handles the task of instantiating the requested library. Before new is called however, all the likely config folders are checked for a file with the same name as the library being loaded.

When it finds a file (on any of the possible paths) it is included. A config file must define and set the var $config. When set, $config is passed to the class constructor, i.e. new library_name($config).

I should have recalled that several other core libraries use the "library_name.php" config file pattern e.g. Email and Upload. If I had been less brain-dead it wouldn't have taken me so long to look in the loader class.

This config autoload behaviour is something to keep in mind because it is potentially useful in custom classes.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB