cakephp:基于另一个字段的模型验证 [英] cakephp: model validation based on another field
问题描述
我试图在一个字段上设置模型验证,只需要检查另一个字段是否等于特定值。
I am trying to setup Model validation on one field that only need to be checked if another field equals a particular value.
我的第一个字段是query,下拉列表中有许多值,一个值为其他,如果选中,则我需要第二个字段query_other不为空。
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.
项目型号:
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;
}
}
这不工作,我目前得到这个错误: 参数1到项目:: if_query_other()预期为参考,给定值
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版本2.3.6
CakePHP Version 2.3.6
感谢
推荐答案
错误消息很清楚,参数1传递的值, 按参考作为方法签名要求。没有什么可以通过引用自定义验证方法传递,因此只需删除&
,分别从签名中删除第一个参数。
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.
传递给自定义验证方法的第一个参数将始终是要验证的字段的数据(在 key => value
格式) ,其次是在规则
数组中定义的可能参数,例如您的字段名。因此 $ check
从不会 null
,除非你定义 null
在规则
数组,即'rule'=>数组('if_query_other',null)
,因此您的第三个参数从未是字段名。
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.
这里有一个例子,它检查在 $ query_field
中传递的字段是否存在以及它的值是否等于其他
,如果是这样,它返回当前字段的值是否不为空(我假设内置 Validation :: notEmpty()
足够您的非空检查需求)。如果 $ query_field
字段的值不等于其他
,则此规则将始终成功验证,即值不必为空。
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;
}
}
这篇关于cakephp:基于另一个字段的模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!