Cakephp不验证表单中的数据

时间:2013-08-28 14:24:47

标签: php validation cakephp cakephp-2.3

我正在尝试验证cakephp 2.3.8中Form(.ctp文件)中的数据。

echo $this->Form->create('User').'<br />'.
                             $this->Form->input('handle', array(
                                                'rule' => 'notEmpty',
                                                 'alphaNumeric' => array(
                                                     'rule'      => 'alphaNumeric',
                                                    'required'  => true,
                                                    'message'   => 'Username must be only letters and numbers, no special characters'
                                                ),
                                                    'between' => array(
                                                        'rule'      => array('between', 4, 8),
                                                        'message'   => 'Username must be between 4 and 8 characters',
                                                    ),
                                                                'isUnique' => array(
                                                                    'rule'      => 'isUnique',
                                                                    'message'   => 'This username is already taken. Please choose a different one.'
                                                                ))).'<br />'.
                              $this->Form->input('email',array(
                                                'type'=>'text',
                                                'placeholder'=>'Enter your E-mail',
                                                'class'=>'form-control')).'<br />'.
                              $this->Form->input('password',array(
                                                'type'=>'password',
                                                'placeholder'=>'Enter your password',
                                                'class'=>'form-control'));?>

这是我的模型代码

public function beforeSave($options = array()) {
        parent::beforeSave($options = array());
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        $this->data['User']['token'] = Security::hash(mt_rand(),'md5',true);
        $this->data['User']['resetkey'] = Security::hash(mt_rand(),'md5',true);
        return true;
    }

但是当我单挑时,它没有验证数据,我不知道我在哪里错误

1 个答案:

答案 0 :(得分:2)

之前的视图中没有看到验证规则,仅在模型或控制器中。这可能是另一种方法。尝试将验证代码移动到模型中。

class User extends AppModel {
    public $validate = array(

        'handle' => array(
            'lengthCheck'=>array(
                'rule'=>array('between', 4, 8),
                'message'=>'The username must be between 4 and 8 characters.'
            ),
            'isUnique'=>array(
                'rule'=>'isUnique',
                'message'=>'This username is already taken. Please choose a different one.'
            ),
            'alphanumeric' => array(
                'rule' => array('alphanumeric'),
                'message' => 'Username must be only letters and numbers, no special characters',
            ),
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter your username',
            ),
        ),
        'password' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter your password',
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                'message' => 'Invalid email',
            ),
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter your email',
            ),
        ),
    );

在你看来

<?php
echo $this->Form->create('User');
echo $this->Form->input('handle', array(
    'placeholder'=>'Enter your username',
    'class'=>'form-control'
));
echo $this->Form->input('email', array(
    'placeholder'=>'Enter your E-mail',
    'class'=>'form-control'
));
echo $this->Form->input('password', array(
    'placeholder'=>'Enter your password',
    'class'=>'form-control'
));
?>

默认情况下,它会将密码字段识别为'type' => 'password'