dpus81500574 2014-06-06 19:54
浏览 43

CakePhp表单验证无效

I have this registration form for a plugin I am creating. When I submit the form blank, all I get is a blank page and the validation is not working. It is calling the model, since I can break the app with errors. I think it is in the $validate array, but just can't see it.

Any help is greatly appreciated.

Here is the code:

Controller method:

public function add() {
if ($this->request->is('post')) {
$this->User->create();
      if($this->User->save($this->request->data)){
         $this->set('title', 'Portal Home');
        $this->render('Parents.Portal/index');
      }else{
         $this->Session->setFlash('The user could not be saved. Please, try again.');
      }
  }
}

User Model:

   <?php
   App::uses('AppModel', 'Model');

   class User extends AppModel {
    public $name = 'User';
    public $useTable = 'parents';

    public $validate = array(
        'email'=>array(
            'valid email'=>array(
                'rule'=>array('email'),
                'message' => 'Please supply a valid email address.'
                ),
            'Not Empty'=>array(
                'rule'=>array('notEmpty'),
                'message'=>'Please enter your email address.'
                ),
            'That email address has already been taken'=>array(
                'rule'=>array('isUnique'),
                'message'=>'That email address has already been taken.'
                )
            ),
        'first_name'=>array(
            'Please enter your first name.'=>array(
                'rule'=>array('notEmpty'),
                'message'=>'Please enter your first name.'
                )
            ),
        'last_name'=>array(
            'Please enter your last name.'=>array(
                'rule'=>array('notEmpty'),
                'message'=>'Please enter your last name.'
                )
            ),
        'phone'=>array(
            'Please enter your phone number.'=>array(
                'rule'=>array('notEmpty'),
                'message'=>'Please enter your phone number.'
                )
            ),
        'password'=>array(
            'Not empty'=>array(
                'rule'=>array('notEmpty'),
                'message'=>'Please enter your password'
                ),
            'Match passwords'=>array(
                'rule'=>'matchPasswords',
                'message'=>'Your passwords do not match'
                )
            ),
        'password_confirmation'=>array(
            'Not empty'=>array(
                'rule'=>array('notEmpty'),
                'message'=>'Please confirm your password'
                )
            )

        );

      public function matchPasswords($data) {
    if ($data['password'] == $this->data['User']['password_confirmation']) {
        return true;
    }
    $this->invalidate('password_confirmation', 'Your passwords do not match');
    return false;
      }

      public function beforeSave() {
    if (isset($this->data['User']['password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    return true;
      }
    }
    ?>

Lastly, the view:

          <?php
        echo $this->Form->create('User', array(
                              'url' => array('controller' => 'register', 'action' => 'add'),
                                'type' => 'post', 'class' => 'form-horizontal', 'role' => 'form', 'novalidate'=>true)
                        );
         echo $this->Form->input('email', array('class' => 'form-control'));
         echo $this->Form->input('first_name', array('class' => 'form-control'));
         echo $this->Form->input('last_name', array('class' => 'form-control'));
         echo $this->Form->input('phone', array('class' => 'form-control'));
         echo $this->Form->input('password', array('class' => 'form-control'));
         echo $this->Form->input('password_confirmation', array('type'=>'password', 'class' => 'form-control'));
          echo $this->Form->end(array('label' => 'Register',  'class' =>"btn btn-lg btn-block ".  $this->Buttons->color($account['Config']['theme']))); 
?>
  • 写回答

1条回答 默认 最新

  • dtla92562 2014-06-06 20:38
    关注

    you need read cakephp validation

      public $validate = array(
            'email'=>array(
                'required'=>array(
                    'rule'=>array('email'),
                    'message' => 'Please supply a valid email address.'
                    ),
                'nonEmpty'=>array(
                    'rule'=>array('notEmpty'),
                    'message'=>'Please enter your email address.'
                    ),
                'unique'=>array(
                    'rule'=>array('isUnique'),
                    'message'=>'That email address has already been taken.'
                    )
                ),
            'first_name'=>array(
                'nonempty'=>array(
                    'rule'=>array('notEmpty'),
                    'message'=>'Please enter your first name.'
                    )
                ),
            'last_name'=>array(
                'nonEmpty'=>array(
                    'rule'=>array('notEmpty'),
                    'message'=>'Please enter your last name.'
                    )
                ),
            'phone'=>array(
                'nonEmpty'=>array(
                    'rule'=>array('notEmpty'),
                    'message'=>'Please enter your phone number.'
                    )
                ),
            'password'=>array(
                'nonEmpty'=>array(
                    'rule'=>array('notEmpty'),
                    'message'=>'Please enter your password'
                    ),
                'required'=>array(
                    'rule'=>'matchPasswords',
                    'message'=>'Your passwords do not match'
                    )
                ),
            'password_confirmation'=>array(
                'nonEmpty'=>array(
                    'rule'=>array('notEmpty'),
                    'message'=>'Please confirm your password'
                    )
                )
    
            );
    

    if you want put name in your inputs

     echo $this->Form->input('email', array('label' => 'Please put your email','class' => 'form-control'));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数