用动态添加场客户端验证 [英] client side validation with dynamically added field

查看:114
本文介绍了用动态添加场客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我使用与asp.net的MVC jQuery的不显眼的验证插件。问题是,该呈现服务器侧的所有字段获取与该方案验证,但如果我动态的形式使用javascript它不尽管加入验证添加一个字段的html-5数据 - *属性的字段。任何人都可以指导我在正确的方向上我怎样才能达到这个目标结果
感谢结果
阿迪尔


i am using jquery's unobtrusive validation plugin in with asp.net mvc. the problem is that all the fields that are rendered server side gets validated with this scheme but if i dynamically add a field in the form using javascript it is not validated despite adding html-5 data-* attributes to the field. can anyone guide me in right direction on how i can achieve this goal
thanks
adeel

推荐答案

最后,我必须做什么我想避免:挖掘到jQuery的文件。例如上面的工作,我不得不改变一行

Finally, i had to do what i wanted to avoid: digging into jquery files. For above example to work i had to change one line

(function ($) {
  $.validator.unobtrusive.parseDynamicContent = function (selector) {
    //use the normal unobstrusive.parse method
    //$.validator.unobtrusive.parse(selector); changed this line with
     $(selector).find('*[data-val = true]').each(function(){

            $.validator.unobtrusive.parseElement(this,false);
        });

    //get the relevant form
    var form = $(selector).first().closest('form');

    //get the collections of unobstrusive validators, and jquery validators
    //and compare the two
    var unobtrusiveValidation = form.data('unobtrusiveValidation');
    var validator = form.validate();

    $.each(unobtrusiveValidation.options.rules, function (elname, elrules) {
      if (validator.settings.rules[elname] == undefined) {
        var args = {};
        $.extend(args, elrules);
        args.messages = unobtrusiveValidation.options.messages[elname];
        $('[name=' + elname + ']').rules("add", args);
      } else {
        $.each(elrules, function (rulename, data) {
          if (validator.settings.rules[elname][rulename] == undefined) {
            var args = {};
            args[rulename] = data;
            args.messages = unobtrusiveValidation.options.messages[elname][rulename];
            $('[name=' + elname + ']').rules("add", args);
          }
        });
      }
    });
  }
})($);

$。validator.unobtrusive.parse内部调用parseElement方法,但是每次都用这个值发送isSKip参数设置为true这样

$.validator.unobtrusive.parse internally calls parseElement method but each time it sends isSKip parameter to true so with this value

if (!skipAttach) {
                valInfo.attachValidation();
            }

在此jquery.unobtrusive.js code不重视验证的元素,我们发现仅是最初美元页页码$ psent输入验证数据。

this code in jquery.unobtrusive.js does not attach validation to the element and we find only validation data of inputs that were initially present on the page.

注意以上达林的答案是正确的,你可以找到他提到,很多人都使用xhalent的code(发表达林)解决问题的博客。为什么它没有工作超出了我的理解。此外,你可以找到大量的职位告诉你,只是打电话

Note Darin's answer above is correct and you can find on the blog he referred that many people have solved problem using xhalent's code (posted by darin). why it did not work is beyond my understanding. Moreover, you can find plenty of posts that tell you that just calling

$.validator.unobtrusive.parse(selector) 

就足够了要验证动态加载内容

is enough for dynamically loaded content to be validated

这篇关于用动态添加场客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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