用户模型
//...
public $validate = array(
"username" => array(
"alpha-numeric" => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Username must contain only numbers and letters.'
),
"between" => array(
"rule" => array("between", 4, 25),
"message" => "Username must contain between 4 to 25 characters."
)
),
//...
用户控制器
public function index() {
debug($this->data); // displays values as expected
debug($this->User->validates()); // always false, unless I remove alphanumeric rule
}
在提交时,我的表单将始终将username
字段验证为false(触发其相应的消息),无论文本中是什么。其余字段也未经过验证。如果我一起删除username
验证规则,则其余字段仍未验证。 (表格提交时已经过验证)
我在这里缺少什么?
答案 0 :(得分:4)
在手动调用$this->User->validates()
之前,您必须调用模型的set
方法并提供表单的数据。
public function index() {
$this->User->set($this->request->data); // Only way the next method will work.
debug($this->User->validates());
}