dongniuxia8650 2013-08-28 14:24
浏览 29

Cakephp不验证表单中的数据

I am Trying to validate data from Form(.ctp file) in cakephp 2.3.8.

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'));?>

This is my modle code

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;
    }

but when i singup, it not validating data and i don't know where i am mistaking

  • 写回答

1条回答 默认 最新

  • duanjiushu5063 2013-08-28 15:10
    关注

    I have not seen validation rules in the view before, only in either the model or the controller. This may very well be an alternate method. Try moving validation code to the model.

    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',
                ),
            ),
        );
    

    In your view

    <?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'
    ));
    ?>
    

    By default, it will recognize the password field as 'type' => 'password'

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?