Welcome Guest, Not a member yet? Register   Sign In
Form validation rule set in config file
#1

[eluser]jekorma[/eluser]
Hello.

I know this has been discussed before here and there but here goes:

The official CI documentation for using config based rule sets states:

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

the run function of course being
Code:
$this->form_validation->run()

the documentation also states:

Quote:In order to call a specific group you will pass its name to the run() function. For example, to call the signup rule you will do this:
Code:
if ($this->form_validation->run('signup') == FALSE) ... etc

As far as I can see, this is simply not the case. First of all, the config file (config/form_validation.php) does NOT get auto loaded and must be manually set to load in autoload.php or in the controller.

Second of all the documentation also fails to explain that rules have to be set like this:

Code:
$this->form_validation->set_rules($this->config->config['rule_set_name']);

and then simply run in the usual manner:

Code:
if ($this->form_validation->run() == FALSE) ... etc

this being done WITHOUT passing the rule set name parameter to run().

Can anyone tell me if they have got it working using the officially documented way?

Regards,

Jesper.
#2

[eluser]Kindari[/eluser]
I have it working exactly as described in the official docs.

Here is my config/form_validation.php:

Code:
<?php


$config['create_user'] = array(
    array(
          'field' => 'username',
          'label' => 'Username',
          'rules' => 'trim|required|min_length[3]|max_length[40]',
          ),
    array(
          'field' => 'confirm_password',
          'label' => 'Password',
          'rules' => 'trim|required',
          ),
    array(
          'field' => 'password',
          'label' => 'Password',
          'rules' => 'trim|required|matches[confirm_password]',
          ),
);

Then My Controller:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends Controller {
    function create() {
        $this->load->library('form_validation');

        if ($this->form_validation->run('create_user')) {
            echo 'yay';
        } else {
            echo 'noooo';
        }
    }
}
#3

[eluser]jekorma[/eluser]
[SOLVED]

Thank you for your reply. Knowing that it worked for you I knew something had to be wrong in my end. The following explanation may be an obvious no-brainer to some out there, but I still think it's worth posting here.

It seems the problem was the MY_Form_validation library I was using. When system/libraries/Loader.php instantiates the form_validation class it happens this way:

Code:
// Instantiate the class        
$CI =& get_instance();
if ($config !== NULL)  // My form_validation config file WAS being loaded, but...
{
     $CI->$classvar = new $name($config);

// ... the above line would translate into...
// $CI->form_validation = new MY_Form_Validation($config)        

}
else ....


and my MY_Form_validation constructor looked like this:

Code:
function MY_Form_validation()
    {
        parent::CI_Form_validation();
                
    }

// after comparing with the parent form_validation.php constructor that begins like this:

    function CI_Form_validation($rules = array())
    {    
        $this->CI =& get_instance();
        
        // Validation rules can be stored in a config file.
        $this->_config_rules = $rules;
  //......etc

// I could see that it was set to receive the config array via the $rules parameter,
// which MY_Form_validation() was not.

solution:

Code:
// change the MY_Form_validation.php constructor to this

    function MY_Form_validation($rules = array())
    {
        parent::CI_Form_validation();
        
        $this->_config_rules = $rules;
        
    }

The official documentation on how to extend libraries does not mention this, which I should think (unless I am missing something), is a pretty substantial omission.


Regards.
#4

[eluser]Kindari[/eluser]
Thanks for posting your results, I will have to look for things like that in the future. Glad to hear its working now.




Theme © iAndrew 2016 - Forum software by © MyBB