对于 codeigniter 中的 select 框,我的表单验证无法正常工作
my form validation is not working properly for select box in codeigniter
我正在 codeigniter 中进行表单验证我在我的控制器中为我的表单创建了验证规则。我有两个 select 框,我想验证这两个框,现在问题是两个 select 框都显示相同的错误消息 我想为每个 select 框显示不同的消息。
主要问题是错误消息会为两个 select 框显示相同的城市错误。我想要不同的 select 框显示不同的错误消息。
还有一个问题 :: 当我收到任何一个字段的验证错误消息时,整个表单都为空。我想这样显示,当我收到任何字段的验证错误时,其他字段数据将在那里。
这是我的控制器:
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;
}
}
在视图中我有这样的 select 框。当我在没有 selecting select 框的情况下提交表单时,它显示 "Please Select Your City" 的错误消息
对于 select 框。我想显示不同的信息。
<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>
城市:
Select 城市
city_id?>">city_name?>
您好,您需要定义不同的消息:
留言:
$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_validate1');
$this->form_validation->set_message('city_id', 'Your message');
$this->form_validation->set_message('desi_id', 'Your other message');
其他字段为空:
您必须在所有文件中使用 set_value('field_name')。
<input type="text" name="city" value="<?php echo set_value('city'); ?>" />
你的小错误:
callback_select_validate 和 callback_select_validate1
$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_validate1');
希望这对您有所帮助:)
我正在 codeigniter 中进行表单验证我在我的控制器中为我的表单创建了验证规则。我有两个 select 框,我想验证这两个框,现在问题是两个 select 框都显示相同的错误消息 我想为每个 select 框显示不同的消息。
主要问题是错误消息会为两个 select 框显示相同的城市错误。我想要不同的 select 框显示不同的错误消息。
还有一个问题 :: 当我收到任何一个字段的验证错误消息时,整个表单都为空。我想这样显示,当我收到任何字段的验证错误时,其他字段数据将在那里。
这是我的控制器:
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;
}
}
在视图中我有这样的 select 框。当我在没有 selecting select 框的情况下提交表单时,它显示 "Please Select Your City" 的错误消息 对于 select 框。我想显示不同的信息。
<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>
城市: Select 城市 city_id?>">city_name?>
您好,您需要定义不同的消息:
留言:
$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_validate1');
$this->form_validation->set_message('city_id', 'Your message');
$this->form_validation->set_message('desi_id', 'Your other message');
其他字段为空: 您必须在所有文件中使用 set_value('field_name')。
<input type="text" name="city" value="<?php echo set_value('city'); ?>" />
你的小错误: callback_select_validate 和 callback_select_validate1
$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_validate1');
希望这对您有所帮助:)