简体   繁体   English

单击简单按钮即可验证Yii2 Activeform

[英]Validate Yii2 Activeform on simple button click

I want to validate my Yii2 activeform on simple button click which is not submitButton. 我想在不是SubmitButton的简单按钮单击上验证我的Yii2 activeform。 I tried $('#formId').yiiActiveForm("validate") but it is not working. 我尝试了$('#formId').yiiActiveForm("validate")但是它不起作用。

I also tried - $('#formId').yiiActiveForm('submitForm') it validates form but it also submit it if everything is ok. 我也尝试过- $('#formId').yiiActiveForm('submitForm')可以验证表单,但如果一切正常,它也可以提交表单。 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 单击按钮时可以通过什么方法验证此活动表单

To validate your form perform ajax validation only inside your action as follows. 要验证表单,请仅在操作内部执行Ajax验证,如下所示。 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();
        }
    }

您必须在表单的options中设置enableClientValidation = false,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM