I am trying to setup Model validation on one field that only need to be checked if another field equals a particular value.
My first field is query which is a dropdown with many values, one value is 'Other' if this is selected then I need the second field 'query_other' to not be empty.
I have this setup in my Item Model:
public $validate = array(
'query' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'THE QUERY IS REQUIRED',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'query_other' => array(
'notempty' => array(
'rule' => array('if_query_other', 'query'),
'message' => 'REASON IS REQUIRED',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
I then have this custom function which is being called above.
function if_query_other(&$data, $check, $query_field) {
if($this->data[$this->name][$query_field] == 'Other' && $check == NULL)
{
return false;
}
else
{
return true;
}
}
It's not working, I am currently getting this error: Parameter 1 to Item::if_query_other() expected to be a reference, value given
CakePHP Version 2.3.6
Thanks
The error message is pretty clear, parameter 1 is passed by value, not by reference as your method signature requires. There is nothing to be passed by reference to a custom validation method, so simply remove the
&
, respectively remove the first parameter from the signature altogether.The first parameter passed to a custom validation method will always be the data of the field to validate (in
key => value
format), followed by possible parameters defined in therule
array, like for example your fieldname. So$check
would have never beennull
unless you would have definednull
in therule
array, ie'rule' => array('if_query_other', null)
, consequently your third parameter would have never been the fieldname.Long story short, you need to define two parameters only, the first will contain the data of the field to validate, the second one the additional value defined in the
rule
array.Here's an example, it checks whether the field passed in
$query_field
exists and whether its value equalsOther
, if it does it returns whether the value of the current field is not empty (I'm assuming the built-inValidation::notEmpty()
is sufficient enough for your "not empty" check needs). If the value of the$query_field
field doesn't equalOther
, then this rule will always validate successfully, ie the value then isn't required to be not empty.