[eluser]codex[/eluser]
[quote author="walesmd" date="1187140896"]You can access any field from the form if you have assigned the fields names using $this->validation->[fieldname]:
Code:
$rules['username'] = 'trim|required|callback__check_logindata';
$rules['password'] = 'trim|required';
$this->validation->set_rules($rules);
$fields['username'] = 'username';
$fields['password'] = 'password';
$this->validation->set_fields($fields);
Code:
function _check_logindata($username) {
$password = $this->validation->password;
if ($this->login_model->check_login_data($username, $password) !== FALSE) {
$this->validation->set_message('check_logindata', 'Logindata incorrect!');
return FALSE;
} else {
return TRUE;
}
Note: I also changed your callback function by prepending an underscore - now it won't be accessible via the URL - without that underscore people could access it via controller/check_logindata[/quote]
Hey thanks! I was just about to give up on CI ;-)
I've tried it, but it doesn't work properly. The required rules apply, but even with a false username you're being redirected. Maybe the model is no good?
Model:Code:
<?php
class Login_model extends Model {
// ------------------------------------------------------------------------
function Login_model()
{
parent::Model();
}
// ------------------------------------------------------------------------
# /////////////////////////
function check_login_data($username, $password)
{
$query = $this->db->select('user_id, user_password');
$query = $this->db->from('users');
$query = $this->db->where('user_name', $username);
$query = $this->db->where('user_password', md5($password));
$query = $this->db->get();
if($query->num_rows() > 0) {
return $query->result();
}
else {
return false;
}
}
// ------------------------------------------------------------------------
}
?>
Controller:Code:
<?php
class Home extends Controller {
// ========================================================================
function Home()
{
parent::Controller();
}
// ========================================================================
function index()
{
# Validations
$this->load->library('validation');
$this->load->model('login_model');
$rules['user_name'] = 'trim|required|callback__check_logindata';
$rules['user_password'] = 'trim|required';
$this->validation->set_rules($rules);
$fields['user_name'] = 'user_username';
$fields['user_password'] = 'user_password';
$this->validation->set_fields($fields);
$data['title'] = "Welkom";
$this->validation->set_error_delimiters('<p class="alert">', '</p>');
if ($this->validation->run() == FALSE)
{
$template['content'] = $this->load->view('home/home_view', $data, true);
$this->load->view('main_template', $template);
}
else
{
redirect('start/');
}
}
// check logindata
// --------------------------------------------------------------
function _check_logindata($username) {
$password = $this->validation->user_password;
if ($this->login_model->check_login_data($username, $password) !== FALSE) {
$this->validation->set_message('check_logindata', 'Logindata incorrect!');
return FALSE;
} else {
return TRUE;
}
// ========================================================================
}
}
?>
View:Code:
<?php echo form_open();?>
<label for="user_name">Gebruikersnaam</label>
<p class="pb-10"><?php echo input_text("user_name", 20, "input_text", $this->validation->user_name)?></p>
<?=$this->validation->user_name_error; ?>
<label for="user_password">Wachtwoord</label>
<p class="pb-10"><?php echo input_password("user_password", 20, "input_text", $this->validation->user_password)?></p>
<?=$this->validation->user_password_error; ?>
<p class="pb-20"><label for="login"></label> <a href="/forgot_pass">Wachtwoord vergeten?</a></p>
<p><label for="login"></label>
<?php echo input_submit("login", "input_submit", "Naar binnen")?></p>
</form>