K
K
Kirill Kaliev2019-11-18 11:43:59
Yii
Kirill Kaliev, 2019-11-18 11:43:59

Data is not filled in the database from the Yii2 form?

Dear gentlemen, programmers
Who is not difficult to help
I need to make sure that the data from the form is filled in the database.
Here is the model

class Userts extends ActiveRecord
{
    public $id;
    public $name;
    public $password;
    public $password_repeat;
    public $email;
    public $wallet;
    public $amount_of_tikets;
    public $is_admin;


    public static function tableName()
    {
        return'{{%user}}';
    }

    public function attributeLabels()
    {
        return[
            'name' => 'User',
            'password' => 'Password',
            'password_repeat' => 'Password repeat',
            'email' => 'Email',
            'wallet' => 'Wallet',
            'amount_of_tickets' => 'Amount of tickets',
            'is_admin' => 'Is Admin'
        ];
    }

    public function rules()
    {
        return[
            [['name','password','email'],'required'],
            ['password','compare','compareAttribute' => 'password_repeat'],
            ['email','email']
        ];
    }
}

Here is the controller
class UserController extends Controller
{
    public function actionReg()
    {
        $model = new Userts;
        if (\Yii::$app->getRequest()->isPost)
        {
            if($model->load(\Yii::$app->request->post()) && $model->validate())
            {
                $model->save(false);
            }
        }
        return $this->render('reg',compact('model'));
    }
}

Here is the form
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

?>

<?php $form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'action' => ['user/reg'],
    'method' => 'post'
]) ?>
    <?= $form->field($model, 'name')->textInput() ?>
    <?= $form->field($model, 'email')->input('email') ?>
    <?= $form->field($model, 'password')->passwordInput()?>
    <?= $form->field($model, 'password_repeat')->passwordInput()?>
    <?= Html::submitButton('Send', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end() ?>

The whole problem is that nothing happens when data is entered into the inputs. Is the model transmitted incorrectly, or is it when filling something?
If it's not difficult, then at least throw off some source where you can learn what trouble

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Anton R., 2019-11-18
@anton_reut

Recently I encountered the same problem in yii, everything turned out to be simple - to write to the database, you need to validate ALL fields for some reason, otherwise it is not written. In general, add all the fields to the rules.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question