• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation ... callback doesn't get triggered

#1
[eluser]FinalFrag[/eluser]
I'm just starting out with CodeIgniter and I ran up to a problem I cannot seem to fix. So I decided to pay another visit to the forums Smile

I have a simple form with 2 fields: username and password. I don't want to use the validation rules that come with CodeIgniter because I want to have more control over the error message the user gets. I currently have this code:

Code:
function login() {
        $this->load->library('validation');
        
        $this->validation->set_error_delimiters('Functions::showMessage(\'error\', \'', '\');');
        
        $rules['username'] = 'trim';
        $rules['password'] = 'trim|callback__check_input';
        $this->validation->set_rules($rules);
        
        $fields['username'] = 'username';
        $fields['password'] = 'password';
        $this->validation->set_fields($fields);
        
        if ($this->validation->run()) {
            // everything is ok
            redirect('home');
        }
        else {
            $this->load->view('admin/login');
        }
    }
    
    function _check_input() {
        $username = trim($this->input->post('username'));
        $password = trim($this->input->post('password'));
        
        if (!empty($username) && !empty($password)) {
            $this->load->model('users');
            
            $result = $this->users->GetUserByUsername($username);
            
            if ($result->num_rows() > 0) {
                foreach ($result->result() as $row) {
                    if ($row->password == md5($password)) {
                        // the password is correct
                        $this->session->set_userdata('userId', $row->userId);
                        $this->session->set_userdata('username', $row->username);
                        
                        $this->session->set_flashdata('info', 'Logged in as ' . $row->username . '.');
                        
                        return true;
                    }
                    else {
                        $this->validation->set_message('_check_input', 'Incorrect username and/or password.');
                    }
                }
            }
            else {
                $this->validation->set_message('_check_input', 'Incorrect username and/or password.');
            }
        }
        else {
            $this->validation->set_message('_check_input', 'All fields are required.');
            return false;
        }
        
        return false;
    }

Can anyone tell me why _check_input is never called? Instead I just get redirected to 'home' if I leave all fields empty...

Thanks is advance...

#2
[eluser]xwero[/eluser]
none of the fields are required which means the validation just skips them if they are empty. Because there is no output for the run method php will see this as true. The simplest way to fix this is to do this
Code:
if ($this->validation->run() === TRUE)

#3
[eluser]FinalFrag[/eluser]
Code:
if ($this->validation->run() === TRUE)

That doesn't work. I have tested this with the following code:

Code:
if ($this->validation->run() === true) {
    echo 'result: ' . $this->validation->run();
    redirect('home');
}
else {
    $this->load->view('admin/login');
}

This always returns:

Code:
result: 1

So it always returns true... even if it shouldn't...

Anyone else have a suggestion / solution?

#4
[eluser]xwero[/eluser]
Nice to know a void return is always 1.

The next thing you can do is turn around your if
Code:
if (!$this->validation->run()) {
      $this->load->view('admin/login');
}else {
     // everything is ok
     redirect('home');
}

#5
[eluser]FinalFrag[/eluser]
Nope, still no luck...

#6
[eluser]xwero[/eluser]
The same as the true then but for false
Code:
if ($this->validation->run() === FALSE) {
      $this->load->view('admin/login');
}else {
     // everything is ok
     redirect('home');
}

#7
[eluser]FinalFrag[/eluser]
Nope, that's just another way to write the same thing, I think I need a different approach.

I noticed that when I add a 'required' to the rules, it does work. But I also get the 'The ... field is required.'-message (which I obviously don't want)...

Any more hints on this subject?

#8
[eluser]xwero[/eluser]
Maybe it is the trim function that is the cause, you could try this to be sure
Code:
$rules['username'] = 'trim';
$rules['password'] = 'callback__check_input|trim';

#9
[eluser]xwero[/eluser]
[quote author="FinalFrag" date="1210855936"]Nope, that's just another way to write the same thing, I think I need a different approach.[/quote]
It's not another way it is a different way
Code:
!$variable
equals
Code:
$variable == FALSE
And because of the weak typing of php NULL and 0 equal to FALSE which gives you false result in this case.
With
Code:
$variable === FALSE
The type of the response gets checked too so the return of the variable needs to be a boolean.

#10
[eluser]wiredesignz[/eluser]
My 2c.

@xwero: echoing TRUE always displays as 1

@Final: You will find the error messages in system/language/english/validation_lang.php, you could create your own version of these in application/language.

Callbacks are never run for an empty input. So `trim|required` is your only choice, unless you create a MY_Validation extension.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.