CodeIgniter set_value() 不工作?
Posted
技术标签:
【中文标题】CodeIgniter set_value() 不工作?【英文标题】:The CodeIgniter set_value() not working? 【发布时间】:2014-06-26 18:43:49 【问题描述】:我正在开发一个使用 CI 验证规则的类别模块。当验证规则失败时,我希望表单具有与用户输入的相同的值。在验证规则失败后,我使用 set_value 函数在表单中加载值。但是每次验证失败时,值都会设置为空白。
这是我的代码。我已经在构造函数中设置了验证规则。
$this->form_validation->set_rules($this->validation_rules);
if ($this->input->post())
$db_array=array(
'CategoryName' => $this->input->post('CategoryName'),
'ParentID' => $this->input->post('ParentID'),
'Status' => $this->input->post('Status')
);
if($this->form_validation->run() != false)
if($id)
$result = $this->category_m->update($db_array,$id);
if(isset($result['error'])&& count($result['error']>0))
foreach($result['error'] as $error)
$this->session->set_flashdata('error',$error);
if($result['success'])
$action['class'] = 'success';
$action['msg'] = 'Data added successfully.';
$this->session->set_flashdata('success',$action);
else
$action['class'] = 'error';
$action['msg'] = 'Invalid values to the form!';
$this->session->set_flashdata('error',$action);
else
$result = $this->category_m->insert($db_array);
if(isset($result['error']) && count($result['error']>0))
foreach($result['error'] as $error)
$this->session->set_flashdata('error',$error);
if($result['success'])
$action['class'] = 'success';
$action['msg'] = 'Data added successfully.';
$this->session->set_flashdata('sucess',$action);
else
$action['class'] = 'error';
$action['msg'] = 'Invalid values to the form!';
$this->session->set_flashdata('error',$action);
if(isset($this->input->post['frmSubmit']))
redirect('category/form/'.$result['CategoryID']);
else
redirect('category/index');
else
$categories = $this->category_m->getCategoryName(true);
$data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category');
$data['CategoryName'] = set_value('CategoryName');
$data['ParentID'] = set_value('ParentID');
$data['Status'] = set_value('Status');
if($id)
$category = $this->category_m->getCategory($id);
$data['CategoryName'] = $category->CategoryName;
$data['ParentID'] = $category->ParentID;
$data['Status'] = $category->Status;
else
$data['CategoryName'] = '';
$data['ParentID'] = '';
/*foreach($this->validation_rules as $rule)
//$data['category']->$rule['field'] = set_value($rule['field']);
*/
$categories = $this->category_m->getCategoryName(true, $id);
$data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category');
$this->load->view('form',$data);
【问题讨论】:
这里的代码相当多。请删除与问题无关的代码。 【参考方案1】:改变这段代码的位置。
else
$categories = $this->category_m->getCategoryName(true);
$data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category');
$data['CategoryName'] = set_value('CategoryName');
$data['ParentID'] = set_value('ParentID');
$data['Status'] = set_value('Status');
作为
if($this->input->post())
$categories = $this->category_m->getCategoryName(true);
$data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category');
$data['CategoryName'] = set_value('CategoryName');
$data['ParentID'] = set_value('ParentID');
$data['Status'] = set_value('Status');
else if($id)
$category = $this->category_m->getCategory($id);
$data['CategoryName'] = $category->CategoryName;
$data['ParentID'] = $category->ParentID;
$data['Status'] = $category->Status;
else
$data['CategoryName'] = '';
$data['ParentID'] = '';
一定会成功的
【讨论】:
以上是关于CodeIgniter set_value() 不工作?的主要内容,如果未能解决你的问题,请参考以下文章
使用 set_value 的 Textarea 未填充 Codeigniter
我应该使用 set_value() 在 CodeIgniter 中重新填充表单吗
Codeigniter set_value() 并填充表单值
php codeigniter - 表单助手 - 如何在无线电输入上实现 set_value