如何在php codeigniter中编辑时使验证字段可选?

时间:2014-06-24 07:06:34

标签: php codeigniter

我有一个要添加和更新的表单。所以,我想将一个字段显示为可选字段,就像我在添加它时必须显示验证错误,并且在编辑相同字段时不要求验证错误。 这是代码

$this->load->library('form_validation');

            $this->form_validation->set_rules('title', 'Job Title', 'trim|required|min_length[5]');
            $this->form_validation->set_rules('description', 'Job Description', 'trim|required');
            $this->form_validation->set_rules('qualification', 'Job Qualification', 'trim|required');
            $this->form_validation->set_rules('mskills', 'Job Mandatory Skills', 'trim|required');
            $this->form_validation->set_rules('askills', 'Job Advantage Skills', 'trim');
            $this->form_validation->set_rules('minexp', 'Job Min Experience', 'trim|required');
            $this->form_validation->set_rules('maxexp', 'Job Max Experience', 'trim|required');
            $this->form_validation->set_rules('postedon', 'Job Posted On', 'trim|required');
            $this->form_validation->set_rules('postedby', 'Job Posted By','trim|required');
            $this->form_validation->set_rules('salary', 'Job Salary', 'trim|required');

            if($this->form_validation->run() == FALSE)
            {
                echo json_encode(array('st'=>0, 'msg' => validation_errors()));
            }

这里我想将可选字段显示为

$this->form_validation->set_rules('askills', 'Job Advantage Skills', 'trim');

1 个答案:

答案 0 :(得分:0)

您需要设置检查条件以编辑或添加触发器。创建一个隐藏字段并检查它的编辑模式或添加模式并传递值隐藏

<input type='hiddden' name='checkform' value='add/edit'> // value change on runtime

然后检查

if($this->input->post('checkform') == 'add')
  $this->form_validation->set_rules('askills', 'Job Advantage Skills', 'trim|required');
else 
  $this->form_validation->set_rules('askills', 'Job Advantage Skills', 'trim');