yii2中的js活动表单验证 [英] js active form validation in yii2

查看:234
本文介绍了yii2中的js活动表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的表单,我正在尝试使用下面编写的脚本对其进行验证:

I have a active form and I am trying to validate it using the script written below :

jQuery("#form").yiiActiveForm("submitForm");

问题是,脚本始终返回false。但是,当我第二次延迟调用它时,它返回true。

The problem is, the script always returns false. But when I call it second time with some delay, it returns true.

jQuery("#form").yiiActiveForm("submitForm");
setTimeout(function () {
if (jQuery("#form").yiiActiveForm("submitForm")) {
        //ajax call...    
    }
}, 300);

我的问题是,验证如何在yii2中起作用?
还有没有更好的方法来验证js中的活动表单?

My question is, how does validation works in yii2 ? And is there a better way to validate active forms in js?

当我使用

jQuery("#form").yiiActiveForm("submitForm");

beforeSubmit 处理程序被调用。

$('#form').on('beforeSubmit', function (e) {
    alert('message');
});

beforeSubmit 是否自动验证表单?

推荐答案

简单

您应该尝试一下

<?php
    $this->registerJs('



            jQuery("#w0").yiiActiveForm("add",{
                "id": "customer-name",
                "name": "name",
                "container": ".field-customer-name",
                "input": "#customer-name",
                "error": ".help-block.help-block-error",
                "validate": function(attribute, value, messages, deferred, $form) {

                    yii.validation.required(value, messages, {
                        "message": "Name be blank bug."
                    });

                    yii.validation.string(value, messages, {
                        "message": "Name must be a string.",
                        "max": 255,
                        "tooLong": "Name should contain at most 255 characters.",
                        "skipOnEmpty": 1
                    });
                }
        });


    ');
 ?>

更改

w0 into your form ID

"id": "customer-name" into your input field ID

"container": ".field-customer-name" into input field div container class

这篇关于yii2中的js活动表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆