简体   繁体   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'));

callback function: 回调函数:

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

My question is how to update if only "password" not blank? 我的问题是,如果只有“密码”不是空白,如何更新?

I've installed CI 2.0.3 and GC 1.1.4 to test because at a glance your code looked fine. 我已经安装了CI 2.0.3和GC 1.1.4进行测试,因为您的代码一目了然。 As it turns out, it is and your code works. 事实证明,这是和你的代码一起工作的。 I modified the out of the box employees_management method in the examples controller with GC. 我使用GC修改了examples控制器中开箱即用的employees_management方法。 Added a user_password column to the database and added your code to the controller. 在数据库中添加了user_password列,并将代码添加到控制器中。

The code both ensures that the password fields match and that they're not empty when submit. 代码既可以确保密码字段匹配,也可以在提交时不为空。

  • Empty results in "The Password field is required" 空白结果在"The Password field is required"
  • Mismatched results in "The Password field does not match the konfirmpass field." "The Password field does not match the konfirmpass field."结果"The Password field does not match the konfirmpass field."

Perhaps if this isn't working for you, you should post your entire method instead of just the rules and callbacks so we can see if there are any other problems. 也许如果这不适合你,你应该发布你的整个方法而不仅仅是规则和回调,这样我们就可以看出是否还有其他问题。

工作

Edit 编辑

To edit the field, only if the password has been edited you need to add 要编辑该字段,只有在编辑了密码后才需要添加

$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;
}

This however may mean that you need to remove the validation for empty password depending on which order the callbacks run (if they're before or after the form validation runs). 但是,这可能意味着您需要删除空密码的验证,具体取决于回调运行的顺序(如果它们在表单验证运行之前或之后)。 If they run before the form validation, you'll need to also need to run a call to callback_before_insert() and add your validation rules within the two callbacks. 如果它们在表单验证之前运行,则还需要运行对callback_before_insert()的调用,并在两个回调中添加验证规则。 Insert obviously will need the required rule, and update won't. 显然,插入将需要required规则,而更新则不会。

Edit 2, Clarification of Edit 1 编辑2,编辑澄清1

Having looked into it, the validation runs before the callbacks, so you can't set validation rules in the callback functions. 经过调查,验证在回调之前运行,因此您无法在回调函数中设置验证规则。 To achieve this you'll need to use a function called getState() which allows you to add logic based on the action being performed by the CRUD. 要实现这一点,您需要使用一个名为getState()的函数,它允许您根据CRUD执行的操作添加逻辑。

In this case, we only want to make the password field required when we are adding a row, and not required when updating . 在这种情况下,我们只想在添加行时使密码字段成为required ,而在更新时则不需要。

So in addition to the above callback update_password() , you will need to wrap your form validation rules in a state check. 因此,除了上面的回调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');
}

This will add the validation options if the CRUD is inserting. 如果CRUD正在插入,这将添加验证选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM