1

我正在尝试在 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;
    }

但是当我 singup 时,它没有验证数据,我不知道我在哪里弄错了

4

1 回答 1

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'

于 2013-08-28T15:10:27.227 回答