如何在cakephp中验证两个表单

如何在cakephp中验证两个表单,php,cakephp,Php,Cakephp,我是cakephp的新手。 鉴于我有两个表单(登录和注册),两个表单都有电子邮件id,那么如何在cake php中验证这两个表单的值是否相同请提供帮助,如果您有示例,请发送该类型的信息。首先在模型中定义验证函数 public $validate = array( 'username' => array( 'nonEmpty' => array('rule' => array('notEmpty'), 'message' =>

我是cakephp的新手。
鉴于我有两个表单(登录和注册),两个表单都有电子邮件id,那么如何在cake php中验证这两个表单的值是否相同请提供帮助,如果您有示例,请发送该类型的信息。

首先在模型中定义验证函数

public $validate = array(
    'username' => array(
        'nonEmpty' => array('rule' => array('notEmpty'), 
            'message' => 'A username is required',
            'allowEmpty' => false),),
     'password' => array(
         'required' => array(
             'rule' => array('notEmpty'),
             'message' => 'A password is required' ),
         'min_length' => array(
              'rule' => array('minLength', '6'), 
              'message' => 'Password must have a mimimum of 6 characters')),

    'password_confirm' => array(
         'required' => array(
              'rule' => array('notEmpty'),
               'message' => 'Please confirm your password' ), ),

    'email' => array(
          'required' => array(
              'rule' => array('email', true),   
              'message' => 'Please provide a valid email address.' ),
               ),              
        );
然后在控制器中使用

    function register() {
    if( isset($this->data) )
    {
        $this->User->create();
        if($this->User->save($this->data))
        {
            $this->Session->setFlash( 'Thank you for registering!' );
        }else
        {    
            $this->Session->setFlash('An error occurred, try again!');
        }
    }
}
function login(){
    if ($this->request->is('post')) {
        $this->User->set($this->request->data);
        if ($this->User->validates()) {
             echo "This is valid!";
        } else {
             echo "This is invalid";
             $errors = $this->User->validationErrors;
        }
    }
}
注册ctp

   <div class="users form">
      <?php echo $this->Form->create('User');?>
         <fieldset> 
             <legend><?php echo __('Add User'); ?></legend>
               <?php    echo $this->Form->input('username');
                        echo $this->Form->input('email');  
                        echo $this->Form->input('password');
                     echo $this->Form->input('password_confirm', array('label' => 'Confirm Password *', 'maxLength' => 255, 'title' => 'Confirm password', 'type'=>'password'));
            echo $this->Form->submit('Add User', array('class' => 'form-submit',  'title' => 'Click here to add the user') ); ?>
         </fieldset>
     <?php echo $this->Form->end(); ?>
  </div>

login.ctp

   <div class="users form">
       <?php echo $this->Form->create('User');?>
         <fieldset> 
         <legend><?php echo __('Add User'); ?></legend>
              <?php     echo $this->Form->input('email');  
            echo $this->Form->input('password');
            echo $this->Form->submit('Login'); ?>
        </fieldset>
      <?php echo $this->Form->end(); ?>
    </div>


请参考:但是@sudhir此验证针对单个表单,而不是针对单个视图中的两个表单。我有两个表单登录和注册,两个表单中都有相同的字段,如电子邮件、用户name@PrakashMadhak在这两种形式中,都使用用户模型验证。你试过上面的代码吗。如果,那么在我的模块相同的代码中,但在登录表单中,有效用户名中的显示错误在登录表单中为“not empty in”,但在我的登录表单中,没有任何类似用户名的字段。但注册表页正在工作perfact@PrakashMadhak我已经编辑并检查它是否工作正常。现在你检查一下