所以我有一个cakePHP(2.4.6)表单,我正在尝试使用第二个密码字段创建一个典型的用户注册表单。我编写了一个自定义规则来检查密码字段的值是否匹配。
我第一次提交表单时,验证有效,但在验证失败一次后,表单拒绝再次提交。任何人都知道为什么?任何具有自定义验证规则的表单都会发生这种情况......
以下是参考模型:
class User extends AppModel {
var $primaryKey = 'username';
public $validate = array(
'username' => array(
'between' => array(
'rule' => array('between', 8, 20),
'message' => 'Usernames must be 8-20 characters long.',
),
'reg' => array(
'rule' => '/^[\w]{8,20}$/',
'message' => 'Valid characters: a-z, A-Z, 0-9, _'
),
),
'password' => array(
'between' => array(
'rule' => array('between', 8, 20),
'message' => 'Passwords must be 8-20 characters long.',
),
'reg' => array(
'rule' => '/^[\w\.\-]{8,20}$/',
'message' => 'Valid characters: a-z, A-Z, 0-9, _, -, .'
),
),
're_password' => array(
'same' => array(
'rule' => array('same', 'password'),
'message' => 'Passwords do not match!',
)
),
);
public function same($first, $second) {
$val = '';
foreach($first as $k=>$v) {
$val = $v;
}
return $this->data[$this->$second] == $val;
}
}
控制器add()
功能:
public function add() {
if ($this->request->is('post')) {
pr($this->request->data);
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('controller'=>'home'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
第一次提交工作正常: first submission
但是在我收到验证错误后,无论是否将输入值更改为没有错误,单击“提交”都不会执行任何操作 - 页面只会刷新输入字段中的任何值,如下所示: after
没有flash消息,没有验证错误消息,没有。 (对不起,没有声望,无法发布图片..)
答案 0 :(得分:1)
用帖子替换你的帖子,像
一样 public function add() {
if ($this->request->is(array('post','put'))) {
pr($this->request->data);
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('controller'=>'home'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}