skip to Main Content

I’m new to yii2, I’m trying to send a request to the database, but it comes out that such columns cannot be null, although when checking through @var_dump I can see the sending data, what is the matter and how to fix it

—> Controller

 public function actionSignup()
{
    $model = new Signup();

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


        if ($model->validate()) {
            $model->signup();
            # code...
        }
    }

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

—> View page

<?php
use yiiwidgetsActiveForm; 
?>
<?php
$form = ActiveForm::begin(['class'=>'form-horizontal']);
?>

<?= $form->field($model,'email')->textInput(['autofocus'=>true]) ?>

<?= $form->field($model,'password')->passwordInput() ?>

<div>
<button type="submit" class="btn primary-btn">Submit</button>
</div>
<?php
ActiveForm::end();
?>

—> Model

class Signup extends Model
{
public $email;
public $password;

public function reles()
{
    return [

        [['email', 'password'], 'required'],
        ['email', 'email'],
        ['email', 'unique', 'targetClass'=>'appmodelsUser'],
        ['password', 'string', 'min'=>2,'max'=>10]
    ];
}

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

—> phpmyadmin database

enter image description here

namespace appmodels;

use yiidbActiveRecord;


class User extends ActiveRecord
{

}

–> var_dump enter image description here

–> sql enter image description hereenter image description here
enter image description here

2

Answers


  1. Try this:

    **
         * Signs user up.
         *
         * @return mixed
         */
        public function actionSignup()
        {
    
            $model = new SignupForm()
            //Sign up if it is post request
            if ($model->load(Yii::$app->request->post()) && $model->signup()) { //Here YOU LOAD your POST data in to the Lodal using load() method.
                Yii::$app->session->setFlash('success', 'Thank you for registration. Please check your email for further instructions.', false);
                return $this->goHome();
            } else {
                Yii::$app->session->setFlash('success', 'Sorry, you can not register right now.', false);
                return $this->redirect('/login');
            }
        }
    
    Login or Signup to reply.
  2. In Model

    First of all, the method for attribute rules is called "rules" I don’t know if it’s a typo from you in the question or this is the problem; If there are no rules for user input data the returned values will be NULL, I have faced this issue before, I didn’t include a rules() method (or in your case it’s name is not correct) and the model returned null for attribute values. Your code have to be:

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

    Technical note: normally, or based upon my understanding a password should have a minimum of 8 not 2 characters, if the password entered is only 2 characters it can be brute-forced in a second.

    In Controller

    As in @Serghei Leonenco’s Answer, Yii2 already provides some functions to use to handle post requests and so, so please stick to Yii2 functions it can enhance your app security

    So in the actionSignup() method the code ought to be

    public function actionSignup()
    {
        $model = new Signup();
    
        // Loads the post request directly onto the model and validates it against the model rules() method
        if($model->load(Yii::$app->request->post()) && $model->validate){
                $model->signup();      
                // Model validated and saved successfully, IT OUGHT TO BE SAVED IN THE DATABASE AT THIS POINT
                // Here you can set a flash or redirect the user to another page  
        }
    
        return $this->render('signup', ['model'=>$model]);
    }
    

    In View

    At last, in your view code it’s better to use Yii2 internal functions for Html buttons

    <?= Html::submitButton('Signup', ['class' => 'btn btn-primary']) ?>
    

    Of course don’t forget to use yiihelpersHtml

    use yiihelpersHtml;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search