]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Форма создания на главной странице
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 15 Jan 2025 13:53:35 +0000 (16:53 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 15 Jan 2025 13:53:35 +0000 (16:53 +0300)
erp24/controllers/StoresTypeListController.php
erp24/records/StoresTypeList.php
erp24/views/stores-type-list/index.php

index 02e5b985e07297b14a9f967aba0998d0f5af5b5c..aaf8a04d04824b2f97def19f395aa4ed59ac08b7 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace app\controllers;
 
+use Yii;
 use yii\data\ActiveDataProvider;
 use yii\filters\VerbFilter;
 use yii\web\Controller;
@@ -38,22 +39,25 @@ class StoresTypeListController extends Controller
      */
     public function actionIndex()
     {
+        $model = new StoresTypeList();
+        if ($this->request->isPost) {
+            if ($model->load($this->request->post()) && $model->validate()) {
+                $model->type_alias = strtolower(preg_replace('/[\s-]+/', '_', $model->type_name));
+
+                if ($model->save()) {
+                    Yii::$app->session->setFlash('success', 'Тип магазина успешно сохранен.');
+                    return $this->redirect(['index']);
+                }
+            }
+        }
+
         $dataProvider = new ActiveDataProvider([
             'query' => StoresTypeList::find(),
-            /*
-            'pagination' => [
-                'pageSize' => 50
-            ],
-            'sort' => [
-                'defaultOrder' => [
-                    'id' => SORT_DESC,
-                ]
-            ],
-            */
         ]);
 
         return $this->render('index', [
             'dataProvider' => $dataProvider,
+            'model' => $model,
         ]);
     }
 
index ceab1b507d4b60585742da7e27fc09697524f7be..1955e758bd64a92e245961e48148b073edca4597 100644 (file)
@@ -29,8 +29,9 @@ class StoresTypeList extends \yii\db\ActiveRecord
     public function rules()
     {
         return [
-            [['type_name', 'type_alias'], 'required'],
+            [['type_name'], 'required'],
             [['created_at'], 'safe'],
+            [['type_name'], 'unique', 'message' => 'Название "{value}" уже существует.'],
             [['type_name', 'type_alias', 'type_description'], 'string', 'max' => 255],
         ];
     }
index fe65d02d90e6cd444e93853f328ef92ccc458aff..a7de5094165e0895bec70cfe1261c94ad90f8301 100644 (file)
@@ -1,13 +1,16 @@
 <?php
 
+use yii\widgets\ActiveForm;
+use yii\widgets\Pjax;
 use yii_app\records\StoresTypeList;
 use yii\helpers\Html;
 use yii\helpers\Url;
 use yii\grid\ActionColumn;
 use yii\grid\GridView;
 
-/** @var yii\web\View $this */
-/** @var yii\data\ActiveDataProvider $dataProvider */
+/* @var $this yii\web\View */
+/* @var $dataProvider yii\data\ActiveDataProvider */
+/* @var $model yii_app\records\StoresTypeList */
 
 $this->title = 'Тип магазина';
 $this->params['breadcrumbs'][] = $this->title;
@@ -16,10 +19,29 @@ $this->params['breadcrumbs'][] = $this->title;
 
     <h1><?= Html::encode($this->title) ?></h1>
 
-    <p>
-        <?= Html::a('Создать', ['create'], ['class' => 'btn btn-success']) ?>
-    </p>
+    <?php Pjax::begin(['enablePushState' => false]); ?>
+    <?php if (Yii::$app->session->hasFlash('success')): ?>
+        <div class="alert alert-success">
+            <?= Yii::$app->session->getFlash('success') ?>
+        </div>
+    <?php endif; ?>
 
+    <?php if (Yii::$app->session->hasFlash('error')): ?>
+        <div class="alert alert-danger">
+            <?= Yii::$app->session->getFlash('error') ?>
+        </div>
+    <?php endif; ?>
+    <div class="form-container mb-4 col-4">
+        <?php $form = ActiveForm::begin([
+            'options' => ['data-pjax' => true],
+        ]); ?>
+
+        <?= $form->field($model, 'type_name')->textInput(['maxlength' => true])->label('Название типа') ?>
+
+        <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
+
+        <?php ActiveForm::end(); ?>
+    </div>
 
     <?= GridView::widget([
         'dataProvider' => $dataProvider,
@@ -33,12 +55,12 @@ $this->params['breadcrumbs'][] = $this->title;
             'created_at',
             [
                 'class' => ActionColumn::class,
-                'urlCreator' => function ($action, StoresTypeList $model, $key, $index, $column) {
+                'urlCreator' => function ($action, $model, $key, $index, $column) {
                     return Url::toRoute([$action, 'id' => $model->id]);
-                }
+                },
             ],
         ],
     ]); ?>
 
-
+    <?php Pjax::end(); ?>
 </div>