• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form validation and default values


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:
function edit(){

                $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);

                    $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);



and for the view
<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
Yehia A.Salam

Yes, as I understand it, this is all good practice.

Your code:
&lt;input type="radio" name="profile_gender" value="1" &lt;?php echo set_radio('profile_gender', '1', $profile_gender_male); ?&gt;/&gt; Male
- makes use of pre-posted data (if available) otherwise falling through to (presumably) your database-retrieved data.

One thing you might want to do is use the form helper functions.

An excerpt from one of my views:
$first_name_spec = array (    "name" => "first_name",
                            "value" => set_value('first_name', $member['first_name']),
                            "maxlength" => "50",
                            "size" => "20"  );
echo form_input ($first_name_spec);

Could I request an example like the above be brought into the form validation and helper documentation? This just saved me a ton of time, I knew it was possible, but wasn't sure the best-practice way of accomplishing it, and it's not spelled out well in the docs!

Thanks. Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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