使用其他控制器呈现页面时,表单验证停止工作

时间:2013-03-23 23:18:23

标签: php codeigniter variable-assignment

我写了一个快速CI库类来渲染我的页面,所以我不必一直输入'$ this-> load-> view'和DRY。现在,当我在传递无效数据后重新呈现我的联系表单时,错误消息没有显示出来。

图书馆类:

class Page extends CI_Controller {

    public function render($page, $data) { // $page should be path to page view

        $this->load->view('fragments/header', $data);
        $this->load->view('fragments/navigation');
        $this->load->view($page);
        $this->load->view('fragments/navigation');
        $this->load->view('fragments/footer');

    }  

}

和控制器:

class Contact extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->helper('form');
        $this->load->library(array('form_validation', 'email', 'page'));
    }

    public function index() {

        $this->form_validation->set_rules('sender_name', 'From', 'required');
        $this->form_validation->set_rules('sender_email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'required');
        $this->form_validation->set_rules('message', 'Message', 'required');

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

            $this->load->library('page');
            $this->page->render('contact/contact', array('title' => 'Contact Me')); // pass in page title

            /* IT WORKED THIS WAY
            $this->load->view('fragments/header', array('title' => 'Contact Me')); // pass in page title
            $this->load->view('fragments/navigation');
            $this->load->view('contact/contact'); // TODO maintain form state
            $this->load->view('fragments/navigation');
            $this->load->view('fragments/footer');
             * 
             */
        }

//SNIP

联系表单视图:

<h1>Contact Me</h1>
<?php echo form_open('contact', 'id="contact_form"'); ?>

<label for="sender_name">Name:</label>
<?php echo form_input('sender_name'); ?>
<span class="error"><?php echo form_error('sender_name'); ?></span>

<label for="sender_email">Email:</label>
<?php echo form_input('sender_email'); ?>
<span class="error"><?php echo form_error('sender_email'); ?></span>

<label for="subject">Subject:</label>
<?php echo form_input('subject'); ?>
<span class="error"><?php echo form_error('subject'); ?></span>

<label for="message">Message:</label>
<?php echo form_textarea('message'); ?>
<span class="error"><?php echo form_error('message'); ?></span>

<?php echo form_submit('submit', 'Send'); ?>  

如何使用此帮助程序呈现页面并仍然从form_validation库中检索错误消息?

1 个答案:

答案 0 :(得分:3)

在您的views文件夹中创建一个名为template.php的视图,其中包含以下代码:

<?php

$this->load->view('fragments/header', $this->_ci_cached_vars); // pass $data vars
$this->load->view('fragments/navigation');
$this->load->view($view);
$this->load->view('fragments/navigation');
$this->load->view('fragments/footer');

然后使用以下控制器代码:

public function index() {

    $this->form_validation->set_rules('sender_name', 'From', 'required');
    $this->form_validation->set_rules('sender_email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('subject', 'Subject', 'required');
    $this->form_validation->set_rules('message', 'Message', 'required');

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

        $data = array(
            'view' => 'contact/contact', 
            'title' => 'Contact Me');
        $this->load->view('template', $data);
    }

您无需创建库来执行此操作,因为您只需创建一个模板视图,该视图将加载相应的视图和指定的视图,并将数据传递给它们。