在UsersTable类中,我尝试在CakeBook之后实现自定义验证,但出现错误消息Object of class App\Model\Table\UsersTable could not be converted to string [CORE/src/Validation/ValidationRule.php, line 128]
。下面是我在UsersTable.php中的代码。
class UsersTable extends Table{
public function validationDefault(Validator $validator){
$validator->add(
"password",[
"notEmpty"=>[
"notEmpty"
],
"custom"=>[
"rule"=>[$this,"customFunction"],
"message"=>"foo"
]
]
);
}
public function customFunction($value,$context){
//some logic here
}
}
ValidationRule.php
在核心CakePHP库中查看时,我注意到array_shift()
(在第185行)正在采用的第一个元素[$this,"customFunction"]
,即,$this
并将其分配给$value
。但实际上$value
应该是[$this,"customFunction"]
。因此,为了使我的代码能够正常工作,我需要再添加一个嵌套[$this,"customFunction"]
(现在是[[$this,"customFunction"]]
)。我会误会某些东西还是这种错误?
UPD:现在已解决此问题。
我认为您已经正确地发现了问题,似乎是CakePHP期望rule
键值位于
[string or callable, ...args]
在数组中时的格式,即它不测试该值本身是否已经是可调用的。
该文档说,非嵌套变体应该可以工作,因此您可能希望将此报告为错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句