簡體   English   中英

Codeigniter Grocery Crud更新字段?

[英]Codeigniter Grocery Crud update field?

$crud->set_rules('user_password', 'Password', 'trim|required|matches[konfirmpass]');
$crud->set_rules('konfirmpass', 'Konfirmasi Password', 'trim|required');

$crud->callback_edit_field('user_password',array($this,'_user_edit'));
$crud->callback_add_field('user_password',array($this,'_user_edit'));

回調函數:

function _user_edit(){
    return '<input type="password" name="user_password"/>  Confirmation password* : <input type="password" name="konfirmpass"/>';   
}

我的問題是,如果只有“密碼”不是空白,如何更新?

我已經安裝了CI 2.0.3和GC 1.1.4進行測試,因為您的代碼一目了然。 事實證明,這是和你的代碼一起工作的。 我使用GC修改了examples控制器中開箱即用的employees_management方法。 在數據庫中添加了user_password列,並將代碼添加到控制器中。

代碼既可以確保密碼字段匹配,也可以在提交時不為空。

  • 空白結果在"The Password field is required"
  • "The Password field does not match the konfirmpass field."結果"The Password field does not match the konfirmpass field."

也許如果這不適合你,你應該發布你的整個方法而不僅僅是規則和回調,這樣我們就可以看出是否還有其他問題。

工作

編輯

要編輯該字段,只有在編輯了密碼后才需要添加

$crud->callback_before_update( array( $this,'update_password' ) );

function update_password( $post ) { 
if( empty( $post['user_password'] ) ) {
    unset($post['user_password'], $post['konfirmpass']);
}

return $post;
}

但是,這可能意味着您需要刪除空密碼的驗證,具體取決於回調運行的順序(如果它們在表單驗證運行之前或之后)。 如果它們在表單驗證之前運行,則還需要運行對callback_before_insert()的調用,並在兩個回調中添加驗證規則。 顯然,插入將需要required規則,而更新則不會。

編輯2,編輯澄清1

經過調查,驗證在回調之前運行,因此您無法在回調函數中設置驗證規則。 要實現這一點,您需要使用一個名為getState()的函數,它允許您根據CRUD執行的操作添加邏輯。

在這種情況下,我們只想在添加行時使密碼字段成為required ,而在更新時則不需要。

因此,除了上面的回調update_password() ,您還需要將表單驗證規則包裝在狀態檢查中。

if( $crud->getState() == 'insert_validation' ) {
    $crud->set_rules('user_password', 'Password', 'trim|required|matches[konfirmpass]');
    $crud->set_rules('konfirmpass', 'Konfirmasi Password', 'trim|required');
}

如果CRUD正在插入,這將添加驗證選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM