繁体   English   中英

CakePHP 2共享自定义验证方法,用户定义

[英]CakePHP 2 sharing custom validation methods, userDefined

我正在使用CakePHP2.8,并且我想在模型之间共享多种自定义验证方法。

我使用已知可在模型中使用的自定义验证方法创建了一个帮助器CustomValidators.php类。 这里的逻辑不是问题,这里仅用于说明。

<?php
App::uses('CakeLog', 'Utility');

class CustomValidators {

  public function checkDateNotFuturePast($checks, $params)
  {
    $params += array(
      'type' => 'past', //Date cannot be in the past
      'current_date' => 'now', //Todays date
      'include_current_date' => false //Allow current date to pass validation
    );
    CakeLog::write('error', print_r(json_encode([
      'checks' => $checks,
      'params' => $params,
    ]), true));

    $date = array_values($checks)[0];

    try {
      $timezone = new DateTimeZone("UTC");
      $input_date = new DateTime($date, $timezone);
      $current_date = new DateTime($params['current_date'], $timezone);
    } catch(Exception $e) {
      return false;
    }

    switch ($params['type']) {
      case 'future':
        if($params['include_current_date']){
          if($input_date->format('dmY') != $current_date->format('dmY')&&$input_date->format('U') > $current_date->format('U')) return false;
        }else{
          if($input_date->format('U') > $current_date->format('U')) return false;
        }
        break;
      case 'past':
        if($params['include_current_date']){
          if($input_date->format('dmY') != $current_date->format('dmY')&&$input_date->format('U') <= $current_date->format('U')) return false;
        }else{
          if($input_date->format('U') < $current_date->format('U')) return false;
        }
        break;
    }

    return true;
  }

  public function checkNotOlderThan($check, $params)
  {
    CakeLog::write('error', 'CustomValidators::checkNotOlderThan');
    $params += [
      'current_date' => date('Y-m-d'),
    ];
     CakeLog::write('error', print_r(json_encode([
      'checks' => $checks,
      'params' => $params,
    ]), true));

    if (!isset($params['range'])) {
      return false;
    }

    $date = array_values($check)[0];

    try {
      $current_date = new DateTime($params['current_date']);
      $current_date->modify('-' . $params['range']);
      $input_date = new DateTime($date);
    } catch(Exception $e) {
      return false;
    }

    if ($input_date >= $current_date) {
      return true;
    }

    return false;
  }

}

我将此文件包含在模型JobCustomA ,并在beforeValidate对其进行实例化。

  public function beforeValidate($options = [])
  {
    $CustomValidators = new CustomValidators();

我试图在其模型中对JobCustomA进行所有验证,以验证Job数据。

在我的JobCustomA模型中,我想在Job上添加验证,我这样做是:

public function beforeValidate($options = [])
{
  $CustomValidators = new CustomValidators();

  $this->Job->validator()->add('deposit_paid', [
    'not_future' => [
      'rule' => [
        'userDefined', $CustomValidators, 'checkDateNotFuturePast', [
          'type' => 'future',
        ]
      ],
      'message' => 'Deposit date can\'t be in the future',
    ],
    'nottooold' => [
      'rule' => [
        'userDefined', $CustomValidators, 'checkNotOlderThan', [
          'current_date' => date('Y-m-d'),
          'range' => '120 days',
        ],
      ],
      'message' => 'Deposit date can\'t have been paid more than 120 days ago',
    ],
  ]);

  // ...
}

但是,似乎并没有采用这些自定义验证方法,我不确定如何解决此问题。 我需要能够在多个类之间重用自定义验证方法,而不必在每个模型中重复它们。

TL; DR:在验证器添加中使用userDefined角色不起作用,需要在多个模型之间重用许多自定义验证方法。

谢谢

您可以在此处选择几个选项:

  • 在AppModel中定义自定义验证规则,这样您就可以在任何模型中使用它们而无需复制
  • 创建一个抽象模型,例如Job,然后扩展该模型以创建CustomJobA,CustomJobB等,并在那里定义自定义验证。 所有扩展Job模型的模型都可以使用您的验证规则
  • 创建一个行为,并在其中定义验证规则,然后在任何模型上使用此行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM