M
M
Maxim2016-06-21 09:41:13
Yii
Maxim, 2016-06-21 09:41:13

I get an error when submitting the registration form?

----------
Signup.php model code

<?php
class Signup extends Model
{
    public $email;
    public $password;

    public function rules()
    {
        return[
           [['email', 'password'], 'required'],
            ['email', 'email'],
            ['email', 'unique', 'targetClass' => 'app\Models\User'],
            ['password', 'string', 'min' => 2, 'max' => 10]
        ];
    }

    public function signup()
    {
        $user = new User();
        $user->email = $this->email;
        $user->password = $this->password;
        return $user->save();
    }
}

Controller code
<?php
    public function actionSignup()
    {
        $model = new Signup();

        if (isset($_POST['Signup']))
        {
            $model->attributes = Yii::$app->request->post('Signup');

            if ($model->validate() && $model->signup())
            {
                return $this->goHome();
            }
        }

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

and view
<br><br>
<?php<br><br>
$form = ActiveForm::begin(['class' => 'form-horizontal']);<br><br>
$form->field($model, 'email')->textInput(['autofocus'=>true]); <br><br>
$form->field($model, 'password')->passwordInput(); <br><br>Register<br><br>
ActiveForm::end();<br><br>

And here is the error itself:
c4c654decafd40b6a93110e90f5cd9df.png

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2016-06-21
@KidsBout

Swears at this line
and either you don't have app\Models\User or it has wrong namespace

V
VitGun, 2016-06-21
@VitGun

Do you have the User class somewhere declared?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question