T
T
tiqq2018-09-15 07:59:26
Yii
tiqq, 2018-09-15 07:59:26

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;
    }
}

actionSugnup in the SiteController:
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,
        ]);
    }

signup.php file in views/site:
<?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(); ?>

In the action, an instance of the model class is created and passed to the view, it seems to be clear here. If I understand correctly, then by clicking on the "Register" button, the signup () method is called for an instance of the model class passed from the action to the view. Question: where is this connection indicated that this method should be called when the form button is clicked? Is it something to do with the method name being the same as the class name before the "Form" suffix or what? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question