我正在开发一个类别模块,我正在使用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);
答案 0 :(得分:4)
更改此代码的位置。
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');
}
as
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'] = '';
}
肯定会有用