0
私はCakePHP2.8を使用しています。モデル間で複数のカスタム検証メソッドを共有したいと思います。CakePHP 2がカスタム検証メソッドを共有する、userDefined
モデル内で動作することが知られているカスタム検証メソッドを使用して、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();
私は
Job
からデータを検証しますそのモデルで
JobCustomA
ためのすべての検証を持ってしようとしています。
私はJob
に検証を追加したい私のJobCustomA
モデルでは、私はそうのようにやっている:
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;バリデーターでのuserDefined
ロールの使用は機能しません。複数のモデル間で多くのカスタム検証メソッドを再利用する必要があります。
おかげ
ルールが機能しないことは確かですか?あなたは 'beforeValidate()'メソッドが全く呼び出されていないことをテストしましたか?特に 'Job'モデルデータに_before_ validationが適用されていますか? – ndm
はい、いくつかのログを記録しました。ログはbeforeValidateで動作します。ヘルパーのルールは何も書き込まなかったため、ルールに当たっていませんでした。 – alistaircol
あなたはこのhttps://book.cakephp.org/2.0/en/models/data-validation.html#adding-your-own-validation-methodsを見ましたか?カスタム検証方法を正しく定義する方法を説明しています –