2013-05-14 18 views
0

我有一个视图至极具有2点形成一个用于登录,一个用于登记如下: signup.ctp //我的视图CakePHP的验证:用于向在同一视图的形式之一模型

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

现在如果我填写错误的数据到注册表单并提交它发生的验证,而且在登录表单字段,以便我怎样才能设置验证只适用于注册表单而不是两种表单?谢谢。

回答

0

看起来所有验证都在每次读取和写入时被调用,因为您告诉您的模型无限制地运行所有验证。处理这个问题的更好方法是为每个表单单独创建一个模型。

通过创建两个新模型userLoginuserRegister扩展Tbluser,您可以为每个表单设置特定的验证规则。你可以这样做:

查看/ 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> 

在这里,因为我们使用两个单独的模型,每个$this->Form-create();帮手,只能在指定的模型验证会跑。您的模型将只包含适用于分配给它的形式验证:

型号/ 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!' 
      ) 
     ) 
    ); 
} 

型号/ 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' 
      ) 
     ) 
    ); 
} 

然后在您的signup();方法,您将需要加载您刚刚创建的两个新模型:

控制器/ 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 
       } 
    } 
} 

希望这有助于

+0

您好,感谢您的回答,我测试您的解决方案,但是我得到以下错误:错误:类“Tbluser”未找到 文件:/var/www/pttlk/app/Model/userRegistration.php Line:2 – OussamaLord 2013-05-14 10:37:04

+0

由于您正在扩展Tbluser,因此它需要存在。为了保持所有的automagic完好无损,您需要扩展由TbluserController使用的表格的模型。 – Dan 2013-05-14 18:03:42