Form submits as soon as cursor enters a field

I’ve had this issue in Yii2 development off and on for quite some time. I have the latest version of Yii2 and have generated the CRUD views with Schmunk Giiant (latest version also). Basically, I can display a form for create or update. The minute that I click in a text input (or any other input), the form immediately autosubmits before I have the opportunity to enter anything in the field. It simply immediately goes to the view (if I’m in update) or back to the index (if I’m in create). I cannot do any data entry whatsoever. I’ve tried the forms with Google Chrome, Firefox, Vivaldi, Brave, Opera and Microsoft Edge; they all respond the same way. If I open a form that is not part of the Yii2 framework, such as Amazon.com or any others, the forms act just as you would expect. I can do data entry without any problems. It’s just Yii2 forms that behave this bizarre way.

Although I originally created the views, controller and models using Giiant, I also tried created them using the core Yii2 gii, and they have the same issue, so it’s not the Giiant extension at play here.

There are no error messages in the Apache logs or the Yii2 runtime error logs for the time periods in which this behavior occurred.

If anybody can give me any ideas on how to fix this or further troubleshoot it, I would be eternally grateful. Unless I can resolve this matter, I’m going to have to find a different development platform.

Hi Larry,

It looks like the ajax validation call is causing the issue.

The following is an actionCreate() method in one of my yii apps. It has been created by gii and I modified a bit (not very much).




    /**

     * Creates a new Bp model.

     * If creation is successful, the browser will be redirected to the 'index' page.

     * @return mixed

     */

    public function actionCreate()

    {

        $model = new Bp();

        if ($model->load(Yii::$app->request->post())) {

            if (Yii::$app->request->isAjax) {  // ajax validation

                Yii::$app->response->format = Response::FORMAT_JSON;

                return ActiveForm::validate($model);

            }

            if ($model->save()) {  // validate and save

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

            }

        }

        return $this->render('create', [

            'model' => $model,

        ]);

    }



The basic structure of the actionCreate and the actionUpdate should be the same in every yii application. It handles the ajax validation call using ActiveForm::validate() method in this way.

Check how your ajax validation call get processed. Does it fall into the "if" statement block and processed by ActiveForm::validate()? It seems like your ajax validation call get processed by "save()" method in the same way as the normal submit of the form.

I had high hopes, but … that didn’t fix it. I copied and pasted your actionCreate into my actionCreate and actionUpdate, just changing the Model class. The behavior didn’t change at all. It still submits the form as soon as I click in any field. I had thought it might have something to do with Ajax, so I also tried disabling enableAjaxValidation, and that didn’t help either. It’s got me completely stumped. By the way, I’m using Kartik-V’s ActiveForm widget. Tomorrow, when I get time, I’m going to try dispensing with that. I can’t imagine that the widget would be at fault. I’ve used it for years.

I see.

So the problem seems to lie in the scripts of the view. Some kind of event handler might catching a click event of the inputs and submitting the form.

Did you install an extra widget or something like that other than Kartik’s ActiveForm that would insert some scripts? Anyway, I would try to trace the code execution of the scripts of the page using a browser’s debugging tool.