KomalJariwala
KomalJariwala

Reputation: 2017

validate textarea in cakephp

I want to validate textarea as not empty!but this code is not working and this is not showing me an validate error message from model! my code is as below,

public $validate = array(
    'address' => array(
        'rule' => array('validateAddress'),
        'message'  => 'Address is required',
    ),
);    

public function validateAddress() {
    if(empty($this->data[$this->alias]['address'])) {
        debug('hi');
        return true;
    }
    return true;
}

}

please help me for textarea validation in cakephp! Any suggestions are welcomed!

Upvotes: 1

Views: 1682

Answers (2)

noslone
noslone

Reputation: 1289

You are always returning true. So it will always validate. Try following:

public function validateAddress() {
    if(empty($this->data[$this->alias]['address'])) {
        debug('hi');
        return false;
    }
    return true;
}

Upvotes: 1

nIcO
nIcO

Reputation: 5001

Your custom validation rule always returns true. It should return false when the validation fails:

public function validateAddress() {
    if(empty($this->data[$this->alias]['address'])) {
        //debug('hi');
        return false;
    }
    return true;
}

That said, for such a simple rule, you should use the core validation rule notempty:

public $validate = array(
    'address' => array(
        'rule'    => 'notEmpty',
        'message' => 'Address is required'
    )
);

Upvotes: 3

Related Questions