Welcome Guest, Not a member yet? Register   Sign In
Having tough time with validation_errors
#1

[eluser]mr_prasanna[/eluser]
Hi,

I'm using the form_validation library for validating form. Everything with it works fine except the validation_errors(). Is there something I am missing? Please help...really stuck with it.

Loading:
Code:
$this->load->library('validation', 'form_validation');

Request Handler (part of a controller):
Code:
function update($_user_id)
    {
        $update_status = array();
        $this->load->model('UserModel');

        $user_data = $this->UserModel->get_user($_user_id);

        $v_rules['user_password'] = 'trim|min_length[4]|xss_clean';
        $v_rules['user_password_new'] = 'trim|min_length[4]|xss_clean';
        $v_rules['user_firstname'] = 'trim|required|xss_clean';
        $v_rules['user_lastname'] = 'trim|required|xss_clean';
        $v_rules['user_email'] = 'trim|required|email|xss_clean';

        $this->validation->set_rules($v_rules);

        $v_fields['user_password'] = 'Current Password';
        $v_fields['user_password_new'] = 'New Password';
        $v_fields['user_firstname'] = 'Firstname';
        $v_fields['user_lastname'] = 'Lastname';
        $v_fields['user_email'] = 'E-Mail Address';

        $this->validation->set_fields($v_fields);
        $this->validation->set_error_delimiters('<span class="inlineErr">', '</span><br />');
        
        if( $this->validation->run() == FALSE ) {
            echo 'outside';
            echo $data['validation_errors'] = validation_errors('<div class="inlineErr">', '</div>');
        } else {
            echo 'inside';
            $data['validation_errors'] = 'jg';
        }

        $this->load->view('update_user_status', $data);
    }


Also, is there any way to load the 'form_validation' library as 'validation' from autoload?
#2

[eluser]WebsiteDuck[/eluser]
I think this is wrong:
Code:
$this->load->library('validation', 'form_validation');

What you might be trying to do is this:
Code:
$this->load->library('form_validation', NULL, 'validation');

What you probably should do is this:
Code:
$this->load->library('form_validation');
#3

[eluser]mr_prasanna[/eluser]
I changed the way I was loading to this:
Code:
$this->load->library('form_validation', NULL, 'validation');

I guess I had to change the way I was setting rules. Not sure how User Guide pointed me to a wrong place. Anyways, it works now.

The Only thing I'm still not sure is how to do the following from config/autoload.php
Code:
$this->load->library('form_validation', NULL, 'validation');
#4

[eluser]danmontgomery[/eluser]
There's an example in config/autoload.php how to do that

Code:
/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your system/application/libraries folder.
|
| Prototype:
|
|    $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

Just add 'form_validation' to the array of libraries you're autoloading
#5

[eluser]mr_prasanna[/eluser]
I know this. But, I want to alias the library. I want to auto load 'form_validation' and make it available in all controllers as "$this->validation" instead of long "$this->form_validation"
#6

[eluser]bretticus[/eluser]
You can't alias this in the autoload config (at least without hacking the core which is just dumb.) Here's a hacky silly work around that *may* work.

Create a file in your libraries folder under application. Call it validation.php. Create an empty class that inherits everything from the form validation class.

Code:
class Validation extends CI_Form_validation {

}

Now you should be able to reference 'validation' from the autoload.php config file.

NOTE: This is untested, so give it a whirl. Wink
#7

[eluser]mr_prasanna[/eluser]
Ah! Ok. Hope it wouldn't clash with the old validation or maybe I should simply remove the old version.

Will give it a try.
#8

[eluser]mr_prasanna[/eluser]
Error:
Fatal error: Class 'CI_Form_validation' not found in D:\wamp\www\_central\system\application\libraries\validation.php on line 2

Code:
&lt;?php
class Validation extends CI_Form_validation {
    
}
#9

[eluser]WebsiteDuck[/eluser]
You'll have to autoload the Form_validation class before you can extend it... and even then it may clash with the old validation class.

This whole thing doesn't seem like a good idea...just to save a few keystrokes. It will certainly confuse anyone else that may work on your code.
#10

[eluser]mr_prasanna[/eluser]
I'm considering removing the old Validation.php completely and renaming the Form_validation to Validation.

For now, loading it from the corresponding controller's constructor will do the above after UAT is over. Not the time for risks Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB