jQuery Validate - Enable validation for hidden fields

后端 未结 9 1756
忘了有多久
忘了有多久 2020-11-21 22:27

In the new version of jQuery validation plugin 1.9 by default validation of hidden fields ignored. I\'m using CKEditor for textarea input field and it hides the field and re

相关标签:
9条回答
  • 2020-11-21 22:47

    This worked for me within an ASP.NET site. To enable validation on some hidden fields use this code

    $("form").data("validator").settings.ignore = ":hidden:not(#myitem)";

    To enable validation for all elements of form use this one $("form").data("validator").settings.ignore = "";

    Note that use them within $(document).ready(function() { })

    0 讨论(0)
  • 2020-11-21 22:57

    Just added ignore: [] in the specific page for the specific form, this solution worked for me.

    $("#form_name").validate({
            ignore: [],
            onkeyup: false,
            rules: {            
            },      
            highlight:false,
        });
    
    0 讨论(0)
  • 2020-11-21 22:58

    This worked for me, within an ASP.NET MVC3 site where I'd left the framework to setup unobtrusive validation etc., in case it's useful to anyone:

    $("form").data("validator").settings.ignore = "";
    
    0 讨论(0)
  • 2020-11-21 22:59

    Just find the text ignore: ":hidden" in your jquery validation file and comment it. After comment this it will never loss any hidden elements to validate...

    Thanks

    0 讨论(0)
  • 2020-11-21 23:00

    So I'm going to go a bit deeper in to why this doesn't work because I'm the kind of person that can't sleep at night without knowing haha. I'm using jQuery validate 1.10 and Microsoft jQuery Unobtrusive Validation 2.0.20710.0 which was published on 1/29/2013.

    I started by searching for the setDefaults method in jQuery Validate and found it on line 261 of the unminified file. All this function really does is merge your json settings in to the existing $.validator.defaults which are initialized with the ignore property being set to ":hidden" along with the other defaults defined in jQuery Validate. So at this point we've overridden ignore. Now let's see where this defaults property is being referenced at.

    When I traced through the code to see where $.validator.defaults is being referenced. I noticed that is was only being used by the constructor for a form validator, line 170 in jQuery validate unminified file.

    // constructor for validator
    $.validator = function( options, form ) {
        this.settings = $.extend( true, {}, $.validator.defaults, options );
        this.currentForm = form;
        this.init();
    };
    

    At this point a validator will merge any default settings that were set and attach it to the form validator. When you look at the code that is doing the validating, highlighting, unhighlighting, etc they all use the validator.settings object to pull the ignore property. So we need to make sure if we are to set the ignore with the setDefaults method then it has to occur before the $("form").validate() is called.

    If you're using Asp.net MVC and the unobtrusive plugin, then you'll realize after looking at the javascript that validate is called in document.ready. I've also called my setDefaults in the document.ready block which is going to execute after the scripts, jquery validate and unobtrusive because I've defined those scripts in the html before the one that has the call in it. So my call obviously had no impact on the default functionality of skipping hidden elements during validation. There is a couple of options here.

    Option 1 - You could as Juan Mellado pointed out have the call outside of the document.ready which would execute as soon as the script has been loaded. I'm not sure about the timing of this since browsers are now capable of doing parallel script loading. If I'm just being over cautious then please correct me. Also, there's probably ways around this but for my needs I did not go down this path.

    Option 2a - The safe bet in my eyes is to just replace the $.validator.setDefaults({ ignore: '' }); inside of the document.ready event with $("form").data("validator").settings.ignore = "";. This will modify the ignore property that is actually used by jQuery validate when doing each validation on your elements for the given form.

    Options 2b - After looking in to the code a bit more you could also use $("form").validate().settings.ignore = ""; as a way of setting the ignore property. The reason is that when looking at the validate function it checks to see if a validator object has already been stored for the form element via the $.data() function. If it finds a validator object stored with the form element then it just returns the validator object instead of creating another one.

    0 讨论(0)
  • 2020-11-21 23:01

    The validation was working for me on form submission, but it wasn't doing the reactive event driven validation on input to the chosen select lists.

    To fix this I added the following to manually trigger the jquery validation event that gets added by the library:

    $(".chosen-select").each(function() {
      $(this).chosen().on("change", function() {
        $(this).parents(".form-group").find("select.form-control").trigger("focusout.validate");
      });
    });
    

    jquery.validate will now add the .valid class to the underlying select list.

    Caveat: This does require a consistent html pattern for your form inputs. In my case, each input filed is wrapped in a div.form-group, and each input has .form-control.

    0 讨论(0)
提交回复
热议问题