我需要在form_dropdown中使用set_value显示form_error以进行验证,但它对我不起作用。
这是模型(model_home.php):
public function get_dropdown() {
$result = $this->db->select('designation_id, designation')->get('designation')->result_array();
$dropdown = array();
foreach($result as $r) {
$dropdown[$r['designation_id']] = $r['designation'];
}
return $dropdown;
}
这是控制器(home.php):
public function viewAddEmployeeForm() {
$this->load->model('Model_home');
$data = array();
$data['dropdown'] = $this->Model_home->get_dropdown();
$this->load->view('imports/header');
$this->load->view('imports/menu');
$this->load->view('emp_add', $data);
}
public function saveEmployee() {
$this->load->model('Model_home');
$data = array();
$data['dropdown'] = $this->Model_home->get_dropdown();
$rules = array(
array('field'=>'emp_desig','label'=>'Designation','rules'=>'trim|required')
);
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == FALSE) {
$this->load->view('emp_add', $data);
} else {
$this->load->model('Model_home');
$p = new Model_home();
$p->designation_id = $this->input->post('emp_desi');
if($p->designation_id == 1) {
$p->user_type = 0;
} else {
$p->user_type = 1;
}
$result = $p->saveEmployee();
if (!$result) {
echo mysqli_error($result);
}
else {
redirect('home/goSettings', 'refresh');
}
}
}
这是视图(emp_add.php):
<?php echo form_open('home/saveEmployee',array('class'=>'form-horizontal'));?>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Designation <span class="required"><font size="3" color="red">*</font></span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<?php echo form_dropdown('emp_desi', $dropdown, '', 'class="form-control" id="emp_desi" name="emp_desig" value="<?php echo set_value('emp_desig') ?>"'); ?>
<span style="color: red;"><?php echo form_error('emp_desig'); ?></span>
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<button type="submit" class="btn btn-success" name="emp_submit" id="emp_submit">Submit</button>
</div>
</div>
</form>
如何在form_dropdown中显示form_error,尤其是set_value?谢谢你的时间。
答案 0 :(得分:0)
阅读Form Helper doc。您的form_dropdown()使用不正确。您应该传入一个选项数组作为第三个参数,而不是使用set_value()。