Answer the question
In order to leave comments, you need to log in
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');
}
}
}
<?php
namespace app\modules\auth\models;
use yii\db\ActiveRecord;
class Login extends ActiveRecord
{
public static function tableName()
{
return '{{user}}';
}
}
<?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>
$(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
download data automatically
$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'] Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question