gpt4 book ai didi

php - Lithium:如何从另一个内部调用一个验证过滤器

转载 作者:搜寻专家 更新时间:2023-10-31 20:49:07 25 4
gpt4 key购买 nike

我有一个扩展的基础模型。在其中,我定义了两个验证过滤器。一个检查记录是否唯一,另一个检查记录是否存在。它们的工作方式完全相同,只是一个返回值与另一个相反。

因此,将相同的代码编写两次只返回不同的值听起来是不对的。我想知道如何从另一个调用一个自定义验证器。

这是我的 unique 验证器的代码:

<?php
Validator::add('unique', function($value, $rule, $options) {
$model = $options['model'];
$primary = $model::meta('key');

foreach ($options['conditions'] as $field => $check) {
if (!is_numeric($field)) {
if (is_array($check)) {
/**
* array(
* 'exists',
* 'message' => 'You are too old.',
* 'conditions' => array(
*
* 'Users.age' => array('>' => '18')
* )
* )
*/
$conditions[$field] = $check;
}
} else {
/**
* Regular lithium conditions array:
* array(
* 'exists',
* 'message' => 'This email already exists.',
* 'conditions' => array(
* 'Users.email' //no key ($field) defined
* )
* )
*/
$conditions[$check] = $value;
}
}

/**
* Checking to see if the entity exists.
* If it exists, record exists.
* If record exists, we make sure the record is not checked
* against itself by matching with the primary key.
*/
if (isset($options['values'][$primary])) {
//primary key value exists so it's probably an update
$conditions[$primary] = array('!=' => $options['values'][$primary]);
}

$exists = $model::count($conditions);
return ($exists) ? false : true;
});
?>

exists 应该像这样工作:

<?php
Validator::add('exists', function($value, $rule, $options) {
$model = $options['model'];
return !$model::unique($value, $rule, $options);
});
?>

但显然,不能那样做。我是否必须将验证函数定义为匿名函数,将其分配给一个变量并将其传递而不是闭包?或者有什么方法可以从 exists 中调用 unique

最佳答案

匿名函数方法可以工作。然后您可以在为“存在”验证器定义的另一个匿名函数中使用该变量。这是将其合并到您的基本模型类中的另一个想法:

<?php

namespace app\data\Model;

use lithium\util\Validator;

class Model extends \lithium\data\Model {

public static function __init() {
static::_isBase(__CLASS__, true);
Validator::add('unique', function($value, $rule, $options) {
$model = $options['model'];
return $model::unique(compact('value') + $options);
});
Validator::add('exists', function($value, $rule, $options) {
$model = $options['model'];
return !$model::unique(compact('value') + $options);
});
parent::__init();
}

// ... code ...

public static function unique($options) {
$primary = static::meta('key');

foreach ($options['conditions'] as $field => $check) {
if (!is_numeric($field)) {
if (is_array($check)) {
/**
* array(
* 'exists',
* 'message' => 'You are too old.',
* 'conditions' => array(
*
* 'Users.age' => array('>' => '18')
* )
* )
*/
$conditions[$field] = $check;
}
} else {
/**
* Regular lithium conditions array:
* array(
* 'exists',
* 'message' => 'This email already exists.',
* 'conditions' => array(
* 'Users.email' //no key ($field) defined
* )
* )
*/
$conditions[$check] = $options['value'];
}
}

/**
* Checking to see if the entity exists.
* If it exists, record exists.
* If record exists, we make sure the record is not checked
* against itself by matching with the primary key.
*/
if (isset($options['values'][$primary])) {
//primary key value exists so it's probably an update
$conditions[$primary] = array('!=' => $options['values'][$primary]);
}

$exists = $model::count($conditions);
return ($exists) ? false : true;
}

}

?>

关于php - Lithium:如何从另一个内部调用一个验证过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10062577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com