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');
}