Welcome Guest, Not a member yet? Register   Sign In
Form validation and default values
#1

[eluser]yehiasalam[/eluser]
Hi,

I have a form that require server side validation, and in addition i need to prefill it from existing values in the database, all the examples i saw from the community were 2 separate functions, one for displaying the initial edit page, and another one for the form submition itself (something like this with update and updatePerson),
but what i'm actually doing is using the same function for both the prefilling and the validation taking the advantage of the set_value function and wanted to check if this is good practice, here is the code:
Code:
function edit(){

        $this->load->helper('form');
        $this->load->library('form_validation');
                $this->form_validation->set_error_delimiters('<div class="form_error">* ', '</div>');
                $this->form_validation->set_rules('profile_name_first', 'First Name', 'required');
                $this->form_validation->set_rules('profile_name_last', 'Last Name', 'required');
                $this->form_validation->set_rules('profile_gender', 'Gender', 'required');

                $account_id = $this->session->userdata('account_id');
                $account_name = $this->Account_model->field('account_name', "account_id = '$account_id'" );

        if ($this->form_validation->run() == FALSE)
        {
                    $data = $this->Profile_model->read($account_id);
                    $data['account_name'] = $account_name;
                    $data['account_id'] = $account_id;
                    $data['profile_gender_male'] = $data['profile_gender'] == 1;
                    $data['profile_gender_female'] = $data['profile_gender'] == 2;

                    $this->load->view('profile_edit', $data);
        }else{

                    $this->Profile_model->save(array('profile_name_first' => $this->input->post('profile_name_first'),
                                                     'profile_name_last' => $this->input->post('profile_name_last'),
                                                     'profile_website' => $this->input->post('profile_website'),
                                                     'profile_gender' => $this->input->post('profile_gender'),
                                                     'profile_about' => $this->input->post('profile_about'),
                                                     'profile_favorite_music' => $this->input->post('profile_favorite_music')), $account_id);
                    redirect('/profile');

                }

        }

and for the view
Code:
<label for="profile_name_first">First Name: </label>
                    &lt;input type="text" name="profile_name_first" value="&lt;?php echo set_value('profile_name_first', $profile_name_first ); ?&gt;" /&gt;
                    <br />

                    <label for="profile_name_last">Last Name: </label>
                    &lt;input type="text" name="profile_name_last" value="&lt;?php echo set_value('profile_name_last', $profile_name_last ); ?&gt;" /&gt;
                    <br />


                    <label for="profile_gender">Gender: </label>
                    &lt;input type="radio" name="profile_gender" value="1" &lt;?php echo set_radio('profile_gender', '1', $profile_gender_male); ?&gt;/&gt; Male
                    &lt;input type="radio" name="profile_gender" value="2" &lt;?php echo set_radio('profile_gender', '2', $profile_gender_female); ?&gt;/&gt; Female
                    <br />

Thanks In Advance
Regards.
Yehia A.Salam


Messages In This Thread
Form validation and default values - by El Forum - 10-02-2009, 02:39 PM
Form validation and default values - by El Forum - 10-05-2009, 05:36 AM
Form validation and default values - by El Forum - 04-14-2010, 02:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB