Answer the question
In order to leave comments, you need to log in
Yii2 - Where is the relationship between forms in views and specific methods indicated?
Hello.
Example:
signupForm.php in Models:
class SignupForm extends Model {
public $username;
public $email;
public $password;
public $password_repeat;
public $agree_rules = true;
public function rules()
{
return [
['username', 'trim'],
...
];
}
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
return $user->save() ? $user : null;
}
}
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
<?php
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
<?= $form->field($model, 'email')->textInput(['autofocus' => false, 'class' => 'auth-input', 'placeholder' => ' EMAIL'])->label(false) ?>
<?= $form->field($model, 'password')->passwordInput(['class' => 'auth-input', 'placeholder' => ' Пароль'])->label(false) ?>
<?= $form->field($model, 'password_repeat')->passwordInput(['class' => 'auth-input', 'placeholder' => ' Повторите пароль'])->label(false) ?>
<?= $form->field($model, 'agree_rules')->checkbox(['template' => '{input} {label}'])->label('Я принимаю условия <a href="#">пользовательского соглашения</a>') ?>
<?= Html::submitButton('Регистрация', ['class' => 'auth-button', 'name' => 'signup-button']) ?>
<?php ActiveForm::end(); ?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question