3

私は私が拡張するベースモデルを持っています。その中で、2つの検証フィルターを定義しました。1つはレコードが一意であるかどうかをチェックし、もう1つはレコードが存在するかどうかをチェックします。これらは、一方の戻り値が他方の逆になることを除いて、まったく同じように機能します。

したがって、同じコードを2回記述して、異なる値を返すだけでは正しくありません。あるカスタムバリデーターを別のカスタムバリデーターから呼び出す方法を知りたいのですが。

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);
});
?>

しかし、明らかに、それはそのように行うことはできません。検証関数を無名関数として定義し、それを変数に割り当てて、クロージャーの代わりにそれを渡す必要がありますか?uniqueまたは、内部から呼び出す方法はありますexistsか?

4

2 に答える 2

2

匿名関数メソッドは機能します。そして、その変数を「exists」バリデーター用に定義した別の無名関数で使用できます。これを基本モデルクラスに組み込む別のアイデアは次のとおりです。

<?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;
    }

}

?>
于 2012-04-10T20:52:57.603 に答える
1

必要な機能を含む別のメソッドを作成し、検証フィルターから呼び出すことになりました。基本モデルをトリミングして、関連するデータのみを保持しました。同様の問題を抱えている人に役立つことを願っています。

<?php
namespace app\extensions\data;
class Model extends \lithium\data\Model {

    public static function __init() {
        parent::__init();

        Validator::add('unique', function($value, $rule, $options) {
            $model  = $options['model'];
            return ($model::exists($value, $rule, $options, $model)) ? false : true;
        });

        Validator::add('exists', function($value, $rule, $options) {
            $model  = $options['model'];
            return ($model::exists($value, $rule, $options, $model)) ? true : false;
        });
    }


    public static function exists($value, $rule, $options, $model) {
        $field   = $options['field'];
        $primary = $model::meta('key');

        if (isset($options['conditions']) && !empty($options['conditions'])) {
            //go here only of `conditions` are given
            foreach ($options['conditions'] as $field => $check) {
                if (!is_numeric($field)) {
                    if (is_array($check)) {
                        /**
                         *   'conditions' => array(
                         *       'Users.age' => array('>' => 18) //condition with custom operator
                         *   )
                         */
                        $conditions[$field] = $check;
                    }
                } else {
                    /**
                     * Regular lithium conditions array:
                     *   'conditions' => array(
                     *       'Users.email' //no key ($field) defined
                     *   )
                     */
                    $conditions[$check] = $value;
                }
            }
        } else {
            //since `conditions` is not set, we assume
            $modelName = $model::meta('name');
            $conditions["$modelName.$field"] = $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]);
        }

        return $model::count($conditions);
    }
}
?>
于 2012-04-10T11:16:13.787 に答える