表格验证在cakePHP 2.4.6中无法正常工作

时间:2014-04-02 17:34:02

标签: cakephp

在2.2.1版本中,我可以使用规则和自定义消息验证表单,如下所示。但不知何故,密码规则从版本2.3开始不起作用。任何帮助我可能在这里做错了吗?

型号:

class User extends AppModel {
public $validate = array(
'password' => array(
    'rule' => array ('between', 5, 10 ),
        'message' => 'Password must between 5 and 10 characters long'
    )
);
public function beforeSave($options = array()) {

    $this->data['User']['password'] = Security::hash($this->data['User']['password'], 'sha1', true);

return true;

}
}

查看:

    <?php

echo $this->Form->create();
echo $this->Form->input('firstname', array('label' => 'First name'));
echo $this->Form->input('lastname', array('label' => 'Last name'));
echo $this->Form->input('adminrole', array('type' => 'checkbox', 'label' => 'Is admin?<br /><br />'));
echo $this->Form->input('email', array('label' => 'E-mail address'));
echo $this->Form->input('password', array('label' => 'Password'));
echo $this->Form->input('picturethumb', array('type' => 'file', 'label' => 'Profile picture'));
echo $this->Form->end('Save');

?>

请记住,这个完全相同的代码在2.2.1中正确验证

控制器:

    class UsersController extends AppController {

    public function index() {
        $users = $this->User->find('all');
        $this->set('users', $users);

    }
    public function add() {
        if ($this->request->is('post')) {
            $this->User->save($this->request->data);
            $this->redirect('/users');
        }

    }
}

2 个答案:

答案 0 :(得分:0)

我有时候不和蛋糕一起工作,但我记得之前有这个问题。问题是,cakephp会创建一个密码哈希,所以当Model get密码已经很大了。我及时做的是进行另一次验证,比如password_tmp,并像字段一样使用它,并在控制器中自己创建哈希,以获得真实的字段密码。

答案 1 :(得分:0)

试试这个 -

public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        if($this->User->save($this->request->data)){
            $this->redirect('/users');
        }else{
            $this->Session->setFlash('Opps... Something is wrong');
        }   
    }
}