Codeigniter Grocery Crud update field? Codeigniter Grocery Crud update field? codeigniter codeigniter

Codeigniter Grocery Crud update field?


I've installed CI 2.0.3 and GC 1.1.4 to test because at a glance your code looked fine. 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. Added a user_password column to the database and added your code to the controller.

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"
  • Mismatched results in "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.

Working

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. Insert obviously will need the required rule, and update won't.

Edit 2, Clarification of Edit 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.

In this case, we only want to make the password field required when we are adding a row, and not required when updating.

So in addition to the above callback update_password(), you will need to wrap your form validation rules in a state check.

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.