0

カスタム検証を行っていますが、無効にするとエラーメッセージが表示されません。問題がどこにあるか知っていますか?問題は無効化機能にあるのではないかと思います。このようなネストされた検証用に設定する方法を知っていますか?

var $validate = array(
    'receiver' => array(
        'maxMsg' => array(
            'rule' => array('maxMsgSend'),
            //'message' => ''
            ),
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'field must not be left empty'
            ))......

モデルのカスタム検証方法:

    function maxMsgSend ( $data )
    {   
        $id = User::$auth['User']['id'];

        $count_contacts = (int)$this->Contact->find( 'count', array( 'conditions' =>array( 'and' =>array(   'Contact.contact_status_id' => '2',
                                                            'Contact.user_id' => $id)))); 

        $current_credit = (int)$this->field( '3_credit_counter', array( 'id' => $id));
        $max_allowed_messages = ($count_contacts >= $current_credit)? $current_credit: $count_contacts ; 


        if ($data>$max_allowed_messages)
        {
        $this->invalidate('maxMsg', "you can send maximum of {$max_allowed_messages} text messages.");
        }
    }

更新:どのように解決されますか。関数の内臓をモデルのbeforeValidate()に移動しました。

function beforeValidate($data) {
    if (isset($this->data['User']['receiver'])) 
    {
            $id = User::$auth['User']['id'];

            $count_contacts = (int)$this->Contact->find( 'count', array( 'conditions' =>array( 'and' =>array(   'Contact.contact_status_id' => '2',
                                                                'Contact.user_id' => $id)))); 

            $current_credit = (int)$this->field( '3_credit_counter', array( 'id' => $id));
            $max_allowed_messages = ($count_contacts >= $current_credit)? $current_credit: $count_contacts ; 


            if ($data>$max_allowed_messages)
            {
                $this->invalidate('receiver', "you can send maximum of {$max_allowed_messages} text messages.");
                return false;
            }
    }
    return true;
} 
4

2 に答える 2

0

検証が失敗した場合でも、maxMsgSend関数はfalseを返す必要があると思います。

于 2010-01-07T13:38:11.020 に答える
0

Model::maxMsgSend 関数に問題があると思います。ベーカリー ( http://bakery.cakephp.org/articles/view/using-equalto-validation-to-compare-two-form-fields ) に書かれているように、カスタム検証ルールを作成します (2 つのフィールドですが、概念は同じです)、彼らは次のように書いています:

値が一致しない場合は false を返し、一致する場合は true を返します。

Model クラスのコードを確認してください。半分ほど下にあります。つまり、カスタム検証メソッド内から invalidate を呼び出す必要はありません。検証に合格した場合は true を返し、検証に合格しなかった場合は false を返すだけです。

于 2010-01-07T16:36:41.767 に答える