简体   繁体   English

cakephp验证规则未触发

[英]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. 例如,如果我在URL中键入以下内容: http://example.com/users/findUser?u=a : http://example.com/users/findUser?u=a u=a验证器不会被触发。 Why? 为什么?

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

You have to execute validates method manually (unless you're saving data): 您必须手动执行validates方法(除非您要保存数据):

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

Validator is executed automatically when you insert/update data. 插入/更新数据时,将自动执行验证程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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