【问题标题】:cakePHP Validations : one model for to forms in the same viewcakePHP Validations : 在同一视图中表单的一种模型
【发布时间】:2013-05-14 00:07:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: validation model cakephp-2.0


    【解决方案1】:

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

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

    查看/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-&gt;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
                    }
        }
    }
    

    希望对你有帮助

    【讨论】:

    • 您好,感谢您的回答,我测试了您的解决方案,但是我收到以下错误:错误:找不到类“Tbluser”文件:/var/www/pttlk/app/Model/userRegistration.php线路:2
    • 既然你在扩展 Tbluser,它需要存在。为了保持所有自动魔法完好无损,您需要扩展 TbluserController 使用的表的模型,在您的情况下是 Tbluser。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多