E
E
Edward2017-10-07 23:09:17
Yii
Edward, 2017-10-07 23:09:17

Is it necessary to create models for forms in yii2?

This question has been tormenting for a long time, was it worth creating, for example, a Singup model (like this one ), if you can use the main User model:

public function actionSignup()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new User();        
        if ($model->load(Yii::$app->request->post())) {
            $model->setPassword($model->password);
            if ($model->save()) {
                if (Yii::$app->getUser()->login($model)) {
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }

Instead of:
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,
        ]);
    }

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Elena Stepanova, 2017-10-08
@edvardpotter

- Forms are more portable and reusable
- Forms may not be tied to model attributes and better express the subject area
- Forms are easier to test
- Using forms is easier to develop in a team
- Validation in models violates SOLID principles (AR violates them in principle, but at least this part can be leveled)
- For elementary CRUD with minimal business logic, you can do without, but if a lot of logic is tied to the model and the scripts grow, the model will quickly turn into a divine object
- Sculpting all validations into one form-model is no better than sculpting them in Ap-model -> for each case a separate model-shape
--------------------
Summary:Use validation on scripted models until you understand what forms are for. Use for the sake of use, without awareness - is still meaningless and merciless

A
Alexander, 2017-10-08
@Sassoft

Models are just thin AR classes with no validation attributes.
Forms are input and validation.
Use the SRP principle, forms should validate data, the AR entity should store just fields.

I
Impeeeery, 2017-10-07
@Impeeeery

not for "specific purposes", but for forms. yes, it is necessary.
I don’t remember what exactly was pathetic when there was no form model during registration, but something was definitely pathetic.
but if everything is in order for you, then for now limit yourself to User, then transfer it if anything.

B
Boris Korobkov, 2017-10-07
@BorisKorobkov

was it worth creating, for example, the Singup model

Better User with scenario
$model = new User;
$model->scenario = User::SCENARIO_LOGIN;

$model = new User;
$model->scenario = User::SCENARIO_REGISTER;

www.yiiframework.com/doc-2.0/guide-structure-model...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question