所以我有一个用于两个功能/条件的表单:保存(添加)和更新现在我为我的输入文本设置了一个规则:$this->form_validation->set_rules('ID_user', 'User ID', 'required');
逻辑是这样的:如果用户处于保存状态,那么我的输入文本将被启用,因为他们将 tyoe 他们的 id,但如果用户处于更新状态,那么我的输入文本将被禁用,文本将是当前用户id,因为他们无法更改其用户 ID。
这是我的控制器中的保存(添加)和更新功能:
function add(){
//set common properties
$data['title'] = 'Tambah User baru';
$data['action'] = site_url('user/add');
$data['link_back'] = anchor('user/index/', 'Back to User list', array('class'=>'back'));
$this->_set_rules();
//run validation
if($this->form_validation->run() == false){
$data['message'] = '';
//bedakan add/update
$data['validate'] = 'add';
//set common properties
$data['title'] = 'Add new User';
//$data['message'] = '';
$data['user']['ID_user'] = '';
$data['user']['pass'] = '';
$data['user']['nama'] = '';
$data['user']['email'] = '';
$data['user']['active'] = '';
$data['link_back'] = anchor('user/index/', 'Lihat daftar User', array('class'=>'back'));
$this->load->view('user_form_v', $data);
}
else{
//save data
$user = array('ID_user'=>$this->input->post('ID_user'),
'pass'=>sha1($this->input->post('pass')),
'nama'=>$this->input->post('nama'),
'email'=>$this->input->post('email'),
'active'=>$this->input->post('active'),
'regis_date'=>date('Y-m-d H:i:s'));
$ID_user = $this->user_m->save($user);
//set form input nama = "id"
$this->validation->ID_user = $ID_user;
redirect('user/index/add_success');
}
}
function update($ID_user){
//set common properties
$data['title'] = 'Update user';
$this->load->library('form_validation');
//set validation properties
$this->_set_rules();
$data['action'] = ('user/update/'.$ID_user);
//bedakan add/update
$data['validate'] = 'update';
//run validation
if ($this->form_validation->run() == false){
$data['message'] = '';
$data['user'] = $this->user_m->get_by_id($ID_user)->row_array();
//set common properties
$data['title'] = 'Update User';
$data['message'] = '';
}
else{
//save data
$ID_user = $this->input->post('ID_user');
$user = array(
'pass'=>$this->input->post('pass'),
'nama'=>$this->input->post('nama'),
'email'=>$this->input->post('email'),
'active'=>$this->input->post('active'),
'regis_date'=>date('Y-m-d H:i:s'));
$this->user_m->update($ID_user, $user);
$data['user'] = $this->user_m->get_by_id($ID_user)->row_array();
//set user message
$data['message'] = 'Update User Success!';
}
$data['link_back'] = anchor('user/index/', 'Lihat daftar user', array('class'=>'back'));
//load view
$this->load->view('user_form_v', $data);
}
这是视图中的输入类型:
<input type="text" name="ID_user" class="text"
<?php if($validate!='add'){echo "disabled";} ?>
value="<?php echo (isset($user['ID_user']))?$user['ID_user']:""?>"/>
问题是:当我的用户处于更新条件并且他们想要更新时,会显示错误消息,因为 php 认为用户 ID 为空,事实是用户 ID 已经存在,我所做的只是打印用户 ID我的输入类型并禁用它
这是错误消息:The User ID field is required.