2

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

2 Answers 2

2

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();
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Nitin. Almost done can u suggest one more thing. It is not removing error class from field. even after entring correct data. Validation is working fine just error message is not getting removed
you need to set form properties like enableAjaxValidation,enableClientValidation,validateOnChange,validateOnSubmit to true
I tried validateOnChange and validateOnBlur but error message is still displaying.
what about enableAjaxValidation ? is it set to true
it is set to value true
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.