E
E
EvgMul2018-12-26 15:37:20
Yii
EvgMul, 2018-12-26 15:37:20

Yii2 why is the form submitted twice in a time?

Hello. Faced the following problem. Sometimes the form is sent twice, respectively, 2 records are created in the database. I tried to fill out the form in different ways under different conditions, I can not catch any pattern.
The form itself:

<?php

use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\models\City;
/* @var $this yii\web\View */

$this->title = $model->isNewRecord ? 'Создание адреса' : 'Изменение адреса';

?>

<div class="row">
    <div class="col-sm-4 col-md-3">
        <?= $this->render('/contacts/menu') ?>
    </div>
    <div class="col-sm-8 col-md-9">
        <div class="box box-primary">
            <div class="box-header">
                <h3><?// Html::encode($this->title) ?></h3>
            </div>

            <div class="box-body">
                <?= \common\widgets\Alert::widget(); ?>
                <?php
                $form = ActiveForm::begin([
                        'enableClientValidation' => true,
                    ]
                );
                ?>
                <?= $form->field($model, 'addr')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'post_code')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'id_city')->dropDownList(City::getForDropdown()); ?>
                <?= $form->field($model, 'is_head_office')->checkbox(); ?>
                <?= $form->field($model, 'mapAddr',
                    ['template' => '{label}{input}{hint}{map}{error}',])->widget(\kalyabin\maplocation\SelectMapLocationWidget::className(),
                    [
                        'attributeLatitude' => 'latitude',
                        'attributeLongitude' => 'longitude',
                        'googleMapApiKey' => 'AIzaSyCBHA4KKBAltxjI2LHONR2AhNJekCfx1Vw',
                        'draggable' => true,
                    ]); ?>

                <div class="form-group">
                    <?= Html::submitButton($model->isNewRecord ? 'Добавить' : 'Изменить', ['class' => 'btn btn-success']) ?>
                </div>

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

Action in controller:
public function actionCreateAddress()
    {
        $model = new Address();

        if ($model->load(\Yii::$app->request->post()) && $model->save()) {
            \Yii::$app->session->setFlash('success', 'Адрес успешно добавлен');
            return $this->redirect(['contacts/address']);
        }

        return $this->render('address/_form', compact('model'));
    }

Tell me, please, what could be the reason?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question