问题描述
我写了一个快速的CI库类来渲染我的页面,所以我不需要一直输入'$ this-> load-> view'和DRY。现在,当我在传递无效数据后重新呈现联系人表单时,错误消息不会出现。
库类:
class扩展CI_Controller {
public function render($ page,$ data){// $ page应为页面视图的路径
$ 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: :__构造();
$ 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-& );
$ this-> page-> render('contact / contact',array('title'=>'Contact Me')); //传递页面标题
/ *这样工作
$ this-> load-> view('fragments / header',array('title'=>联络我')); // 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
b $ b
联络表单检视:
< h1>与我联络< / h1>
<?php echo form_open('contact','id =contact_form'); >
< label for =sender_name>名称:< / label>
<?php echo form_input('sender_name'); >
< span class =error><?php echo form_error('sender_name'); ?>< / span>
< label for =sender_email>电子邮件:< / label>
<?php echo form_input('sender_email'); >
< span class =error><?php echo form_error('sender_email'); ?>< / span>
< label for =subject>主题:< / label>
<?php echo form_input('subject'); >
< span class =error><?php echo form_error('subject'); ?>< / span>
< label for =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
的视图, it:
<?php
$ this-> load-& 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'=>'与我联系');
$ this-> load-> view('template',$ data);
}
您不需要创建库,创建一个模板视图,它将加载适当的视图和指定的视图,并将数据传递给它们。
I wrote a quick CI library class to render my pages so I wouldn't have to keep typing '$this->load->view' all the time and for DRY. Now when I re-render my contact form after passing in invalid data the error messages aren't showing up.
The library class:
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');
}
}
and the controller:
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
contact form view:
<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'); ?>
How can I render pages using this helper and still retrieve error messages from form_validation library?
Create a view called template.php
in your views folder with the following code in it:
<?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');
Then use the below controller code:
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);
}
You don't need to create a library to do this as you can just create a template view which will load the appropriate views and the specified view and pass the data along to them.
这篇关于当我使用另一个控制器渲染页面时,表单验证停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!