0

我有一个视图,其中有 2 个表单,一个用于登录,一个用于注册,如下所示:signup.ctp //my view

    <div>
     <?php 
       echo $this->Form->create("Tbluser"); 
       echo $this->Form->hidden('formsent', array('value' => 'signup'));
       echo $this->Form->input('username' ,array('label'=>'Username')); 
       echo $this->Form->input('password' ,array('label'=>'Password','type' => 'password')); 
       echo $this->Form->input('email' ,array('label'=>'Email'));
       echo $this->Form->end('Register');
     ?> 
    </div>

<div>
 <?php 
    echo $this->Form->create("Tbluser");  ?>
    echo $this->Form->hidden('formsent', array('value' => 'login')); 
    echo $this->Form->input('username' ,array('label'=>"Username :"));
    echo $this->Form->input('password' ,array('label'=>"Password :",'type' => 'password'));
    echo $this->Form->end('Login'); 
 ?>
<div>

我用于两种形式的模型如下:

<?php
class Tbluser extends AppModel{

    public $validate = array(
        'username'=>array(
            array(
                'rule'=>'alphaNumeric',
                'allowEmpty'=>false,
                'message'=>'Invalide Username!'
            ),
            array(
                'rule' => array('minLength', '4'),
                'message' => 'Username has to be more than 3 chars'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Username already taken!'
            )
        ),
        'password' => array(
                array(
                    'rule' => 'alphaNumeric',
                    'allowEmpty'=>false,
                    'message' => 'Password must be AlphaNumeric!'
                ),
                array(
                    'rule' => array('minLength', '4'),
                    'message' => 'Username has to be more that 3 chars'
                )
            ),
        'email'=>array(
            array(
                'rule'=>array('email',true),
                'required'=>true,
                'allowEmpty'=>false,
                'message'=>'Invalide email adress!'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Mail adress already taken!'
            )
        )
    );
}
?>

我使用的控制器如下:

<?php
class TblusersController extends AppController
{
    public $uses = array(
        'Tbluser'
    );

    public function signup()
    {
      if ($this->request->is('post')) {
            if ('signup' === $this->request->data['Tbluser']['formsent']) {
                         // Registration part.
                }else if('login' === $this->request->data['Tbluser']['formsent']){
                         //Login part
                }
    }
}
?>

我的 AppController 看起来像:

<?php
class AppController extends Controller {
    public $helpers = array('Form', 'Html');
    public $components = array('Session','Cookie','Auth'=>array(
        'authenticate'=>array(
             'Form' => array(
                'userModel' => 'Tblforumuser',
                'fields' => array(
                    'username' => 'username',
                    'password' => 'password'
                )
            )
        )
    ));
}
?>

现在,如果我在注册表单中填写错误的数据并提交它,则会发生验证,但也会在登录表单字段中进行,所以我如何将验证设置为仅适用于该注册表单而不适用于两个表单?谢谢。

4

1 回答 1

0

It looks like all validation is being invoked on every read and write because you are telling your model to run all validation without restriction. A better way to handle this would be a separate model for each form.

By creating two new models userLogin and userRegister that extend Tbluser, you can set specific validation rules for each form. You could do something like :

View/Tbluser/signup.ctp

<div>
 <?php 
   echo $this->Form->create("userRegister"); 
   echo $this->Form->hidden('formsent', array('value' => 'signup'));
   echo $this->Form->input('username' ,array('label'=>'Username')); 
   echo $this->Form->input('password' ,array('label'=>'Password','type' => 'password')); 
   echo $this->Form->input('email' ,array('label'=>'Email'));
   echo $this->Form->end('Register');
 ?> 
</div>

<div>
 <?php 
    echo $this->Form->create("userLogin");  ?>
    echo $this->Form->hidden('formsent', array('value' => 'login')); 
    echo $this->Form->input('username' ,array('label'=>"Username :"));
    echo $this->Form->input('password' ,array('label'=>"Password :",'type' => 'password'));
    echo $this->Form->end('Login'); 
 ?>
<div>

Here, since we're using two separate models with each $this->Form-create(); helper, only the validation in the specified model will be run. Your models will contain only the validation that applies to the form assigned to it:

Model/userRegister.php

class userRegister extends Tbluser{
    public $validate = array(
        'username'=>array(
            array(
                'rule'=>'alphaNumeric',
                'allowEmpty'=>false,
                'message'=>'Invalide Username!'
            ),
            array(
                'rule' => array('minLength', '4'),
                'message' => 'Username has to be more than 3 chars'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Username already taken!'
            )
        ),
        'password' => array(
                array(
                    'rule' => 'alphaNumeric',
                    'allowEmpty'=>false,
                    'message' => 'Password must be AlphaNumeric!'
                ),
                array(
                    'rule' => array('minLength', '4'),
                    'message' => 'Username has to be more that 3 chars'
                )
            ),
        'email'=>array(
            array(
                'rule'=>array('email',true),
                'required'=>true,
                'allowEmpty'=>false,
                'message'=>'Invalide email adress!'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Mail adress already taken!'
            )
        )
    );
}

Model/userLogin.php

class userLogin extends Tbluser{
    public $validate = array(
        'username'=>array(
            array(
                'rule'=>'alphaNumeric',
                'allowEmpty'=>false,
                'message'=>'Invalide Username!'
            ),
            array(
                'rule' => array('minLength', '4'),
                'message' => 'Username has to be more than 3 chars'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Username already taken!'
            )
        ),
        'password' => array(
            array(
                'rule' => 'alphaNumeric',
                'allowEmpty'=>false,
                'message' => 'Password must be AlphaNumeric!'
            ),
            array(
                'rule' => array('minLength', '4'),
                'message' => 'Username has to be more that 3 chars'
            )
        )
    );
}

Then in your signup(); method, you will want to load the two new models you just created accordingly:

Controller/TblusersController.php

class TblusersController extends AppController {
    public $uses = array(
        'Tblforumuser'
    );

    public function signup() {
      $this->loadModel('userLogin');
      $this->loadModel('userRegistration'); 
      if ($this->request->is('post')) {
            if ('signup' === $this->request->data['Tblforumuser']['formsent']) {
                         // Registration part.
                }else if('login' === $this->request->data['Tblforumuser']['formsent']){
                         //Login part
                }
    }
}

Hope this helps

于 2013-05-14T04:47:05.033 回答