Reputation: 3311
I am using the Codeigniter template library created by phil sturgeon (http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/).
All works correctly until I add form validation to my forms, I can't seem to display the validation error messages how I did previously without the template library.
My validation is setup like:
$data['page'] = $this->Content_model->getContent($page);//Get data from table "content" where "page" equals $page
$subnav_data['subnav'] = $this->Content_model->getSubnav($category);//Get sub navigation items from database
$subnav_data['subnav_active'] = $page; //Set active subnav page
$left_column = $this->load->view('widgets/sub_navigation', $subnav_data, true);
$left_column .= $this->load->view('widgets/search_box', '', true); //Set data to be loaded into columns
$left_column_base = $this->load->view('widgets/assist_navigation', '', true);
$center_column = $this->load->view('content/text', $data, true); //Loads content from db in text view
//insert form into the template
$center_column .= $this->load->view('forms/question', '', true);//Load form
$center_column .= $this->load->view('widgets/breadcrumbs', '', true);
$center_column .= $this->load->view('widgets/share', '', true);
$right_column = $this->load->view('widgets/ask_us_a_question', '', true);
$right_column .= $this->load->view('widgets/newsletter', '', true);
$right_column .= $this->load->view('widgets/latest_news', '', true);
$this->template->inject_partial('left_column', $left_column); //Inject data into the partial columns
$this->template->inject_partial('left_column_base', $left_column_base);
$this->template->inject_partial('center_column', $center_column);
$this->template->inject_partial('right_column', $right_column);
$this->load->library('form_validation');//Load form validation
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('first_name', '"First name"', 'trim|required|alpha_dash|min_length[3]|max_length[55]');
$this->form_validation->set_rules('last_name', '"Last name"', 'trim|required|alpha_dash|min_length[3]|max_length[55]');
$this->form_validation->set_rules('service', '"Service"', 'trim|required|alpha_dash|min_length[3]|max_length[55]');
$this->form_validation->set_rules('email_address', '"Email address"', 'trim|required|valid_email');
$this->form_validation->set_rules('phone_number', '"Phone number"', 'trim|numeric|min_length[8]|max_length[20]');
$this->form_validation->set_rules('question', '"Question"', 'trim|required|min_length[20]|max_length[500]');
if ($this->form_validation->run() == FALSE){
$this->template->build('template',$data);
}
else
{
if($this->Question_model->submit_question()){
$this->session->set_flashdata('success', 'success');
redirect('question', 'location');
}
else{
$this->template->build('template',$data);
}
}
This is my form where I intend to display the validation errors:
<div id="form">
<?php
echo '<div id="error_container">' . validation_errors() . '</div>';
echo form_open('question');
//First name
echo '<b>'.form_label('First name *', 'first_name').'</b>';
echo form_input('first_name',set_value('first_name'));
//Last name
echo '<b>'.form_label('Last name *', 'last_name').'</b>';
echo form_input('last_name',set_value('last_name'));
//Email address
echo '<b>'.form_label('Email address *', 'email_address').'</b>';
echo form_input('email_address',set_value('email_address'));
//Phone number
echo '<b>'.form_label('Phone number *', 'phone_number').'</b>';
echo form_input('phone_number',set_value('phone_number'));
//Service
echo '<b>'.form_label('Service *', 'service').'</b>';
echo '<select name="service">';
echo'<option value=""'.set_select('service', TRUE).' >Select a service...</option> ';
echo'<option value="investments"'.set_select('service', 'investments').' >Investments</option> ';
echo'<option value="mortgages"'.set_select('service', 'mortgages').' >Mortgages</option> ';
echo'<option value="other"'.set_select('service', 'other').' >Other</option> ';
echo '</select>';
//Question
echo '<b>'.form_label('Question *', 'question').'</b>';
echo form_textarea('question',set_value('question'));
echo '<p><i>Your question will be forwarded to our consultants who will answer and return their response in the fastest time possible.</i></p>';
echo form_submit('submit', 'Ask question');
echo form_close();
?>
</div>
Upon submit the validation errors should be displayed, but aren't. The validation is successfully stopping the form posting when errors are found, but is not showing the errors to the user.
Is there something else required for form validation to work with the template library, how can I debug the issue???
Upvotes: 2
Views: 4094
Reputation: 10371
Is there something else required for form validation to work with the template library
Well, yes, you need to echo the form error out, e.g.
//First name
echo '<b>'.form_label('First name *', 'first_name').'</b>';
echo form_input('first_name',set_value('first_name'));
echo form_error('first_name');
Upvotes: 2