重写 CakePHP 中的验证方法
我想 ovveride CakePHP 中的默认 url() 验证方法,因为它不允许在 url 中使用 ~ 。我认为在 AppModel 中声明一个 url() 方法就足够了,但似乎核心方法相对于用户定义的方法具有优先权。
我认为(但我没有尝试过)一种可能的方法是使用
$validate = array('url' => array(
'rule' => array('Userdefined', 'url'),
'message' => 'This is not an URL!!!'
));
或类似的东西(正确的语法是什么?)。但这并不完全令人满意。
事实上,我将 $validate 变量作为 JSON 对象传递给我的 javascript,然后相应地进行客户端验证。基本上我已经用 JavaScript 重写了 CakePHP 验证 automagic 的一部分。所以我真的很想这样做,
$validate = array('url' => array(
'rule' => 'url',
'message' => 'This is not an URL!!!'
));
以免破坏客户端验证。
编辑:事实证明我错了。问题是 Validation 中的方法与 Model 中的方法的调用方式不同,因此在复制/粘贴时必须注意。
第一个区别是 $check 现在将是一个数组而不是字符串,但我已经弄清楚了。我没有意识到的是另一个参数数组被传递给模型中的验证方法。由于 url() 的签名结果
url($check, $strict = false)
是 $strict 始终具有 true 值,因此需要带有协议前缀的完整 URL。看到带有波形符的预期 URL 没有得到验证,我认为问题在于 CakePHP 仍然使用旧方法。
I want to ovveride the default url() validation method in CakePHP, since it does not allow the use of ~ inside urls. I thought it would be enough to declare a url() method in AppModel, but it seems that core methods have the precedence with respect to user defined ones.
I think (but I have not tried) one possible way would be to use
$validate = array('url' => array(
'rule' => array('Userdefined', 'url'),
'message' => 'This is not an URL!!!'
));
or something like that (what is the correct sintax?). But this is not completely satisfying.
Indeed I pass the $validate variable as a JSON object to my javascript, and then I do client validation accordingly. Basically I have rewritten part of the CakePHP validation automagic in javascript. So I really want to have
$validate = array('url' => array(
'rule' => 'url',
'message' => 'This is not an URL!!!'
));
in order not to break client-side validation.
EDIT: It turns out I were wrong. The problem is that methods in Validation are called differently from methods in Model, so one has to pay attention when copying/pasting.
The first difference is that $check will now be an array instead of a string, but this I already figured out. What I did not realize is that another array of parameters is passed to Validation methods in Model. Since the signature of url() was
url($check, $strict = false)
the result was that $strict always had the value true, thereby requiring full URLs with protocol prefix. Seeing that the intended URL with tilde was not validating I assumed that the problem was that CakePHP still used the old method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接使用自定义验证并使用不同的名称创建 url 验证函数呢?
否则,手册说您可以使用 AppModel、Model 或 Behaviour 中的函数覆盖 Validation 类方法。
这是书中的相关链接。
http://book. cakephp.org/view/150/Custom-Validation-Rules#Adding-your-own-Validation-Methods-152
Why not just use the custom validation and make an url validation function with a different name?
Otherwise the manual says that you can override the Validation classes methods with functions in either the AppModel, Model, or Behaviors.
Here is the relevant link in the book.
http://book.cakephp.org/view/150/Custom-Validation-Rules#Adding-your-own-Validation-Methods-152