Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Validator at a "Page Level"

So I've already written a custom jQuery validator method. And it is tied to 1 or more individual dropdown lists. (I'm in asp.net, btw)

jQuery.validator.addMethod("dropdownBoxHasItemSelected", function (value, select, arg) {
    var returnValue = false;
    var selectedIndex = $(select).prop("selectedIndex");
    if (selectedIndex != 0) {
        returnValue = true;
    }
    return returnValue;
}, "Please select a item.");

So that isn't my question really.

I have some validation that has to be done at a "page level". Or maybe at a "GridView" level is a better way to phrase it.

Here's the scenario: (I'm using made-up data to make the explanation go easier, aka, I don't really have Toys and Foods)

I have a grid view.

Column A of the gridview is of no consequence of this, but it exists.
Column B of the gridview has a DropDownList for "FavoriteToy". 
Column C of the gridview has a DropDownList for "FavoriteFood".

So the rules go something like this.

For each row in the gridview:

You must pick a FavoriteToy or a FavoriteFood for each row.
You can pick a FavoriteToy OR a FavoriteFood, but not both on the same row.
If you pick a FavoriteToy of "TeddyBear" in RowX, none of the other rows can have TeddyBear chosen. (Aka, each row must have a distinct FavoriteToy chosen)
If you pick a FavoriteFood of "AppleButter" in RowX, none of the other rows can have AppleButter chosen. (Aka, each row must have a distinct FavoriteFood chosen)

New rows can be added to the gridview if desired. There is a "remove" button as well, just in case the user has exhausted all FavoriteToy and FavoriteFood combinations.

Now, I've written all the validation logic (using jQuery syntax to check values and loop over everything) just fine.

I'm looking for advice on how to wire up the "grid view overall validator", using jQuery.validator.addMethod.

I guess I could wire it up to a asp:Label (an "input" of type="text" on the client side) so the error msg pops there.

Or maybe, the grid view gets rendered as "table" on the client side.

Any in general advice?

====================================================================

What I have put in so far:

The asp:net control:

<asp:HiddenField ID="hidGridViewValidatorPlaceHolder" runat="server" />

and the method below

jQuery.validator.addMethod("gridViewValiatorMethod", function (value, select, arg) {
    var returnValue = true;
    var errorMsg = SuperValidationWrapper();
    if (errorMsg != "") {
        alert(errorMsg);
        returnValue = false;
    }
    return returnValue;
}, "Please address the issues in the gridview.");

SuperValidationWrapper() has all the loopty-loops. And I return a concatenated string of there are any issues. I don't like that, but that's what I've done.

The alert box gives you the details, and the text "Please address the issues in the gridview." shows up if there are any issues.

Any improvements are appreciated.

like image 383
granadaCoder Avatar asked Nov 12 '22 08:11

granadaCoder


1 Answers

Validator is really designed to validate fields, but since you don't want validate a field, you can't bind your expression to a field.

You really need to put the "page validation" logic in the submit handler

http://docs.jquery.com/Plugins/Validation/validate#options

like image 116
Simon Halsey Avatar answered Nov 15 '22 11:11

Simon Halsey