【发布时间】:2015-10-10 12:23:57
【问题描述】:
我正在 codeigniter 中进行表单验证我在我的控制器中为我的表单创建验证规则。我有两个选择框,我想验证两个现在问题是选择框都显示相同的错误消息我想为每个选择框显示不同的消息。
主要问题是错误消息会为两个选择框显示相同的城市错误。我希望不同的选择框有不同的错误消息。
还有一个问题:: 当我收到任何一个字段的验证错误消息时,整个表单都是空的。当我对任何字段出现验证错误时,我想这样显示,其他字段数据将在那里。
这是我的控制器:
public function addEmployeeController()
{
$this->load->library('form_validation');
$abcd = $this->input->post('city_id');
$abc = $this->input->post('desi_id');
$this->form_validation->set_rules('emp_name', 'Name', 'trim|required');
$this->form_validation->set_rules('emp_jdate', 'Joining Date', 'trim|required');
$this->form_validation->set_rules('emp_addr', 'Address', 'trim|required');
$this->form_validation->set_rules('emp_sal', 'Salary', 'trim|required');
$this->form_validation->set_rules('emp_descr', 'Description', 'trim|required');
$this->form_validation->set_rules('emp_mobile', 'Mobile Number', 'trim|required|min_length[10]');
$this->form_validation->set_rules('city_id', 'City', 'trim|required|callback_select_validate');
$this->form_validation->set_rules('desi_id', 'Designation', 'trim|required|callback_select_validate');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
if($query = $this->Emp_model->addEmployeeModel('$data'))
{
$data['main_content'] = 'signup_successful';
$this->load->view('emp_view', $data);
}
else
{
$this->load->view('emp_view');
}
}
}
public function select_validate($abcd)
{
// 'none' is the first option that is default "-------Choose City-------"
if($abcd == "none")
{
$this->form_validation->set_message('select_validate', 'Please Select Your City.');
return false;
}
else
{
// User picked something.
return true;
}
}
public function select_validate1($abc)
{
// 'none' is the first option that is default "-------Choose City-------"
if($abc = "none")
{
$this->form_validation->set_message('select_validate', 'Please Select Your Designation.');
return false;
}
else
{
// User picked something.
return true;
}
}
在视图中我有这样的选择框。当我在不选择选择框的情况下提交表单时,它会显示“请选择您的城市”的错误消息 对于两个选择框。我想显示不同的信息。
<p>
<lable for="desi_id">Designation:</lable><?php echo form_error('desi_id'); ?>
<select name="desi_id">
<option selected="selected" value="none">Select Post</option>
<?php foreach ($records as $row) { ?>
<option value="<?php echo $row->desi_id ?>"><?php echo $row->post_name ?></option>
<?php } ?>
</select>
</p>
城市: 选择城市 city_id?>">city_name?>
【问题讨论】:
标签: php forms codeigniter validation drop-down-menu