在单页cakephp上登录并注册具有相同型号名称的表单

时间:2015-01-02 09:55:02

标签: cakephp cakephp-2.0

enter image description here

我正在使用CAKEPHP 2.6 授权登录

我有一个带登录和注册页面的视图文件,当我提交我的表单时,它在登录部分和注册表单上都会出错。 如何解决这个问题。

2 个答案:

答案 0 :(得分:1)

我使用cakephp version3做了同样的事情。我希望它可以帮助你实现你的。

我登录页面的代码... login.ctp ..它包含注册表和登录表。 诀窍是确保在创建表单时使用参数url将其指向相应的方法。你会注意到我没有在登录表单上使用一个。这是因为此表单发布到名为login.ctp

的相同方法
  <!-- This is the section that contains the login form>
      <div class="navbar-header-menu">
          <ul class="nav navbar-nav">
              <li class="active"><a href="#">Login</a></li>
          </ul>
             <?php                      

                echo $this->Form->create(null, ['class' => 'form-inline navbar-form navbar-left']);

                      //Username
                      echo $this->Form->input('username', [
                        'templates' => ['inputContainer' => '<div class="form-group input text required">{{content}}</div>'],
                        'label' => false,
                        'class' => 'form-control',
                        'required' => 'true',
                        'type' => 'text',
                        'placeholder' => 'Username',
                        'id' => 'inputEmail3'
                      ]);

                      //Password
                      echo $this->Form->input('password', [
                        'templates' => ['inputContainer' => '<div class="form-group input text required">{{content}}</div>'],
                        'label' => false,
                        'class' => 'form-control',
                        'required' => 'true',
                        'type' => 'password',
                        'placeholder' => 'Password',
                        'id' => 'inputEmail3'
                      ]);
                      ?>
                        <div class = "form-group">
                      <?php
                      echo $this->Form->button(__('Login'), [
                        'name'=>'loginnow',
                        'class' => 'btn btn-primary btn-block btn-flat',
                        'type' => 'text',
                        'placeholder' => 'Password',
                        'id' => 'inputEmail3'
                      ]);
                  ?>
                      </div>
                  <?php

                echo $this->Form->end();

                ?>

      </div>
     
                    <div class="register-box-body">
            <p class="login-box-msg">Register a new membership</p>
            <?php
            echo $this->Form->create($user, ['url' => ['action' => 'add'], 'type' => 'file']);
            ?>
              <div class="form-group has-feedback">
              <?php
                //Firstname
                echo $this->Form->input('first_name', [
                  'label' => false,
                  'class' => 'form-control',
                  'required' => 'true',
                  'type' => 'text',
                  'placeholder' => 'First Name',
                  'id' => 'firstname'
                ]);
                ?>
                <span class="glyphicon glyphicon-user form-control-feedback"></span>
              </div>
              <div class="form-group has-feedback">
              <?php
                //Lastname
                echo $this->Form->input('last_name', [
                  'label' => false,
                  'class' => 'form-control',
                  'required' => 'true',
                  'type' => 'text',
                  'placeholder' => 'Last Name',
                  'id' => 'inputEmail3'
                ]);
                ?>
                <span class="glyphicon glyphicon-user form-control-feedback"></span>
              </div>
              <div class="form-group has-feedback">
              <?php
                //Email
                echo $this->Form->input('email', [
                  'label' => false,
                  'class' => 'form-control',
                  'required' => 'true',
                  'type' => 'email',
                  'placeholder' => 'Email',
                  'id' => 'inputEmail3'
                ]);
                ?>
                <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
              </div>
              <div class="form-group has-feedback">
              <?php
                //Username
                echo $this->Form->input('username', [
                  'label' => false,
                  'class' => 'form-control',
                  'required' => 'true',
                  'type' => 'text',
                  'placeholder' => 'Username',
                  'id' => 'inputEmail3'
                ]);
                ?>
                <span class="glyphicon glyphicon-user form-control-feedback"></span>
              </div>
              <div class="form-group has-feedback">
              <?php
                //Password
                echo $this->Form->input('password', [
                  'label' => false,
                  'class' => 'form-control',
                  'required' => 'true',
                  'type' => 'password',
                  'placeholder' => 'Password',
                  'id' => 'inputEmail3'
                ]);
                ?>
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
              </div>
              <div class="form-group has-feedback">
              <?php
                //Tel
                echo $this->Form->input('tel', [
                  'label' => false,
                  'class' => 'form-control',
                  'required' => 'true',
                  'type' => 'text',
                  'placeholder' => 'tel',
                  'id' => 'inputEmail3'
                ]);
                ?>
                <span class="glyphicon glyphicon-phone form-control-feedback"></span>
              </div>
              <div class="form-group has-feedback">
              <?php
                //Password
                  echo $this->Form->input('role_id', [
                      'label' => [
                          'class' => 'col-sm-2 control-label',
                          'text' => 'Role',
                          'id' => 'inputEmail3'
                      ],
                      'options' => $roles,
                      'required' => 'true',
                      'class' => 'form-group',
                      'placeholder' => 'Role',
                      'id' => 'inputEmail3'
                  ]);
                ?>

              </div>
      <?php

      echo $this->Form->input('avatar', ['type' => 'file']);
      ?>



              <div class="row">
                <div class="col-xs-8">
                  <div class="checkbox icheck">
                    <label>
                      <input type="checkbox"> I Agree To the Terms And Conditions
                    </label>
                  </div>
                </div><!-- /.col -->
                <div class="col-xs-4">
                  <?php
                  echo $this->Form->button(__('Submit'), [
                    'name'=>'registernow',
                    'label' => [
                      'class' => 'col-sm-2 control-label',
                      'id' => 'inputEmail3',
                      'text' => ''
                    ],
                    'class' => 'btn btn-primary btn-block btn-flat',
                    'type' => 'text',
                    'placeholder' => 'Password',
                    'id' => 'inputEmail3'
                  ]);

                  ?>
                </div><!-- /.col -->
              </div>
            <?php
            echo $this->Form->end();
            ?>

          </div><!-- /.register-box-body -->
        </div>

在控制器中,即UsersController.ctp

public function login()
{
    $user = $this->Users->newEntity();
    $roles = $this->Users->Roles->find('list', ['limit' => 200]);
    $this->set(compact('user', 'roles'));
    $this->set('_serialize', ['user']);

    $this->layout = 'AdminLTE.logintemplate';
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}

这对我有用。我希望它可以帮助你。 我还在https://gist.github.com/Ugfighter/204f15496eae95fbfba1

上传了示例代码

答案 1 :(得分:0)

为每个表单尝试不同的名称,如下所示

echo $this->Form->create('User',array('name'=>'loginForm'));

对于下一个表单,请使用不同的名称:

echo $this->Form->create('User',array('name'=>'registerForm'));

希望它有所帮助。