1
votes

I have an issue where, when I complete one field on a form and move to another field, either by using the Tab key or the mouse, the form automatically submits and goes to the page specified in actionCreate or actionEdit (as appropriate). If I reopen the form, that single data element was saved, and I can enter one more data element only to have the form automatically resubmit again as soon as I move to another field.

Some background information:

  • This behavior occurs with Google Chrome v.56.x and Firefox v.52.x. It does not occur with Microsoft Edge. (In Edge, the forms work as they should. Unfortunately, I really need the much more robust development tools in Chrome and Firefox.)
  • I am using a localhost with an XAMPP stack (Apache 2.4.23 (Win32), PHP 5.6.28, Yii 2.0.11.2)
  • This issue occurs on all forms (many) generated by gii.
  • This behavior occurs whether I use the standard Yii ActiveForm or the Kartik-V ActiveForm.
  • Apache logs show no errors whatsoever. Yii runtime/logs/app.log also shows no errors.

This behavior has been going on for a very long time; I've just been working around it. Now, it's reached the point where I really need to do something about it. I've tried Googling to see if anyone had identified the issue and haven't been able find any potential resolutions online.

I'm kind of at a loss as to where to go next on this thing. Given that the forms are very standard and do work in Edge, I'm kind of thinking that this may be more a Chrme/Firefox/localhost issue than a Yii issue, but I can't find anything on the behavior.

I'd greatly appreciate any thoughts, ideas, and especially resolutions that anyone might have to offer.

An example controller (one of many associated with this behavior:

<?php
// This class was automatically generated by a giiant build task
// You should not change it manually as it will be overwritten on next build

    namespace backend\controllers\base;

    use common\models\PaletteColors;
    use common\models\search\PaletteColorsSearch;
    use yii\web\Controller;
    use yii\web\HttpException;
    use yii\helpers\Url;
    use yii\filters\AccessControl;
    use dmstr\bootstrap\Tabs;

    /**
     * PaletteColorsController implements the CRUD actions for PaletteColors model.
     */
    class PaletteColorsController extends Controller
    {

        /**
         * @var boolean whether to enable CSRF validation for the actions in this controller.
         * CSRF validation is enabled only when both this property and [[Request::enableCsrfValidation]] are true.
         */
        public $enableCsrfValidation = false;

        /**
         * Lists all PaletteColors models.
         *
         * @return mixed
         */
        public function actionIndex()
        {
            $searchModel  = new PaletteColorsSearch;
            $dataProvider = $searchModel->search($_GET);

            Tabs::clearLocalStorage();

            Url::remember();
            \Yii::$app->session['__crudReturnUrl'] = null;

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

        /**
         * Displays a single PaletteColors model.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionView($id)
        {
            \Yii::$app->session['__crudReturnUrl'] = Url::previous();
            Url::remember();
            Tabs::rememberActiveState();

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

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

            try {
                if ($model->load($_POST) && $model->save()) {
                    return $this->redirect(['view', 'id' => $model->id]);
                } elseif (!\Yii::$app->request->isPost) {
                    $model->load($_GET);
                }
            } catch (\Exception $e) {
                $msg = (isset($e->errorInfo[2])) ? $e->errorInfo[2] : $e->getMessage();
                $model->addError('_exception', $msg);
            }

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

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

            if ($model->load($_POST) && $model->save()) {
                return $this->redirect(Url::previous());
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        }

        /**
         * Deletes an existing PaletteColors model.
         * If deletion is successful, the browser will be redirected to the 'index' page.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionDelete($id)
        {
            try {
                $this->findModel($id)->delete();
            } catch (\Exception $e) {
                $msg = (isset($e->errorInfo[2])) ? $e->errorInfo[2] : $e->getMessage();
                \Yii::$app->getSession()->addFlash('error', $msg);

                return $this->redirect(Url::previous());
            }

// TODO: improve detection
            $isPivot = strstr('$id', ',');
            if ($isPivot == true) {
                return $this->redirect(Url::previous());
            } elseif (isset(\Yii::$app->session['__crudReturnUrl']) && \Yii::$app->session['__crudReturnUrl'] != '/') {
                Url::remember(null);
                $url                                   = \Yii::$app->session['__crudReturnUrl'];
                \Yii::$app->session['__crudReturnUrl'] = null;

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

        /**
         * Finds the PaletteColors model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         *
         * @param integer $id
         *
         * @return PaletteColors the loaded model
         * @throws HttpException if the model cannot be found
         */
        protected function findModel($id)
        {
            if (($model = PaletteColors::findOne($id)) !== null) {
                return $model;
            } else {
                throw new HttpException(404, 'The requested page does not exist.');
            }
        }
    }

The _form.php file associated with this controller:

<?php

    use kartik\helpers\Html;
    use kartik\icons\Icon;
    use kartik\tabs\TabsX;
    use kartik\widgets\ActiveForm;

    // Initialize framework as per <code>icon-framework</code> param in Yii config
    Icon::map($this);

    // Initialize a specific framework - e.g. Web Hosting Hub Glyphs
    Icon::map($this, Icon::FA);

    /**
     * @var yii\web\View                $this
     * @var common\models\PaletteColors $model
     * @var yii\widgets\ActiveForm      $form
     */

?>

<div class="palette-colors-form">

    <?php $form = ActiveForm::begin([
                                        'id'                   => 'PaletteColors',
                                        'type'                 => ActiveForm::TYPE_VERTICAL,
                                        // 'enctype' => 'multipart/form-data',
                                        'enableAjaxValidation' => true,
                                        'fieldConfig'          => [
                                            'autoPlaceholder' => false,
                                            'showErrors'      => true,
                                        ],
                                    ]
    );
    ?>

    <div class="">
        <?php $this->beginBlock('main'); ?>

        <p>

            <!-- attribute palette_id -->
            <?= // generated by schmunk42\giiant\generators\crud\providers\core\RelationProvider::activeField
                $form->field($model, 'palette_id')->dropDownList(
                    \yii\helpers\ArrayHelper::map(common\models\Palettes::find()->all(), 'id', 'name'),
                    [
                        'prompt'   => 'Select',
                        'disabled' => (isset($relAttributes) && isset($relAttributes['palette_id'])),
                    ]
                ); ?>

            <!-- attribute palette_color_group_id -->
            <?= // generated by schmunk42\giiant\generators\crud\providers\core\RelationProvider::activeField
                $form->field($model, 'palette_color_group_id')->dropDownList(
                    \yii\helpers\ArrayHelper::map(common\models\PaletteColorGroups::find()->all(), 'id', 'name'),
                    [
                        'prompt'   => 'Select',
                        'disabled' => (isset($relAttributes) && isset($relAttributes['palette_color_group_id'])),
                    ]
                ); ?>

            <!-- attribute rgba_red -->
            <?= $form->field($model, 'rgba_red')->textInput() ?>

            <!-- attribute rgba_green -->
            <?= $form->field($model, 'rgba_green')->textInput() ?>

            <!-- attribute rgba_blue -->
            <?= $form->field($model, 'rgba_blue')->textInput() ?>

            <!-- attribute hsla_hue -->
            <?= $form->field($model, 'hsla_hue')->textInput() ?>

            <!-- attribute hsla_saturation -->
            <?= $form->field($model, 'hsla_saturation')->textInput() ?>

            <!-- attribute hsla_luminosity -->
            <?= $form->field($model, 'hsla_luminosity')->textInput() ?>

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

            <!-- attribute rgba_alpha -->
            <?= $form->field($model, 'rgba_alpha')->textInput() ?>

            <!-- attribute hsla_alpha -->
            <?= $form->field($model, 'hsla_alpha')->textInput() ?>

            <!-- attribute cmyk_cyan -->
            <?= $form->field($model, 'cmyk_cyan')->textInput() ?>

            <!-- attribute cmyk_magenta -->
            <?= $form->field($model, 'cmyk_magenta')->textInput() ?>

            <!-- attribute cmyk_yellow -->
            <?= $form->field($model, 'cmyk_yellow')->textInput() ?>

            <!-- attribute cmyk_key -->
            <?= $form->field($model, 'cmyk_key')->textInput() ?>

            <!-- attribute radial_gradient -->
            <?= $form->field($model, 'radial_gradient')->textarea(['rows' => 6]) ?>

            <!-- attribute description -->
            <?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>

            <!-- attribute comments -->
            <?= $form->field($model, 'comments')->textarea(['rows' => 6]) ?>

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

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

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

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

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

            <!-- attribute hex_complement -->
            <?= $form->field($model, 'hex_complement')->textInput(['maxlength' => true]) ?>
        </p>
        <?php $this->endBlock(); ?>

        <?=
            TabsX::widget(
                [
                    'encodeLabels' => false,
                    'items'        => [
                        [
                            'label'   => '<i class="fa fa-home"></i> Palette Colors',
                            'content' => $this->blocks['main'],
                            'active'  => true,
                        ],
                    ],
                    'position'     => TabsX::POS_ABOVE,
                    'encodeLabels' => false
                ]
            );
        ?>
        <hr/>

        <?php echo $form->errorSummary($model); ?>

        <div class="row">
            <div class="col-md-12 text-right">
                <?= Html::submitButton(
                    '<span class="glyphicon glyphicon-check"></span> ' .
                    ($model->isNewRecord ? 'Create' : 'Save'),
                    [
                        'id'    => 'save-' . $model->formName(),
                        'class' => 'btn btn-success'
                    ]
                );
                ?>
            </div>
        </div>

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

    </div>

</div>
1
update question with controller action and form. - Insane Skull
I'll check browser console to show what action launch form submit - Fabrizio Caldarelli

1 Answers

0
votes

For some reason it did the same for me in activeForm, what seemed to help albeit I don't know exactly why, was chaging:

<?php $form = ActiveForm::begin([
    'enableClientValidation' => false,
    'enableAjaxValidation' => true,
]) ?>

to

<?php $form = ActiveForm::begin([
//    'enableClientValidation' => false,
//    'enableAjaxValidation' => true,
]) ?>