【问题标题】:cakePHP 2 forms in the same view not workingcakePHP 2 表单在同一视图中不起作用
【发布时间】:2013-05-11 14:47:37
【问题描述】:

我有一个 cakePHP 应用程序,我希望我的登录和注册表单在我遵循本教程的同一页面中:http://bakery.cakephp.org/articles/RabidFire/2010/06/26/multiple-forms-per-page-for-the-same-modeland 我能够使注册部分工作,但登录部分不工作我'我收到以下错误:“缺少数据库表错误:在数据源默认值中找不到模型 Tbluserlogin 的表 tblforumuserlogins。” 这是我正在使用的控制器:

    class TblusersController extends AppController {

        public function signup() {
            $this->loadModel('Tbluser');
            $this->loadModel('Tbluserlogin');

            if (!empty($this->data)) {
                if (isset($this->data['Tbluser'])) { // Check if the signup Form was submitted

                   $this->Session->setFlash("SignUp Form was submitted.","notif");

                } else if (isset($this->data['Tbluserlogin'])) { // Check if the login Form was submitted
                    $this->Session->setFlash("Login Form was submitted.","notif");
                }

            }
        }
    }
?> 

我使用的模型是:
Tbluser.php

 <?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'
                    ),
                    array(
                        'rule' => array('confirmPassword', 'cakehashedpassword'),
                        'message' => 'Passwords do not match'
                    )), 
            'email'=>array(
                array(
                    'rule'=>array('email',true),
                    'required'=>true,
                    'allowEmpty'=>false,
                    'message'=>'Invalide email adress!'
                ),
                array(
                    'rule'=>'isUnique',
                    'message'=>'Mail adress already taken!'
                )
            )
        );
    }
    ?>   

Tbluserlogin.php 模型:

<?php 
class Tblforumuserlogin extends Tblforumuser{

}
?>      

我的视图文件是“signup.ctp”

<h4>Sign up</h4>    
<div><?php echo $this->Session->flash();?></div>
<?php                   

echo $this->Form->create("Tblforumuser", array('url' => '/Tblusers/signup')); 
echo $this->Form->input('username' ,array('label'=>'Username<b style="color:red;">')); 
echo $this->Form->input('password' ,array('label'=>'Password<b style="color:red;">','type' => 'password')); 
echo $this->Form->input('email' ,array('label'=>'Email<b style="color:red;">'));
echo $this->Form->end('Register');

?> 

<h4>Log in to Ohyeahhh</h4> 
<div><?php echo $this->Session->flash(); ?></div>
<?php echo $this->Form->create("Tbluserlogin", array('url' => '/Tblusers/signup'));  ?>
<?php echo $this->Form->input('username' ,array('label'=>"Username :")); ?>
<?php echo $this->Form->end('Login'); ?>               

谢谢。

【问题讨论】:

    标签: forms cakephp model


    【解决方案1】:

    您没有遵循 CakePHP 约定,因此 CakePHP 无法自动找到要使用的正确数据库表;

    默认情况下,CakePHP 模型应该以数据库表命名;例如对于表foos(复数),模型应命名为Foo(单数)。

    如果您不遵守约定,则应通过 useTable 属性手动指定要用于模型的表;

    class Tblforumuserlogin extends Tblforumuser
    {
        public $useTable = 'tblforumusers';
    }
    

    使用相同的属性,这还允许您创建更“友好”的模型名称(也就是说,如果您无法重命名数据库表,这可能是一个更好的方法);

    class User extends AppModel
    {
        public $useTable = 'tblforumusers';
    ]
    

    替代方法

    由于这两种模型仅用于区分“哪个”表单已提交,因此这里的开销似乎很大。另一种方法是仅使用“正常”模型,但将操作指向单独的“登录”操作;

    class TblusersController extends AppController
    {
        public $uses = array(
            'Tbluser';
        );
    
        public function signup()
        {
            if ($this->request->is('post')) {
                 // handle sign-up
            }
        }
    
        public function login()
        {
            if ($this->request->is('post')) {
                 if ($this->Auth->login()) {
                     $this->redirect($this->Auth->redirectUrl);
                 }
            }
    
            // Login failed or no form submitted
            return $this->redirect(array('action' => 'signup'));
        }
    }
    

    在你的视野中;

    echo $this->Form->create("Tblforumuser"); 
    echo $this->Form->input('username' ,array('label'=>'Username<b style="color:red;">')); 
    echo $this->Form->input('password' ,array('label'=>'Password<b style="color:red;">')); 
    echo $this->Form->input('email' ,array('label'=>'Email<b style="color:red;">'));
    echo $this->Form->end('Register');
    
    
    echo $this->Form->create("Tblforumuser", array('action' => 'login')); 
    
    // etc...
    
    echo $this->Form->end('Login');
    

    还有一种方法

    只需在您的表单中添加一个隐藏字段以指示已发送的表单;

    echo $this->Form->create("Tblforumuser"); 
    echo $this->Form->hidden('formsent', array('value' => 'register'));
    // etc...
    echo $this->Form->end('Register');
    
    
    echo $this->Form->create("Tblforumuser"); 
    echo $this->Form->hidden('formsent', array('value' => 'login'));
    // etc...
    echo $this->Form->end('Login');
    

    在你的控制器内部;

    if ($this->request->is('post')) {
        if ('register' === $this->request->data['Tblforumuser']['formsent']) {
            // register
        } else {
            // login
        }
    }
    

    【讨论】:

    • 您好,感谢您的回答。确实这两种解决方案可以工作我测试了最后一个并且它可以工作但是因为我只留下了一个模块女巫是 Tblforumuser 现在我的两个表单在提交其中一个时都受到验证这不是我想要的所以我该如何制作仅在我将提交的表单上进行验证?谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多