K
K
Konstantin Zhikhor2017-05-31 23:42:20
Yii
Konstantin Zhikhor, 2017-05-31 23:42:20

Yii2 How to register in a modal window?

Hello. please help me with the code. I need to make a module with registration in a modal window, but I can't load the data automatically. What is the problem ? Please share your experience.
Here is the code.
Controller

<?php

namespace app\modules\auth\controllers;


use yii;
use yii\web\Controller;
use app\modules\auth\models\Login;

/**
 * Default controller for the `auth` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
    $model = new \app\models\LoginForm();
        return $this->render('index',['model'=>$model]);
    }
  
  public function actionRegistration()
  {
    $model = new Login();
    //die(print_r($model->load(Yii::$app->request->post())));
    if($model->load(Yii::$app->request->post()) && $model->save()){
      return true;
      //$this->redirect('index');
    }
    
  }
}

Model
<?php

namespace app\modules\auth\models;

use yii\db\ActiveRecord;

class Login extends ActiveRecord
{


    public static function tableName()
    {
        return '{{user}}';
    }
}

view
<?php

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\models\LoginForm */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Modal;

$this->title = 'Личный кабинет';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
  <?php 
    Modal::begin([
      'header' => '<h2>Registration</h2>',
      'toggleButton' => ['label' => 'click me'],
      
    ]);?>

    <?php $form = ActiveForm::begin([
        'id' => 'login-form',
        'layout' => 'horizontal',
        'fieldConfig' => [
            'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
            'labelOptions' => ['class' => 'col-lg-1 control-label'],
        ],
    ]); ?>

        <?= $form->field($model, 'login_user')->textInput(['autofocus' => false])->label('Логин') ?>

        <?= $form->field($model, 'password_user')->passwordInput()->label('Пароль') ?>
        <div class="form-group">
            <div class="col-lg-offset-1 col-lg-11">
                <?= Html::Button('Save', ['class' => 'btn btn-primary', 'name' => 'save','id'=>'user_reg']) ?>
            </div>
        </div>

    <?php ActiveForm::end(); ?>

    <?php Modal::end();?>
</div>
<div class="site-login">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>Пожалуйста, заполните поля:</p>

    <?php $form = ActiveForm::begin([
        'id' => 'login-form',
        'layout' => 'horizontal',
        'fieldConfig' => [
            'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
            'labelOptions' => ['class' => 'col-lg-1 control-label'],
        ],
    ]); ?>

        <?= $form->field($model, 'username')->textInput(['autofocus' => false])->label('Логин') ?>

        <?= $form->field($model, 'password')->passwordInput()->label('Пароль') ?>

        <?= $form->field($model, 'rememberMe')->checkbox([
            'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",
        ])->label('Запомнить') ?>

        <div class="form-group">
            <div class="col-lg-offset-1 col-lg-11">
                <?= Html::submitButton('Войти', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
            </div>
        </div>

    <?php ActiveForm::end(); ?>
</div>

js
$(function(){
  $("#user_reg").click(function(e){
     $.ajax({
       url:"/auth/default/registration",
       type:"POST",
       data:"login_user="+$("#loginform-login_user").val()+"&password_user="+$("#loginform-password_user").val(),
       success:function(answer){
         alert(answer);
       }
     });
  });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-06-01
@webinar

download data automatically

The term needs to be spelled out. A million options for what lies underneath.
Next:
the Login class has no validation or anything. How do you build activeForm from it later?
$model->load(Yii::$app->request->post())it will work if the POST contains an array of the form [model class name][attribute] for example ['Login']['userName']
I would not do this bike. I would take the standard authorization method for yii and just put it in a modal window.
Sending the form is not ajax, but a regular post, otherwise what's the point? The user is authorized on the server, and the client will not know about it yet. So you can click on a certain button, open a modal, do get ajax, get the form code in response, insert this form into the modal, and then just submit, and on the server receive, login and redirect to the previous page

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question