在提交之前手动触发活动表单验证 [英] trigger active form validation manually before submit

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

问题描述

是否可以通过javascript以编程方式调用活动表单验证?我需要在做一些ajax操作之前调用验证过程.

is it possible to call the active form validation programmatically via javascript? I need to call the validation procedure before doing some ajax operations.

推荐答案

我想我在这里回复有点晚了,但我只是有同样的问题,soju 的解决方案对我也不起作用.

Guess I'm a bit late with a reply here but I just had the same question and the solution by soju did not work for me either.

所以我更深入地查看了 ActiveForm 的 JS 代码,发现它似乎监视变量中每个字段的状态,如果该字段未触及",则不会触发验证,除非提交实际形式.所以我改变了我的电话:

So I looked a bit deeper into the JS-code of ActiveForm and found that it appears to monitor the status of each field in a variable and if the field is "untouched" the validation isn't triggered, unless submitting the actual form. So I changed my call to this:

var $form = $("#my-form"), 
    data = $form.data("yiiActiveForm");
$.each(data.attributes, function() {
    this.status = 3;
});
$form.yiiActiveForm("validate");

现在这似乎按我的预期工作.

This now appears to be working as I expect.

这篇关于在提交之前手动触发活动表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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