$check 值是 CakePHP 验证行为中的整个模型对象
我正在尝试在 Cake 中的多个模型中实现自定义验证行为。我对框架和 php 总体来说是新手,并且可能容易出现愚蠢的错误。
我遇到的问题是,我的自定义验证函数的第一个参数正在填充正在验证的模型,而不是文档似乎说应该存在的 array('fieldname'=>'input') 。我得到的是:
class Policy extends AppModel {
var $actsAs = array('JsonValidation');
var $validate = array(
'field1' => array(
'rule' => 'myValidationRule',
'message' => 'Please enter valid stuff.'
),
'field2' => array(
'rule' => myValidationRule',
'message' => 'Please enter valid my.'
));
....
class MyValidationBehavior extends ModelBehavior {
function myValidationRule($check, $allowEmpty = false){
debug($check); //prints out the Policy model
$input = array_shift($check); //errors since $check isn't an array
debug($input);
if($allowEmpty && trim($input) == "") return true;
if(isValid($input)) return false;
return true;
}
关于我做错了什么或者如何在这种情况下获得用户的输入有什么想法吗?
谢谢, 洛伦
工作解决方案:
var $validate = array(
'rules' => array(
'rule' => array('myValidationRule', true),
'message' => 'Please enter something valid.'
),
'rule_parms' => array(
'rule' => array('myValidationRule', true),
'message' => 'Please enter something valid.'
));
function myValidationRule($Model, $check, $allowEmpty){
$input = array_shift($check);
if($allowEmpty && trim($input) == "") return true;
if(valid($input) == null) return false;
return true;
}
I'm trying to implement a custom validation behavior across several models in Cake. I'm new to the framework and to php in general, and may be prone to silly errors.
The problem I'm running into is that the first parameter to my custom validation function is being filled with the model that is being validated instead of array('fieldname'=>'input') which the documentation seems to say should be there. What I've got is:
class Policy extends AppModel {
var $actsAs = array('JsonValidation');
var $validate = array(
'field1' => array(
'rule' => 'myValidationRule',
'message' => 'Please enter valid stuff.'
),
'field2' => array(
'rule' => myValidationRule',
'message' => 'Please enter valid my.'
));
....
class MyValidationBehavior extends ModelBehavior {
function myValidationRule($check, $allowEmpty = false){
debug($check); //prints out the Policy model
$input = array_shift($check); //errors since $check isn't an array
debug($input);
if($allowEmpty && trim($input) == "") return true;
if(isValid($input)) return false;
return true;
}
Any ideas on what I'm doing wrong or how I can get the user's input in this context?
Thanks,
Loren
Working solution:
var $validate = array(
'rules' => array(
'rule' => array('myValidationRule', true),
'message' => 'Please enter something valid.'
),
'rule_parms' => array(
'rule' => array('myValidationRule', true),
'message' => 'Please enter something valid.'
));
function myValidationRule($Model, $check, $allowEmpty){
$input = array_shift($check);
if($allowEmpty && trim($input) == "") return true;
if(valid($input) == null) return false;
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如书本所述,第一个参数行为方法始终是对调用模型的引用。
As stated by the book here, the first parameter of a behavior method is always a reference to the calling model.