Welcome Guest, Not a member yet? Register   Sign In
Problems loading validation rules in config file
#1

[eluser]built2fall[/eluser]
Hello, I'm new to CodeIgniter and very much liking it so far, however I've run into a problem. I'm having difficulty getting my validation rules to load automatically from file by associating a controller function with a rule group. Following the Saving Sets of Validation Rules to a Config File, I placed my form validation rules within a file named form_validation.php in application/configs. However, no validation errors are being printed out by validation_errors(). I have tried making a specific rule group and inserting as a parameter to form_validation->run(), but I get the same result. However, If I set the rules manually in the controller file using set_rules() it works properly. Anyone have any advice?

Here is my code:
Controller: controllers/signup.php
Code:
<?php
class Signup extends Controller {

    function __construct()
    {
        parent::__construct();
        $this->output->enable_profiler(TRUE);
        $this->load->helper('recaptcha');
    }

    function index() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $email    = $this->input->post('email');

        if ($this->form_validation->run() == FALSE)
        {
            $data = array('captcha_form' => recaptcha_get_html($this->config->item('public_key', 'recaptcha')));
            $this->load->view('auth/signup', $data);
        }
        else
        {
            // create new user account
        }
    }

}
?>

View: views/auth/signup.php
Code:
<?php doctype('xhtml1-strict'); ?>
<html >
<head>
    <title>Signup</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <meta name="robots" content="" />
    <link rel="stylesheet" href="<?php echo base_url(); ?>css/screen.css" media="screen,projection" charset="utf-8" />
</head>
<body>

<?php echo validation_errors(); ?>
    
<?php echo form_open('signup', array('id' => 'form-signup')) ?>
<fieldset>
<label for="username">Username</label>
&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" id="username" maxlength="15" tabindex="1" /&gt;

<label for="password">Password</label>
&lt;input type="password" name="password" id="password" maxlength="255" tabindex="2" /&gt;

<label for="email">Email</label>
&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" id="email" maxlength="255" tabindex="3" /&gt;

&lt;?php echo $captcha_form; ?&gt;

&lt;input type="submit" value="Create Account" id="create-acct-btn" tabindex="5" /&gt;
</fieldset>

&lt;?php echo form_close(); ?&gt;
    

&lt;/body&gt;
&lt;/html&gt;

Validation Rules: config/form_validation.php
Code:
&lt;?php
$config = array(
        'signup/index' => array(
                array(
                        'field' => 'username',
                        'label' => 'lang:username',
                        'rules' => 'trim|required|min_length[3]|max_length[15]|alpha_dash|unique_username|xss_clean'
                ),
                array(
                        'field' => 'password',
                        'label' => 'lang:password',
                        'rules' => 'trim|required|min_length[6]|max_length[255]|xss_clean'
                ),
                array(
                        'field' => 'email',
                        'label' => 'lang:email',
                        'rules' => 'trim|required|max_length[255]|valid_email|unique_email|xss_clean'
                ),
                array(
                        'field' => 'recaptcha_response_field',
                        'label' => 'lang:recaptcha',
                        'rules' => 'required|valid_captcha'
                )
        ),
        'login/index' => array(
                array(
                        'field' => 'login',
                        'label' => 'lang:login',
                        'rules' => 'trim|exists|xss_clean'
                ),
                array(
                        'field' => 'password',
                        'label' => 'lang:password',
                        'rules' => 'trim|xss_clean'
                ),
                array(
                        'field' => 'remember',
                        'label' => '',
                        'rules' => 'integer'
                )
        ),
        'account/forgot_password' => array(
                array(
                        'field' => 'login',
                        'label' => 'lang:login',
                        'rules' => 'trim|exists|xss_clean'
                )
        )
);
?&gt;
#2

[eluser]flaky[/eluser]
Code:
if ($this->form_validation->run('signup/index') == FALSE)
#3

[eluser]built2fall[/eluser]
EDIT: scratch that, still not working.
#4

[eluser]nkm82[/eluser]
Are you auto-loading the library? If not, you must explicitly load it at the controller.

Code:
$this->load->library('form_validation');
#5

[eluser]built2fall[/eluser]
It was being loaded in a custom library (which itself was auto loaded). Tried loading explicitly in the controller, same result. And just to reiterate it works if I explicitly set the rules in the controller:

Code:
$config = array(
                array(
                        'field' => 'username',
                        'label' => 'lang:username',
                        'rules' => 'trim|required|min_length[3]|max_length[15]|alpha_dash|unique_username|xss_clean'
                ),
                array(
                        'field' => 'password',
                        'label' => 'lang:password',
                        'rules' => 'trim|required|min_length[6]|max_length[255]|xss_clean'
                ),
                array(
                        'field' => 'email',
                        'label' => 'lang:email',
                        'rules' => 'trim|required|max_length[255]|valid_email|unique_email|xss_clean'
                ),
                array(
                        'field' => 'recaptcha_response_field',
                        'label' => 'lang:recaptcha',
                        'rules' => 'required|valid_captcha'
                )
        );
        $this->form_validation->set_rules($config);


EDIT: I guess I'll just settle for making the validation rules a config file and autoload it. Followed by adding the following line to index()
Code:
$this->form_validation->set_rules($this->config->item('signup', 'validation_rules'));
so it basically achieves the same thing I'm going for (having all the rules in one self-contained file). My ears are still open if anyone knows why the method mentioned in the user guide is not working.
#6

[eluser]ekevin[/eluser]
Hi,

I've just had the same problem, following this topic I put

Code:
$this->form_validation->run('[b]controller/login[/b]')


instead of

Code:
$this->form_validation->run('[b]accounts/login[/b]')

And it worked. Not sure about the logic of putting the word controller. What should I do if I had the two same name methods in two different classes ?

Ex:

Code:
// file a.php
class A extends Controller {
  //...
  function login() {
    // ...
    // check
    //
    if ($this->form_validation->run('controller/login') == FALSE)
    ...
  }
}

// file b.php
class B extends Controller {
  //...
  function login() {
    // ...
    // check
    $this->form_validation->run('controller/login')
    ...
  }
}
#7

[eluser]MurkeyDismal[/eluser]
I may be wrong, but shouldn't line 5 read
Code:
parent::Controller();
#8

[eluser]ekevin[/eluser]
@MurkeyDismal: you're not wrong Wink -> [url="http://ellislab.com/codeigniter/user-guide/general/controllers.html#constructors"]user guide (controller)[/url]
#9

[eluser]built2fall[/eluser]
[quote author="MurkeyDismal" date="1263769665"]I may be wrong, but shouldn't line 5 read
Code:
parent::Controller();
[/quote]

parent::__construct() and parent::Controller() are equivalent as far as I know--they both call the constructor of the parent class. If I'm not mistaken, the latter is simply used for backward compatibility with PHP4. http://us2.php.net/construct




Theme © iAndrew 2016 - Forum software by © MyBB