Ninja Turtle
Ninja Turtle

Reputation: 1353

Validate Yii2 Activeform on simple button click

I want to validate my Yii2 activeform on simple button click which is not submitButton. I tried $('#formId').yiiActiveForm("validate") but it is not working.

I also tried - $('#formId').yiiActiveForm('submitForm') it validates form but it also submit it if everything is ok. I don't want to submit that form. I just want to validate it on button click and if everything is ok than i want to open a modal popup.

What are possible ways to validate this activeform on button click

Upvotes: 2

Views: 1384

Answers (2)

Mohan
Mohan

Reputation: 616

You have to set enableClientValidation = false, in you'r form's options .

Upvotes: 1

Nitin Pund
Nitin Pund

Reputation: 1092

To validate your form perform ajax validation only inside your action as follows. This will only validate your form

public function actionValidateForm() {
        $model = new Form();

        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
            Yii::$app->end();
        }
    }

Upvotes: 2

Related Questions