CodeIgniter Forums

Full Version: Form Validation Callback With Parameter from Array ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
not sure if this is possible but thought i would ask anyway
form validation - thats validating an array of data - in this case a group of products - one rule has a callback. like

PHP Code:
$this->form_validation->set_rules('productid[]''Product ID''required');

$this->form_validation->set_rules('productsku[]''Product SKU''callback_sku_check'); 

and then the callback goes to the method: sku_check($str)

all that works fine, churns through and checks an array of products no problem. Now is there a way to pass the correct Product ID from the array to the callback method? So i could have

I think the answer is no. You cannot pass additional variables in the callback string 'callback_sku_check'.

That has caught me out a few times when I think 'I can do a callback to validate that' and then find I can't.

The only thing you can do is access the post array directly in the callback. However in your case you will be accessing the entire array, and would have to identify which term the callback is currently being called on, which again I do not know how you would do that.

To do whatever you needed to check here, I would do this as a post validation logic check. So you have passed validation, so all the data is valid, now you need to do some further checks before proceeding, but those fails are not passed as validation errors, but as a normmal message to the user, displayed in your form however you would normally display a user message. (I hope that makes sense).

As a for instance, a login might pass validation for the form sending an email and a password, but would fail the logic check done post validation that the user is actually registered, so you would pass back to the login form with the error message 'You are not yet registered'.

Hope that helps,

Do a json encode on the array before passing it to the callback method, in your callback method do a json decode to put it back into an array.