CakePhp表单验证不工作


CakePhp Form validation is not working

我有这个注册表的插件,我正在创建。当我提交空白表单时,我得到的只是一个空白页面,验证不起作用。它正在调用模型,因为我可以用错误破坏应用程序。我认为它在$validate数组中,但只是看不到它。

感谢您的帮助。

代码如下:

控制器方法:

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.');
      }
  }
}
用户模式:

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

需要读取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'
                )
            )
        );

如果你想在输入中输入名字

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