CakePhp表单验证无效

时间:2014-06-06 19:54:34

标签: php validation cakephp

我有一个我正在创建的插件的注册表单。当我提交表格空白时,我得到的只是一个空白页面,验证无效。它正在调用模型,因为我可以错误地破坏应用程序。我认为它在$ 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']))); 
?>

1 个答案:

答案 0 :(得分:0)

您需要阅读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'
                )
            )

        );

如果您想在输入中输入名称

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