• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning to update form after validation

#1
[eluser]Loquela[/eluser]
Hi there,

I am just loving Codeigniter by the way! I have never done OOP before and I am using this framework to learn all about it. Thank you very much!!

Below I have two functions, update_admin() which displays an update form and populates the fields with the content of the returned row. update_user() validates the user input and updates the selected record if validation is passed.

If validation fails then it reloads update_admin() - the update form with any validation errors.

This all works fine, I am returned to the form and validation messages appear as they should, but I get errors because the variable that contains the record to be updated is now empty. How can I reload update_admin() after a failed validation and return the record that I am trying to update?

I hope my question is clear. Any help much appreciated. Thanks.


Code:
// Display update user form
    function update_admin()
    {
        $data['title'] = 'Update admin user';
        $data['rows'] = $this->users_model->get_admin_users(); // get list of admin users
        $data['row'] = $this->users_model->get_user(); // get user to be updated
        
        $this->load->view('includes/header',$data);
        $this->load->view('admin/users/users_view',$data);
        $this->load->view('admin/users/update_admin_view',$data);    
        $this->load->view('includes/footer',$data);
    }
    
    // Update user details
    function update_user()
    {    
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Retype password', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        
        if($this->form_validation->run()==FALSE)
        {
            $this->update_admin();
        }
        else
        {
        $data = array(
            'username'        =>$this->input->post('username'),
            'password'         =>$this->input->post('password'),
            'firstname'     =>$this->input->post('firstname'),
            'lastname'         =>$this->input->post('lastname'),
            'email'            =>$this->input->post('email'),
            'updated'        =>$this->input->post('updated')
            );
            
            $this->users_model->update_user($data);
            $this->update_admin();
        }
    }

#2
[eluser]flaky[/eluser]
what is $variable for at
Code:
function update_admin($variable)

#3
[eluser]Loquela[/eluser]
Thanks for getting back to me on this.

Sorry, $variable shouldn't be there. But it's nothing to do with the problem. I've removed it from the example.

Cheers for pinting it out!

#4
[eluser]flaky[/eluser]
shouldn't get_user(); at
Code:
$data['row'] = $this->users_model->get_user();
take a parameter (example: $username), as to know which user detail to return?

#5
[eluser]flaky[/eluser]
try this, hope it works
Code:
// Display update user form
    function update_admin($data)
    {
        $data['title'] = 'Update admin user';
        $data['rows'] = $this->users_model->get_admin_users(); // get list of admin users
        $data['row'] = $this->users_model->get_user(); // get user to be updated
        
        $this->load->view('includes/header',$data);
        $this->load->view('admin/users/users_view',$data);
        $this->load->view('admin/users/update_admin_view',$data);    
        $this->load->view('includes/footer',$data);
    }
    
    // Update user details
    function update_user()
    {
        $data = array(
            'username'            =>$this->input->post('username'),
            'password'             =>$this->input->post('password'),
            'firstname'         =>$this->input->post('firstname'),
            'lastname'             =>$this->input->post('lastname'),
            'email'                =>$this->input->post('email'),
            'updated'            =>$this->input->post('updated')
        );
        
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Retype password', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        
        if($this->form_validation->run()==FALSE)
        {
            $this->update_admin($data);
        }
        else
        {
            $this->users_model->update_user($data);
            $this->update_admin($data);
        }
    }

#6
[eluser]Loquela[/eluser]
Thanks for that flaky,

That looks promising. But I still get "undefined variable:data" because the model is expecting the record key in the url segment:

Code:
// Get user model

    function get_user()
    {
        $query = $this->db->get_where('pp_users', array('userid' => $this->uri->segment(4)));
        
        if ($query->num_rows() == 1)
        {
            $data = $query->row();
        }
    
        return $data;
    }

This is because I load the update form initially by clicking on a link in a list of users. So I am gessing I somehow need to get back to the form via the url. But I think if I do that, say redirect, then the validation messages won't appear.

Cheers

#7
[eluser]flaky[/eluser]
just make a check if the segment is set
Code:
if($this->uri->segment(4)){
        $query = $this->db->get_where('pp_users', array('userid' => $this->uri->segment(4)));
        
        if ($query->num_rows() == 1)
        {
            $data = $query->row();
        }
    
        return $data;
}

#8
[eluser]Loquela[/eluser]
Thanks Flakey,

Not sure what you mean. The segment is not set when I call the $this->update_admin($data) after validation. I think I need to set it somehow?

Sorry if I'm being thick :-S

thanks

#9
[eluser]flaky[/eluser]
Don't say sorry.

Change your model to take a parameter (not read from the segment) sth like this
Code:
// Get user model

    function get_user($user_id)
    {
        $query = $this->db->get_where('pp_users', array('userid' => $user_id));
        
        if ($query->num_rows() == 1)
        {
            $data = $query->row();
        }
    
        return $data;
    }

and in the controller
Code:
// Display update user form
    function update_admin($data)
    {
        $data['title'] = 'Update admin user';
        $data['rows'] = $this->users_model->get_admin_users(); // get list of admin users
        
        if($this->uri->segment(4))
            $data['row'] = $this->users_model->get_user($this->uri->segment(4)); // get user to be updated
        
        $this->load->view('includes/header',$data);
        $this->load->view('admin/users/users_view',$data);
        $this->load->view('admin/users/update_admin_view',$data);    
        $this->load->view('includes/footer',$data);
    }
    
    // Update user details
    function update_user()
    {
        $data = array(
            'username'            =>$this->input->post('username'),
            'password'             =>$this->input->post('password'),
            'firstname'         =>$this->input->post('firstname'),
            'lastname'             =>$this->input->post('lastname'),
            'email'                =>$this->input->post('email'),
            'updated'            =>$this->input->post('updated')
        );
        
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Retype password', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        
        if($this->form_validation->run()==FALSE)
        {
            $this->update_admin($data);
        }
        else
        {
            $this->users_model->update_user($data);
            $this->update_admin($data);
        }
    }

#10
[eluser]Loquela[/eluser]
Thanks a lot. I think I got it now. I have to go for a bit but I will try later and let you know how it goes.
Thanks so much for your help!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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