[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');
}
}
}
?>
Model: settings_model.php
Code:
<?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;
}
}
}
?>
View: password_view.php
Code:
<?=$this->validation->error_string; ?>
<form method="post" name="edit_password" class="pt-20">
<label for="old_password" class="bold">Je oude wachtwoord</label>
<p class="pb-5"><?php echo input_password("old_password", 30, "input_text", $this->validation->old_password)?></p>
<label for="new_password" class="bold">Nieuwe wachtwoord</label>
<p class="pb-5"><?php echo input_password("new_password", 30, "input_text", $this->validation->new_password)?></p>
<label for="new_password_retype" class="bold">Nieuwe wachtwoord nogmaals</label>
<p class="pb-5"><?php echo input_password("new_password_retype", 30, "input_text", $this->validation->new_password_retype)?></p>
<p><?php echo input_submit("edit_password", "input_submit", "Wachtwoord opslaan")?></p>
</form>
Any help is greatly appreciated!!