开发者

jQuery Validate: Group Validation But Not Field Validation

I'm using jquery validate and I've set up groups. However, I'm having trouble figuring out the best way to validate each group seperately from the indiv开发者_高级运维idual fields that it contains.

To illustrate this:

I have city, state, and zip ( in that order ). If someone enters a value for zip, they should get positive confirmation ( I've got that part working with a green checkmark by the input ), however, so long as either state and zip are empty the group validation message should still show.

On various attempts I've come up to the following problems: - User skips over "city" and the validation the message displays but then when input is provided for "zip", the validation message goes away. ( this is weird...maybe a bug? ) - User enters "city" and they get the group error message before they even get to enter "state" - User enters "city" but won't get the checkmark because the other inputs in the group are empty

I'm leaning towards addressing this by overwriting the "onfocusout" event ( by having it check all the inputs in the group on focusout ) but not sure if there is a better way via some of validate's built in methods

Thanks!

*UPDATE

The solutions I've come up with are rather ugly and none of them work consistently, below is an example, it results in the inverse of the first problem I listed above. Now the validation message only shows when the last field in a group is filled in.

Maybe group validation rely's on the validation result of the last test in a group?

Anyway...here's the mess I've come up with so far:

Set the group:

groups : { location : 'city state zip' }

Create a rule for the group ( note it has to be an array to preserve the order ):

rules : { location : ['city, 'state', 'zip']}

Now override the onfocusout:

    onfocusout : function(el) {
        var groups = this.groups
            , elName = el.name
            , elGroup = groups[elName]
            , $el = $(el)

        if (!this.checkable(el) ){
            this.element(el));
        }

        // If the element belongs to a group, validate all elements in the group 
        // that come before the current element
        if (elGroup) {
            var groupMembers = this.settings.rules[elGroup];

            for (var i=0; i<groupMembers.length; i++) {
                if ( groupMembers[i] === elName ) {
                    break;
                }
                this.element( $('[name="' + groupMembers[i] + '"]') );
            }
        }
    }


It seems the "groups" feature of this plugin has a few issues that are still outstanding, a few of which look to be related to your problem.

https://github.com/jzaefferer/jquery-validation/issues/search?q=groups


you can use groups for that, take this example:

$("form").validate({
    rules: {
        City: { required: true },
        Zip: { required: true },
        State: { required: true }
    },
    groups: {
        Location: "City Zip State"
    },
   errorPlacement: function(error, element) {
       if (element.attr("name") == "City" || element.attr("name") == "Zip" || element.attr("name") == "State") 
        error.insertAfter("#State");
       else 
        error.insertAfter(element);
   }
});
  • the rules set the 3 fields to true
  • the groups defines them to be validated as a group
  • and the errorPlacement function defines where to put the error message
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜