⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Change value before validate



ENSASKE
  • profile picture
  • Member

Posted 01 February 2017 - 19:40 PM

Hello,

 

How can I change a value before validate? I know I can use the "before insert callback", but its need to pass validation first.

 

Example: My form has 3 fields "Name, Email, User", they are mandatory for the database, but the User is optional. If is empty, i want to use the value inside Email as a user.

$crud->fields('name',email','user');

How to do that? I need to use my validation rules: (Email and User rules are similar)

$crud->set_rules('user','User','required|is_unique[user.user]|_callback1|_callback2');

If I use before insert, doenst work, validation says "User is required"

$crud->callback_before_insert(array($this,'_default_user_callback'));
			
function _default_user_callback($post_array){
 if($post_array['user'] == ''){
  $post_array['user'] = $post_array['email'];
 }
 return $post_array;
}

I have to break the validation? can I use the user as "invisible" and later add the value?

 

Thanks for any advice.


Amit Shah
  • profile picture
  • Member

Posted 02 February 2017 - 08:38 AM

Well there is a turn around solution to by pass the user validation.

 

1. check for the state of the post ... if the current state is validate

2. If the value of the user in the post is blank - set the value in $_REQUEST['user'] / $_POST['user'] to be the email address. 

 

This way -  what happens - when the code is going to check for validation - it will use the new value instead of blank.

 

OR - in the case if we discover the value is blank - we dont add the validation rule (we add the validation rule only when the value is filled by the user)

 

Happy GCing :)


zoodov
  • profile picture
  • Member

Posted 03 February 2017 - 08:33 AM

I have almost the same problem.

 

I have a row with columns: order_count, income_count. First one fills automatically, and the second fills with user.

 

And i want next: when user edits the row, income_count should have default value the same order_count.

Then user changes it, if he needs, and sends form to server.

 

What callback i should use? And when?

 

Thanks)

 

I hope you understand me :)


zoodov
  • profile picture
  • Member

Posted 03 February 2017 - 09:26 AM

And i have additional question - can i control/validate user input field with another field value?


ENSASKE
  • profile picture
  • Member

Posted 04 February 2017 - 11:17 AM

Well there is a turn around solution to by pass the user validation.

 

1. check for the state of the post ... if the current state is validate

2. If the value of the user in the post is blank - set the value in $_REQUEST['user'] / $_POST['user'] to be the email address. 

 

This way -  what happens - when the code is going to check for validation - it will use the new value instead of blank.

 

OR - in the case if we discover the value is blank - we dont add the validation rule (we add the validation rule only when the value is filled by the user)

 

Happy GCing :)

 

Thank you so much! it works perfectly.

 

I totally forgot about the state, I read it somewhere, i'm a newbie ^_^