[eluser]zsela[/eluser]
Hi Guys,
I am pretty new to CodeIgniter and so far I really enjoy using it.
However today I ran into an irritating problem. I have a page, where the users can update their personal information. There are two forms on this page, one for the general information update (update_usr) and the other one is for updating their password (update_usr_pwd). The view is called user/edit.
My first problem is that I can't manage to validate the input fields. I searched for the answer on the internet, and I found some tips, but they doesn't seem to work.
Here is the adequate function from the User controller:
Code:
function update_user() {
if ($this->input->post('submit_btn')) {
$this->form_validation->set_rules('surname', 'Vezetéknév', 'trim|xss_clean|required');
$this->form_validation->set_rules('firstname', 'Keresztnév', 'trim|xss_clean|required');
$this->form_validation->set_rules('birth_year', 'Születési év', 'trim|xss_clean|integer');
$this->form_validation->set_rules('city', 'Város', 'trim|xss_clean');
$this->form_validation->set_rules('gender', 'Nem', 'trim|required|xss_clean');
$this->form_validation->set_rules('avatar', 'Avatár', 'trim|required|xss_clean');
} else if ($this->input->post('pwd_btn')) {
$this->form_validation->set_rules('old_pwd', 'Régi jelszó', 'trim|required|min_length[5]|xss_clean|alpha_numeric');
$this->form_validation->set_rules('pwd', 'Új jelszó', 'trim|required|min_length[5]|xss_clean|alpha_numeric');
$this->form_validation->set_rules('pwd_conf', 'Új jelszó megerősítése', 'trim|required|min_length[5]|matches[pwd]|xss_clean|alpha_numeric');
}
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if($this->session->userdata('logged_in')){
if($this->form_validation->run() == FALSE) {
// didn't run, or with errors
redirect('user/edit_user');
} else {
// everything is OK
if ($this->input->post('submit_btn')) {
$surname = $this->input->post('surname');
$firstname = $this->input->post('firstname');
$city = $this->input->post('city');
$birth_year = $this->input->post('birth_year');
$gender = $this->input->post('gender');
$avatar = $this->input->post('avatar');
$data['default']['firstname'] = $firstname;
$data['default']['surname'] = $surname;
$data['default']['birth_year'] = $birth_year;
$data['default']['gender'] = $gender;
$data['default']['city'] = $city;
$data['default']['avatar'] = $avatar;
if ($this->User_model->update_user($surname, $firstname, $city, $birth_year, $gender, $avatar, $this->session->userdata('ID'))) {
$data['default']['verdict'] = 'success';
$this->load->view('user/edit', $data);
} else {
$data['default']['verdict'] = 'fail';
$this->load->view('user/edit', $data);
}
} else if ($this->input->post('pwd_btn')) {
$old_pwd = $this->input->post('old_pwd');
$pwd = $this->input->post('pwd');
if ($this->User_model->update_user_pwd($pwd, $this->session->userdata('ID'))) {
$data['default']['verdict_pwd'] = 'success';
redirect('user/edit_user', $data);
} else {
$data['default']['verdict_pwd'] = 'fail';
$this->load->view('user/edit', $data);
}
}
}
} else {
$this->session->set_flashdata('signed_in', FALSE);
$this->session->set_userdata('source_page', 'user/update_user');
redirect('user/login');
}
}
I will continue in another post...