Jump to content


Change value before validate

default value validation validate callback

  • Please log in to reply
4 replies to this topic

#1 ENSASKE

ENSASKE

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 01 February 2017 - 07: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.



#2 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

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 :)



#3 zoodov

zoodov

    Newbie

  • Members
  • Pip
  • 2 posts

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 :)



#4 zoodov

zoodov

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 03 February 2017 - 09:26 AM

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



#5 ENSASKE

ENSASKE

    Newbie

  • Members
  • Pip
  • 4 posts

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 ^_^







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users