Welcome Guest, Not a member yet? Register   Sign In
Validation headaches
#1

[eluser]codex[/eluser]
Hello,

I bet I'm not the first to have this kind of trouble as a noob, but I'm puzzled about how to do validation properly using the validation class. I've searched the docs and forum, but it still isn't exactly clear to me. Hope you guys can help!

Basically I want a user to change his password. Therefore he has to enter the old pass, the new and a new retype. Pretty basic stuff (in plain old php I can do it with no probs). The input of the old password is checked against the DB. Now, if the password doesn't exist, aan error should be thrown. But I can't seem to get the validation working.

This is what I have so far:

Controller: settings.php
Code:
<?php
class Settings extends Controller {

    function Settings()
    {
        parent::Controller();
        setlocale (LC_ALL, "nl_NL");
        $this->load->model('mailbox_model');
        $this->load->model('settings_model');
        $_SESSION['uid'] = 1000;
    }
      
    function password()
    {    
        $this->load->library('validation');
                
            $rules['old_password']            = "required";
            $rules['new_password']            = "required";
            $rules['new_password_retype']     = "required|matches[new_password]";
            $this->validation->set_rules($rules);
            
        $old = $this->input->post('old_password');
        
        if ($this->settings_model->check_pass($old)) {
            // ERROR HERE I GUESS
        }
                
        $this->validation->set_error_delimiters('<p class="alert">', '</p>');    
        
        if ($this->validation->run() == FALSE)
        {
            $data['title']             = "Wachtwoord";
            $template['content']    = $this->load->view('settings/password_view', $data, true);
            $this->load->view('main_template', $template);
        }
        else {
            $this->load->view('formsuccess');
        }
    }
    
}
?&gt;


Model: settings_model.php
Code:
&lt;?php
class Settings_model extends Model {

    function Settings_model()
    {
        parent::Model();
    }

    function check_pass($old_pass)
    {
        $query = $this->db->query("    SELECT user_password
                            FROM users
                            WHERE user_password ='". md5($old_pass) ."'
                            AND user_id = '". $_SESSION['uid'] ."' ");
        
        if($query->num_rows() > 0) {
            return true;
        }
    }
}
?&gt;


View: password_view.php
Code:
&lt;?=$this->validation->error_string; ?&gt;
&lt;form method="post" name="edit_password" class="pt-20"&gt;
<label for="old_password" class="bold">Je oude wachtwoord</label>
<p class="pb-5">&lt;?php echo input_password("old_password", 30, "input_text", $this->validation->old_password)?&gt;</p>
<label for="new_password" class="bold">Nieuwe wachtwoord</label>
<p class="pb-5">&lt;?php echo input_password("new_password", 30, "input_text", $this->validation->new_password)?&gt;</p>
<label for="new_password_retype" class="bold">Nieuwe wachtwoord nogmaals</label>
<p class="pb-5">&lt;?php echo input_password("new_password_retype", 30, "input_text", $this->validation->new_password_retype)?&gt;</p>
<p>&lt;?php echo input_submit("edit_password", "input_submit", "Wachtwoord opslaan")?&gt;</p>
&lt;/form&gt;

Any help is greatly appreciated!!
#2

[eluser]codex[/eluser]
Or the 'callback' function is what I'm looking for...
#3

[eluser]codex[/eluser]
But still, this doesn't work for me. Am I missing something?


Code:
function password()
    {    
        $this->load->library('validation');
                
            $rules['old_password']            = "callback_password_check";
            $rules['new_password']            = "required";
            $rules['new_password_retype']    = "required|matches[new_password]";
            $this->validation->set_rules($rules);
            
        $this->validation->set_error_delimiters('<p class="alert">', '</p>');    
        
        if ($this->validation->run() == FALSE)
        {
            $data['title']             = "Wachtwoord";
            $template['content']    = $this->load->view('settings/password_view', $data, true);
            $this->load->view('main_template', $template);
        }
        else {
            $this->load->view('formsuccess');
        }
        
        
        function password_check()
        {
            if ($this->settings_model->check_pass('bogus')) {
                return true;
            }
            else
            {
                $this->validation->set_message('password_check', 'password incorrect!');
                return false;
            }
        }
    }
#4

[eluser]codex[/eluser]
Ok, got it. The function needed was embedded in another function, which is incorrect I guess.
#5

[eluser]NemetraL[/eluser]
Hello,

1) what makes you think that the validation is not working (ie what is displayed? we need to know more..)
2) is 'bogus' really in your database as a password?

My 2 cents..
#6

[eluser]codex[/eluser]
Hey Nemetral,

Thanks, but I already got it. And no, bogus is just a, well, bogus password.
#7

[eluser]Derek Allard[/eluser]
Glad you got it codex. Welcome to CI!
#8

[eluser]codex[/eluser]
[quote author="Derek Allard" date="1187068554"]Glad you got it codex. Welcome to CI![/quote]

Hey Derek, thanks!!




Theme © iAndrew 2016 - Forum software by © MyBB