DB error when saving model

Hello,
I’m new in programming using Yii and today ran into a problem.
When I’m trying to save a model, I’m getting the error:
`Database Exception – yii\db\Exception

## SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
The SQL being executed was: INSERT INTO `templates` (`id`) VALUES (DEFAULT)`

Data is getting from form. Fields of the form are not blank.
The code of the form and controller was generated by Gii.

The code of model is:
namespace app\models;

use yii\db\ActiveRecord;
use Yii;

class Templates extends ActiveRecord
{
public function rules()
    {
        return [
            [['name', 'code'], 'required'],
            [['code'], 'string'],
            [['name'], 'string', 'max' => 255],
            [['name', 'code'], 'safe'],
        ];
    }
public static function tableName()
{
	return 'templates';
}
}

The code of controller:

namespace app\controllers;

use Yii;
use app\models\Templates;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * TemplatesController implements the CRUD actions for Templates model.
 */
class TemplatesController extends Controller 
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Templates models.
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Templates::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Templates model.
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Templates model.
     * If creation is successful, the browser will be redirected to the 'view' page.
*/
    public function actionCreate()
    {
        $model = new Templates();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

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

    /**
     * Updates an existing Templates model.
     * If update is successful, the browser will be redirected to the 'view' page.
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

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

    /**
     * Deletes an existing Templates model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Templates model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     */
    protected function findModel($id)
    {
        if (($model = Templates::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

And… Database structure:


I just can’t understand causes of the error and why the data is not inserting into SQL request.
Can anybody help me?
Thank you.

Model and Controller seems to be all right. I do not know the reason why there is the safe rule for attributions name and code. I understand safe validator as special validator for attributes which we consider to safe without any checking, so it does not make a sense to validate something with string validator and the with safe. Or am I wrong? Please someone more experienced could comment on this.

Please post here your view form code too.

Maybe you could look at the debug panel - click on the Yii logo on the right-down corner. Using Yii::debug() you can trace what really happened to the data you send from the form. More info: https://www.yiiframework.com/doc/guide/2.0/en/runtime-logging

Hello,
I’ve added the safe validator because I’ve tried few different configurations of validators but they also not working.
The view is autogenerated by Gii. It’s code:
create.php

    use yii\helpers\Html;

    /* @var $this yii\web\View */
    /* @var $model app\models\Templates */

    $this->title = 'Create Templates';
    $this->params['breadcrumbs'][] = ['label' => 'Templates', 'url' => ['index']];
    $this->params['breadcrumbs'][] = $this->title;
    ?>
    <div class="templates-create">

        <h1><?= Html::encode($this->title) ?></h1>

        <?= $this->render('_form', [
            'model' => $model,
        ]) ?>

    </div>

_form.php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use leandrogehlen\codemirror\CodeMirror;

/* @var $this yii\web\View */
/* @var $model app\models\Templates */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="templates-form">

    <?php $form = ActiveForm::begin(); ?>

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

    <?= $form->field($model, 'code')->widget(CodeMirror::className(),
['pluginOptions' => ['mode' => 'text/html']])->label('Template code'); ?>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

Also, I’ve already tried the debugger. The data from the form are being transmitted to the model successfully and it’s passing the $model->validate() (it returns true). But the $model->save() sends empty data to the DB.