$check 值是 CakePHP 验证行为中的整个模型对象

发布于 2024-12-27 00:20:50 字数 1604 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界等同你 2025-01-03 00:20:50

正如书本所述,第一个参数行为方法始终是对调用模型的引用。

As stated by the book here, the first parameter of a behavior method is always a reference to the calling model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文