所以我有一個用於兩個函數/條件形式:保存(添加)和更新 現在我設定的規則來我輸入文本:$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在我的輸入型和殘疾人它 氏s是錯誤消息:The User ID field is required.
當比較boolean類似TRUE或FALSE時,你必須使用'==='ex,如果($ true === TRUE) – tomexsans
@tomexsans我不這麼認爲,事情順利地用「==」運行 謝謝:D –
是的,它流暢的運行,但良好的做法使用===當比較布爾閱讀此http://codeigniter.com/user_guide/general/styleguide.html#comparing_return_values_and_typecasting – tomexsans