表单验证不显示Codeigniter模板库的错误

时间:2011-12-03 15:42:52

标签: php forms validation templates codeigniter

我正在使用phil sturgeon创建的Codeigniter模板库(http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/)。

所有工作都正常,直到我向表单添加表单验证,我似乎无法显示验证错误消息,如果没有模板库,我之前的表现如何。

我的验证设置如下:

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

}

这是我打算显示验证错误的表单:

<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>

提交后,应显示验证错误,但不是。验证是在找到错误时成功停止表单发布,但未向用户显示错误。

表单验证是否还需要其他东西才能使用模板库,如何调试问题???

1 个答案:

答案 0 :(得分:2)

  

表单验证是否还需要其他东西才能使用模板库

嗯,是的,你需要回复表单错误,例如

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