简体   繁体   中英

cakephp validation rules not firing

Given the following Controller:

class UsersController extends AppController {
    public function findUser() {
        ...
        $this->User->findUser($this->request->query['u']);
        ...
    }
}

And the following Model:

class User extends AppModel {
    public $validate = array(
        'username' => array(
            'rule' => array('minLength', '8'),
            'message' => 'Username must be at least 8 characters'
        )
    );

    public function findUser($username) {
        return $this->find('all', array(
            'conditions' => array('username' => $username),
        ));
    }
}


If for example I type this in the url: http://example.com/users/findUser?u=a the validator does not get triggered. Why?

验证器用于插入或更新数据,而不用于查询数据。

You have to execute validates method manually (unless you're saving data):

if ($this->User->validates()) {
    // valid
} else {
    // not valid
    $errors = $this->User->validationErrors;
}

Validator is executed automatically when you insert/update data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM