CodeIgniter Forums
Form validation and default values - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form validation and default values (/showthread.php?tid=23174)



Form validation and default values - El Forum - 10-02-2009

[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


Form validation and default values - El Forum - 10-05-2009

[eluser]jedd[/eluser]
Yes, as I understand it, this is all good practice.

Your code:
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:
Code:
$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);



Form validation and default values - El Forum - 04-14-2010

[eluser]octavianmh[/eluser]
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

-matt