views:

386

answers:

2

hi, im doing a custom validation but it does not display error message when invalidated. do you know where is the problem? I think the problem might be in the invalidate function. do you know how to set it up for the nested validation like this one?

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

custom validation method in the model:

    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.");
        }
    }

UPDATE: how is solved it. i moved the the guts of the function to beforeValidate() in the model.

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;
} 
A: 

I think your maxMsgSend function still needs to return false if validation fails.

ianmjones
actually it comes up with a error message that says maxMsg, even after i remove maxMsg from the invalidate function.it seems that it takes the name from the nested array.
ondrobaco
A: 

I think the problem is in your Model::maxMsgSend function. As written in the bakery, (http://bakery.cakephp.org/articles/view/using-equalto-validation-to-compare-two-form-fields), to build a custom validation rule (they want to compare two fields, but the concepts are the same), they write:

I return a false if the values don't match, and a true if they do.

Check out their code for the Model class, about half way down. In short, you don't need to call invalidate from within the custom validation method; you simply return true if it passes validation, and false if it doesn't pass validation.

Travis Leleu
thats true, the main reason i used invalidate is to display a custom error message to the user. if i just return false.. the error message would defoult to a static message
ondrobaco
Can you not set the error message with the message index in your Model::validation attribute?
Travis Leleu
I can but it would be a static message which doesn't insert the value of max_allowed_messages
ondrobaco