CodeIgniter的set_value()不重新填充 [英] CodeIgniter's set_value() not re-populating
问题描述
我已检查并重新检查我的代码,引用了整个网络中的CI文档和其他帖子,但是在验证失败后实现set_value()方法重新填充表单字段时失败。
也许我缺少一个非常基本的CI框架(我是相当新的),但你的见解会非常感激。
我有以下在我的控制器方法:
public function form_step2(){
//表单设置
this-> load-> helper('form');
$ this-> load-> helper('url');
$ this-> load-> library('form_validation');
$ data ['title'] = $ this-> base_title;
$ data ['base_url'] = base_url();
//验证设置 - 必须每步设置
$ this-> form_validation-> set_rules('request_type','Request Type','required');
* ...更多相同的set_rules()*
if($ this-> form_validation-> run()=== FALSE){
###验证失败或新表单
//获取表单元素数据
$ data ['request_types'] = $ this-> my_model-> get_form_data('request_type' );
* ...更多相同的get_form_data()调用加载表单元素*
//从视图模板生成页面
$ this-> load-> view ('templates / header',$ data);
$ this-> load-> view('templates / form_step2',$ data);
$ this-> load-> view('templates / footer');
} else {
###保存到数据库
$ this-> my_model-> set_data($ data);
redirect('my_model / success','refresh');
}
}
在我看来,不重新填充:
<?php echo form_open('my_model / form_step2',array('class'=> ;'form-inline')); >
< label for =fname>名字< / label>
< input type =textid =fnamename =fnamevalue =<?php echo set_value('fname');?& />
< input type =submitname =submitvalue =Submit/>
我无法想象这一个,所以先感谢您的帮助。
如果要使用 set_data()
,还需要使用 set_rules
$ b $ p 因为你已经注释掉了所有的set_rules,所以我不能确认这是
<$ c
$ c> $ this-> form_validation-> set_rules('fname','First name','trim | required');因此,如果你想用 name =fname
重新填充字段,你需要有 set_rules() code>,否则不会处理,因此
set_value('fname')
为空。
I've checked and re-checked my code, referencing the CI docs and other posts throughout the web, but I am unsuccessful at implementing the set_value() method for re-populating form fields after failed validation. Perhaps I am missing something very fundamental to the CI framework (I'm rather new to it), but your insight would be much appreciated.
I have the following in my controller method:
public function form_step2(){
//Form Setup
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$data['title'] = $this->base_title;
$data['base_url'] = base_url();
//Validation Settings - must be set per step
$this->form_validation->set_rules('request_type', 'Request Type', 'required');
*...more of the same set_rules()*
if ($this->form_validation->run() === FALSE) {
### Validation failed or New Form
// Get form element data
$data['request_types'] = $this->my_model->get_form_data('request_type');
*...more of the same get_form_data() calls for loading form elements*
//Generate Page from View Templates
$this->load->view('templates/header', $data);
$this->load->view('templates/form_step2', $data);
$this->load->view('templates/footer');
} else {
### Save to database
$this->my_model->set_data($data);
redirect('my_model/success','refresh');
}
}
And in my view, a snippet of the code that is not re-populating:
<?php echo form_open('my_model/form_step2', array('class'=>'form-inline')); ?>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="<?php echo set_value('fname'); ?>" />
<input type="submit" name="submit" value="Submit" />
I can't figure this one out, so thanks in advance for your help.
if you want to use set_data()
you need to also use set_rules
for that POST/GET field.
Since you've commented out all your set_rules I can not confirm that this is the issue but most likely it is.
please check if you have this line in your code
$this->form_validation->set_rules('fname', 'First name', 'trim|required');
So if you want to re-populate field with name="fname"
you need to have set_rules() // as line above
for it otherwise it won't process therefore set_value('fname')
is empty.
这篇关于CodeIgniter的set_value()不重新填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!