• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple form vaildation problem

#1
[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...

#2
[eluser]zsela[/eluser]
My second problem is that the input fields of the first form remain blank after submitting the second form, however they should be pre-populated from the database.

Here is the function which does this job:
Code:
function edit_user () {
        
        if($this->session->userdata('logged_in')){
        
            $current_data = $this->User_model->get_user_data($this->session->userdata('ID'));
            
            $data['default']['user_name'] = $current_data->user_name;
            $data['default']['firstname'] = $current_data->firstname;
            $data['default']['surname'] = $current_data->surname;
            $data['default']['birth_year'] = $current_data->birth_year;
            $data['default']['gender'] = $current_data->gender;
            $data['default']['city'] = $current_data->city;
            $data['default']['avatar'] = $current_data->avatar;
            
            $this->load->view('user/edit' , $data);
        
        } else {
            $this->session->set_flashdata('signed_in', FALSE);
            $this->session->set_userdata('source_page', 'user/edit_user');
            redirect('user/login');
        }
        
    }

And here is the adequate part from the view:
Code:
$form1_attributes = array("id" => "update_usr");
            echo form_open('user/update_user', $form1_attributes);
            
            $firstname = array(
                'name'    =>    'firstname',
                'id'    =>    'firstname',
                'value'    =>    set_value('firstname' , isset($default['firstname'])?$default['firstname']:'' )
            );
            if (form_error('firstname')) { $firstname['class'] = 'inputFieldError'; }
            else $firstname['class'] = 'inputField';
            
            $surname = array(
                'name'    =>    'surname',
                'id'    =>    'surname',
                'value'    =>    set_value('surname' , isset($default['surname'])?$default['surname']:'' )
            );
            if (form_error('surname')) { $surname['class'] = 'inputFieldError'; }
            else $surname['class'] = 'inputField';
            
            $city = array(
                'name'    =>    'city',
                'id'    =>    'city',
                'value'    =>    set_value('city' , isset($default['city'])?$default['city']:'' )
            );
            if (form_error('city')) { $city['class'] = 'inputFieldError'; }
            else $city['class'] = 'inputField';
            
            $submit_btn = array(
                'name'    =>    'submit_btn',
                'id'    =>    'submit_btn',
                'value'    =>    'Mentés'
            );
            
            if (isset($default['verdict'])) { echo $default['verdict']; }

Thanks for your replies in advance!
PS: Sorry for my english, I know it's terrible.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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