Welcome Guest, Not a member yet? Register   Sign In
Validation callback with parameter?
#1

[eluser]eger[/eluser]
I am trying to validate a field by making it required. But only if another field is set to a certain value. For example, a callback like "callback__required_if_field[type==2]" so I could validate that the filed is required only if field name 'type' is set to '2'.

Anyone have any examples of this? I suppose I could also just require the callback to accept one parameter and then in the private function use a variable for the is required of the other field also. Not quite sure how to do this though...
#2

[eluser]xwero[/eluser]
the validation library assumes you only need one parameter, so everything between the square brackets is the parameter value.

Instead of code you better use a comma seperated list callback__required_if_field[type,2], then you can explode it and do the check
Code:
function _required_if_field($str,$param)
{
   $temp = explode(',',$param);

   if($_POST[$temp[0]] == $temp[1] && empty($str))
   {
      return false;
   }

   return true;
}
#3

[eluser]eger[/eluser]
This makes sense. However I am trying to do a simple test with callback_required_if_field[something] and it does not appear that 'something' is being passed to the callback as the second parameter. Is this supposed to work? Is it working for you in your example?

I am basically testing your code and $param comes up empty Sad
#4

[eluser]Randell Benavidez[/eluser]
[quote author="xwero" date="1234275602"]the validation library assumes you only need one parameter, so everything between the square brackets is the parameter value.

Instead of code you better use a comma seperated list callback__required_if_field[type,2], then you can explode it and do the check
Code:
function _required_if_field($str,$param)
{
   $temp = explode(',',$param);

   if($_POST[$temp[0]] == $temp[1] && empty($str))
   {
      return false;
   }

   return true;
}
[/quote]

This is somewhat wrong. You're supposed to have only one parameter for the callback. So if you call
Code:
callback__required_if_field[type, 2]
, your function declaration should only have one parameter like
Code:
function _required_if_field($param)
.
Code:
$param
should then be exploded to contain 'type' and '2'. Note that 'type' and '2' are values and not variables.
#5

[eluser]Randell Benavidez[/eluser]
[quote author="eger" date="1234307018"]This makes sense. However I am trying to do a simple test with callback_required_if_field[something] and it does not appear that 'something' is being passed to the callback as the second parameter. Is this supposed to work? Is it working for you in your example?

I am basically testing your code and $param comes up empty Sad[/quote]

It would be easier to figure out what's wrong if you can post your code snippet of the set_rules and the callback function itself.
#6

[eluser]Randell Benavidez[/eluser]
Sorry for spamming.

Note that if you want use the submitted field values inside the callback function, you do not need to pass them as parameters to the callback function. Simply call $this->input->post('<field name>') inside the callback function to get their values.




Theme © iAndrew 2016 - Forum software by © MyBB