1
votes

CI3 documentation clearly states, "Since you haven’t told the Form Validation class to validate anything yet, it returns FALSE (boolean false) by default. The run() method only returns TRUE if it has successfully applied your rules without any of them failing.".

I have the below method snipplet. Obviously, on first view, the view will always spit out a 'Warning - Oooops something went wrong' but with no validations_errors(). How would I go about fixing that? Would one check to see if the form was $_POST ? What is the best way or suggestion on how to complete? bootstrap_alert() just wraps the error or success message into a bootstrap alert.

    if ($this->form_validation->run() == FALSE) {
        $message = array(   'message'   => 'Warning - Ooops something went wrong '.validation_errors(),
                            'class'     => 'danger',  // must be warning, danger, success or info.
                    );
        $this->data['alert'] = bootstrap_alert($message);
        $this->load->view($this->mh_template, $this->data);
    }   else {
        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->load->view($this->mh_template, $this->data); 
    }

This is what I finally ended up doing:

// set the form rules $this->form_validation->set_rules('timezones', 'timezones', 'required');

    if ($this->form_validation->run() == FALSE) {

        $this->load->view($this->mh_template, $this->data);


    }   else {

        $my_time_zone   = $this->input->post('timezones');
        $user_id        = $this->ion_auth->user()->row()->id;
        $setting_insert_or_update = 'user_time_zone';
        $this->MH_user_settings_model->mh_insert_or_update_setting($setting_insert_or_update, $my_time_zone, $user_id);
        $message = array(   'message'   => 'Success - Timezone updated',
                            'class'     => 'success',  // must be warning, danger, success or info.
                        );
        $this->data['alert'] = bootstrap_alert($message);
        $this->session->set_flashdata('message', $this->data['alert'] );              
        redirect('/MH_app_private/mh_app_settings');
    }
2
can you post your whole function so we can check left things? - M.Hemant
have you load form_validation library? - PHP Geek
On initial view, there are no errors, however, the message array is shown (without validation errors). On post when I purposely make an error, the message array is show with validation errors. Success is also show at the right time. The problem is the initial view. Yes, form_validation is loaded. - spreaderman

2 Answers

1
votes

Check if is the request is POST to show or not to show the warning.

0
votes

I did not realize I could format the validation_errors() with something like this:

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

This is finally what solved the issue.