tags:

views:

333

answers:

1

I'm having a tough time getting this to work. Maybe my logic is all wrong, but I figure someone with more experience can help me.

I have a page "http://domain.com/account/settings" where users can edit their account information. When the form submits, it triggers the "settings_save" method. All is fine if the form submits successfully, however, in the event one of the field doesn't validate, the URL remains at "http://domain.com/account/settings_save" I actually want it to stay on http://domain.com/account/settings"

account.php controller

function settings() {
    $data['records'] = $this->account_model->getAccountSettings("sam");
    $this->load->view('account_settings_view', $data);
}

function settings_save() {
    $this->load->library('validation');

    $records['email']   = "trim|required|min_length[4]|xss_clean";
    $records['gender']  = "trim|required|xss_clean";
    $records['seeking'] = "trim|required|xss_clean";
    $records['marital_status']  = "trim|required|xss_clean";
    $records['kids']    = "trim|required|xss_clean";
    $records['drinking']    = "trim|required|xss_clean";
    $records['smoking'] = "trim|required|xss_clean";
    $records['ethnicity']   = "trim|required|xss_clean";
    $records['body_type']   = "trim|required|xss_clean";
    $records['zipcode'] = "trim|required|min_length[5]|numeric|xss_clean";

    $this->validation->set_rules($records);

    if ($this->validation->run() == false)
    {
        $this->load->view('account_settings_view', $records);                 
    }

    else
    {
        $records = array();
        $records['email'] = $this->validation->email;
        $records['gender'] = $this->validation->gender;
        $records['seeking'] = $this->validation->seeking;
        $records['marital_status'] = $this->validation->marital_status;
        $records['kids'] = $this->validation->kids;
        $records['drinking'] = $this->validation->drinking;                       
        $records['smoking'] = $this->validation->smoking;
        $records['ethnicity'] = $this->validation->ethnicity;
        $records['body_type'] = $this->validation->body_type;
        $records['zipcode'] = $this->validation->zipcode;     

        $this->account_model->saveAccountSettings("sam", $records);
        $this->session->set_flashdata('message', 'Done. You have added new task.');            

        redirect('account/settings');
        //redirect('account/settings');
    }
}

account_settings_view.php view

I have the following line:

<?=form_open('account/settings_save');?>
A: 

You only need one controller function, something like this:

function settings() {

    $this->load->library('validation');
    $records['email']   = "trim|required|min_length[4]|xss_clean";
    $records['gender']  = "trim|required|xss_clean";
    $records['seeking'] = "trim|required|xss_clean";
    $records['marital_status']  = "trim|required|xss_clean";
    $records['kids']    = "trim|required|xss_clean";
    $records['drinking']    = "trim|required|xss_clean";
    $records['smoking'] = "trim|required|xss_clean";
    $records['ethnicity']   = "trim|required|xss_clean";
    $records['body_type']   = "trim|required|xss_clean";
    $records['zipcode'] = "trim|required|min_length[5]|numeric|xss_clean";

    $this->validation->set_rules($records);

    if ($this->form_validation->run() == TRUE) {
        $records = array();
        $records['email'] = $this->validation->email;
        $records['gender'] = $this->validation->gender;
        $records['seeking'] = $this->validation->seeking;
        $records['marital_status'] = $this->validation->marital_status;
        $records['kids'] = $this->validation->kids;
        $records['drinking'] = $this->validation->drinking;                       
        $records['smoking'] = $this->validation->smoking;
        $records['ethnicity'] = $this->validation->ethnicity;
        $records['body_type'] = $this->validation->body_type;
        $records['zipcode'] = $this->validation->zipcode;     

        $this->account_model->saveAccountSettings("sam", $records);
        $this->session->set_flashdata('message', 'Done. You have added new task.');            

    } 

    $data['records'] = $this->account_model->getAccountSettings("sam");
    $this->load->view('account_settings_view', $data); 
}

Then in the view:

<form method="post" action=''>

That will post to itself.

Zack
Looks like I'm getting somewhere. How about if the validation doesn't go through? I assume I'm going to need an "else" in there. If so, how would I deal w/ the last two lines of the code where you load the view?
luckytaxi
You don't need an else. If validation fails, the same view will be loaded, and <?= validation_errors(); ?> will return a string (or several), and re-populate your forms. If it WAS successful, your info will be added to your database, and the success message shown.
Zack
Thanks, I had an error in my code that prevented that. This is awesome, finally got it working. Thanks!
luckytaxi