【发布时间】:2015-06-30 22:36:15
【问题描述】:
在 MembersTable.php 我有:
$validator
->notEmpty('member_selected')
->add('member_selected', ['rule' => ['inList', [0,1], false]]);
return $validator;
我从 cakePHP 3.0.7 收到以下非常神秘的消息
Unable to call method "" in "default" provider for field member_selected" InvalidArgumentException
⟩ Cake\Validation\ValidationRule->process CORE\src\Validation\Validator.php, line 656
⟩ Cake\Validation\Validator->_processRules CORE\src\Validation\Validator.php, line 136
⟩ Cake\Validation\Validator->errors CORE\src\ORM\Marshaller.php, line 181
⟩ Cake\ORM\Marshaller->_validate CORE\src\ORM\Marshaller.php, line 428
⟩ Cake\ORM\Marshaller->merge CORE\src\ORM\Table.php, line 2034
⟩ Cake\ORM\Table->patchEntity APP/Controller\MembersController.php, line 104
⟩ App\Controller\MembersController->edit [internal function]
⟩ call_user_func_array CORE\src\Controller\Controller.php, line 411
⟩ Cake\Controller\Controller->invokeAction CORE\src\Routing\Dispatcher.php, line 114
⟩ Cake\Routing\Dispatcher->_invoke CORE\src\Routing\Dispatcher.php, line 87
⟩ Cake\Routing\Dispatcher->dispatch ROOT\webroot\index.php, line 37
如果我删除 ->add('member_selected', ['rule' => ['inList' ... 一切正常,但当然验证没有正确完成。我没有使用 if 'inList' 的示例,但我认为我已经正确设置了它。我必须从该表的其他几个字段中删除其他验证器规则才能通过此错误消息(例如 naturalNumber)。
我还将引用另一个名为“cakePHP 3.0.7 - Baked edit function failed to perform save in patchEntity with Marshaller::merge() error”的stackoverflow问题。
可以通过更改其中一个字段的 $validator 设置来“解决”问题...创建这些错误的一种方法是更改字段名称或类型,而忘记在实体 $_accessible 中更改它,并且函数validationDefault() 中的表$validator。也不要忘记 $rules->adds in Table::buildRules()
当然……为什么有人要更改字段名称或类型?哦,好吧,如果 cakePHP 能提供一些关于实际问题的信息(预期的字段不存在,或者指定的字段不应该存在),那就太好了。
无论如何,既然我已经发泄了一点,验证器在崩溃时并没有报告任何有用的信息。我希望有人在关注stackoverflow,以便解决它。
说了这么多,我还是想知道为什么'inList'规则不起作用?
为什么 inList 和 naturalNumber 对我不起作用!
add public
add( string $field , array|string $name , array|Cake\Validation\ValidationRule $rule [] )
Adds a new rule to a field's rule set. If second argument is an array then rules list for the field will be replaced with second argument and third argument will be ignored.
Example:
$validator
->add('title', 'required', ['rule' => 'notBlank'])
->add('user_id', 'valid', ['rule' => 'numeric', 'message' => 'Invalid User'])
$validator->add('password', [
'size' => ['rule' => ['lengthBetween', 8, 20]],
'hasSpecialCharacter' => ['rule' => 'validateSpecialchar', 'message' => 'not valid']
]);
好吧,这不是真的!你不能忽略第二个参数!如果我总是指定第二个参数然后第三个参数指定规则,那么验证期间的所有错误(就我而言是错误报告)都会消失!
感谢所有可能阅读并提供帮助的人,
【问题讨论】:
标签: validation cakephp cakephp-3.0