我使用另一个控制器呈现页面时,表单验证停止工作
作者:互联网
我写了一个快速的CI库类来呈现我的页面,这样我就不必一直为DRY一直输入’$this-> load-> view’.现在,当我在传递无效数据后重新呈现联系表单时,错误消息不再显示.
库类:
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库检索错误消息?
解决方法:
在您的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);
}
您无需创建库即可执行此操作,因为您只需创建一个模板视图即可加载相应的视图和指定的视图,并将数据传递给它们.
标签:php,codeigniter,variable-assignment 来源: https://codeday.me/bug/20191014/1911808.html