简体   繁体   English

CakePhp表单验证不起作用

[英]CakePhp Form validation is not working

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. 我认为它在$ validate数组中,但看不到它。

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

you need read cakephp validation 你需要阅读cakephp验证

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM