Question about flash_data for displaying the errors please.

edited February 2013 in News & Announcements
I try to implement the email form and want to check the user's input for error and display it back if the validation is not pass.

public function send_email() { if ( ! empty($_POST) AND $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']) { // Processing Data $this->load->library('validator'); $this->load->library('session'); $this->validator->add_rule('email', 'valid_email', '- Please enter in a valid email', $this->input->post('email')); $this->validator->add_rule('subject', 'required', '- Please enter in a subject', $this->input->post('subject')); $this->validator->add_rule('body', 'required', '- Please enter in a message', $this->input->post('body')); if ($this->validator->validate()) { $this->load->library('email'); // send email $this->email->from($this->email, 'Server Email'); $this->email->subject($this->input->post('subject')); $this->email->message($this->input->post('body')); if (!$this->email->send()) { add_error('There was an error sending an email, please try again'); } } $this->session->set_flashdata('errors', $this->validator->get_errors()); redirect($_SERVER['HTTP_REFERER']); } redirect(site_url()); }

If the user's input does not pass this validation, it will redirect to the previous page without problem and I check the CI session data and this is what I get.
a:6:{s:10:"session_id";s:32:"e47e3755bbe33bf0f23eb2d04e16105e";s:10:"ip_address";s:7:"0.0.0.0";s:10:"user_agent";s:50:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:18";s:13:"last_activity";i:1359997646;s:37:"fuel_5a39246951b87e143722c4131b7e68e2";a:11:{s:2:"id";s:1:"1";s:9:"user_name";s:5:"admin";s:8:"password";s:32:"21232f297a57a5a743894a0e4a801fc3";s:5:"email";s:0:"";s:10:"first_name";s:5:"Admin";s:9:"last_name";s:0:"";s:8:"language";s:7:"english";s:9:"reset_key";s:0:"";s:11:"super_admin";s:3:"yes";s:6:"active";s:3:"yes";s:9:"last_page";s:14:"fuel/dashboard";}s:16:"flash:old:errors";a:1:{s:5:"email";s:31:"- Please enter in a valid email";}}f68273daeb3174705e4ce98e90e494aa
I saw that the errors get set in the session properly but the form_builder doesn't display the error even I set up the form_builder like this.
public function generate_email_form() { // Load Library $this->_CI->load->library('session'); $this->_CI->load->library('form_builder'); $this->_CI->form_builder->initialize(array('text_size_limit' => '50', 'submit_value' => 'Send Email', 'form_attrs' => array('action' => site_url('email_us/send_email'), 'method' => 'post'), 'textarea_rows' => 5, 'textarea_cols' => 50)); $fields = array(); $fields['email'] = array('required' => TRUE, 'label' => 'To Email:'); $fields['subject'] = array('required' => TRUE); $fields['body'] = array('required' => TRUE, 'type' => 'textarea', 'label' => 'Message:'); $this->_CI->form_builder->set_fields($fields); // will set the values of the fields if there is an error... must be after set_fields $this->_CI->form_builder->set_validator($this->validator); $this->_CI->form_builder->set_field_values($_POST); $this->_CI->form_builder->display_errors = TRUE; $this->_CI->form_builder->required_text = '<span class="required">*</span>required fields'; return $this->_CI->form_builder->render_divs(); }

How do I set the flashdata and send it to the form_builder to render the error correctly?

Thank you.

Comments

  • edited 10:41AM
    Errors on the form_builder object are controlled by the Validator object that you pass to it. So the $this->validator object needs those errors added to them which you can do with the "catch_errors" method like so:
    $this->validator->catch_errors($this->session->flashdata('errors'));
Sign In or Register to comment.