cakephp: model validation based on another field

2019-08-31 01:26发布

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

1条回答
干净又极端
2楼-- · 2019-08-31 01:36

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 the rule array, like for example your fieldname. So $check would have never been null unless you would have defined null in the rule 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 equals Other, if it does it returns whether the value of the current field is not empty (I'm assuming the built-in Validation::notEmpty() is sufficient enough for your "not empty" check needs). If the value of the $query_field field doesn't equal Other, then this rule will always validate successfully, ie the value then isn't required to be not empty.

...

App::uses('Hash', 'Utility');
App::uses('Validation', 'Utility');

class Item extends AppModel
{
    ...

    public function if_query_other($check, $query_field)
    {
        if(Hash::get($this->data[$this->alias], $query_field) === 'Other')
        {
            return Validation::notEmpty(current($check));
        }

        return true;
    }
}
查看更多
登录 后发表回答