CodeIgniter Forums
Problem with form validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem with form validation (/thread-44274.html)

Pages: 1 2


Problem with form validation - El Forum - 08-09-2011

[eluser]Aniket Pant[/eluser]
Form validation is not working in one of the pages. Rest of the pages are working fine.

This is my controller:
Code:
public function CreateProfile_Step2()
        {            
                $data['page_title'] = 'Create Profile (Step 2)';

                $loginid = $this->session->userdata('loginid');

                $this->load->model('profilemodel', 'profile');
                $userid = $this->profile->get_userid($loginid);

                $this->session->set_userdata('userid', $userid);

                $usertype = $this->profile->get_usertype($userid);
                $data['usertype'] = $usertype;

                if ($usertype == 'User') {
                    $this->load->model('activitymodel', 'activity');
                    $arr_activities = $this->activity->get_activities();
                    $data['options'] = $arr_activities;
                }
                else if ($usertype == 'Gym/Health Club Owner') {
                    $this->load->model('facilitymodel', 'facility');
                    $arr_facility = $this->facility->get_facilities();
                    $data['options'] = $arr_facility;
                }
                
                $config = array(
                    'User'  =>  array(
                                    array(
                                        'name'  => 'sex',
                                        'label' => 'Sex',
                                        'rules' => 'required'
                                    )),
                    'Gym/Health Club Owner' =>  array(
                                                    array(
                                                        'name'  => 'website',
                                                        'label' => 'Website',
                                                        'rules' => 'prep_url'
                                                    ),
                                                    array(
                                                        'name'  => 'hours_of_operation',
                                                        'label' => 'Hours of Operation',
                                                        'rules' => 'required|numeric'
                                                    ),
                                                    array(
                                                        'name'  => 'membership_charges',
                                                        'label' => 'Membership Charges',
                                                        'rules' => 'required|numeric'
                                                    ))
                );

                $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

                $this->form_validation->set_rules($config);

                if ($usertype == 'User') {
                    if ($this->form_validation->run('User') == FALSE) {
                        $this->load->view('create-profile-step-2', $data);
                    }
                    else {
                        $selected_options = $this->input->post('activities');
                        $this->activity->add_user_activities($userid, $selected_options);
                        $sex = $this->input->post('sex');
                        $this->profile->add_user_details($userid, $sex);
                        echo 'Profile Creation Completed!';
                    }
                }
                else {
                    if ($this->form_validation->run('Gym/Health Club Owner') == FALSE) {
                        $this->load->view('create-profile-step-2', $data);
                    }
                    else {
                        $selected_options = $this->input->post('facilities');
                        $this->facility->add_gym_facility($userid, $selected_options);
                        $website = $this->input->post('website');
                        $hours_of_operation = $this->input->post('hours_of_operation');
                        $membership_charges = $this->input->post('membership_charges');
                        $this->facility->add_gym_details($userid, $website, $hours_of_operation, $membership_charges);
                        echo 'Profile Creation Completed!';
                    }
                }
                
        }



Problem with form validation - El Forum - 08-09-2011

[eluser]Aniket Pant[/eluser]
And this is my view:
Code:
&lt;?php include 'user/inc/header.php'; ?&gt;

<div id="content" class="row twelvecol">
    <h1>Create Profile (Step 2 of 2)</h1>
    <h2>For &lt;?php echo $usertype; ?&gt;</h2>
    &lt;?php
        echo form_open('register/CreateProfile_Step2');
        if ($usertype == 'User') {
                echo form_label('Sex', 'sex');
                $options_sex = array(
                    'Male' => 'Male',
                    'Female' => 'Female'
                );
                echo form_dropdown('sex', $options_sex, 'male');
                $ctr = 1;
                echo form_label('Activities Interested In', 'activities');
                foreach($options->result() as $option)
                {
                    echo form_label($option->activity, 'activity-'.$ctr);
                    $arr_option = array(
                        'name'  => 'activities[]',
                        'id'    => 'activity-'.$ctr++,
                        'value' => $option->activity
                    );
                    echo form_checkbox($arr_option);
                }
        }
        elseif ($usertype == 'Gym/Health Club Owner')
        {
                echo form_label('Website', 'website');
                $arr_website = array(
                    'name' => 'website',
                    'id' => 'website',
                    'value' => set_value('website')
                );
                echo form_input($arr_website);
                echo form_label('Hours of Operation', 'hours_of_operation');
                $arr_hours = array(
                    'name' => 'hours_of_operation',
                    'id' => 'hours_of_operation',
                    'value' => set_value('hours_of_operation')
                );
                echo form_input($arr_hours);
                echo form_label('Membership Charges', 'membership_charges');
                $arr_charges = array(
                    'name' => 'membership_charges',
                    'id' => 'membership_charges',
                    'value' => set_value('membership_charges')
                );
                echo form_input($arr_charges);
                $ctr = 1;
                echo form_label('Facilities Available', 'facilities');
                foreach($options->result() as $option)
                {
                    echo form_label($option->facility, 'facility-'.$ctr);
                    $arr_option = array(
                        'name'  => 'facilities[]',
                        'id'    => 'facility-'.$ctr++,
                        'value' => $option->facility
                    );
                    echo form_checkbox($arr_option);
                }
        }
        echo "<br/><br/>";
        echo form_submit('submit', 'Create Profile');
        echo form_close();
        
        echo validation_errors();
    ?&gt;
    
    <a href="&lt;?php echo site_url(); ?&gt;/register/CreateProfile_Step1">Return to previous step</a>
    
</div>

&lt;?php include 'user/inc/footer.php'; ?&gt;



Problem with form validation - El Forum - 08-10-2011

[eluser]Aken[/eluser]
What exactly is not working? Are you receiving a specific error?


Problem with form validation - El Forum - 08-10-2011

[eluser]Aniket Pant[/eluser]
No error. Nothing is coming. No notification.

Even when I try to submit any detail it is not proceeding. I have tried echoing at various places, it is going into the part where form validation is failing.


Problem with form validation - El Forum - 08-10-2011

[eluser]praveens[/eluser]
Turn on error reporting (error_reporting(E_ALL)Wink and check the error.

Cheers!!!


Problem with form validation - El Forum - 08-10-2011

[eluser]Aniket Pant[/eluser]
Error reporting is enabled :|


Problem with form validation - El Forum - 08-10-2011

[eluser]LuckyFella73[/eluser]
Enable the profiler - that way you can check if all POST values
are send to the controller like expected:

Code:
// place this in your controller:
$this->output->enable_profiler(TRUE);



Problem with form validation - El Forum - 08-10-2011

[eluser]Aniket Pant[/eluser]
I have checked and all the values are being posted.

Could there be any other problem. I am stuck here for quite sometime.


Problem with form validation - El Forum - 08-10-2011

[eluser]Wondering Coder[/eluser]
why don't you put your form_validation in config file. Read about it in the CI userguide. That's how I do it. Your code will be much cleaner and easier to debug


Problem with form validation - El Forum - 08-10-2011

[eluser]Aken[/eluser]
Okay, the problem is that the set_rules() function isn't designed to take a multi-dimensional array like that. If you want to use the different groups, you have to set your rules in a config file as Wondering Coder said, otherwise the library won't know where to find those rules.