【发布时间】:2014-08-21 13:52:10
【问题描述】:
在 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
}
}
查看CakePHP 核心库中的ValidationRule.php,我注意到array_shift()(第185 行)正在获取[$this,"customFunction"] 的第一个元素,即$this,并将其分配给$value。但实际上$value 应该是[$this,"customFunction"]。因此,为了让我的代码正常工作,我需要在[$this,"customFunction"] 中再添加一个嵌套(所以现在是[[$this,"customFunction"]])。我误解了什么还是这是某种错误?
UPD:此问题现已修复。
【问题讨论】:
标签: cakephp cakephp-3.0