views:

417

answers:

0

I am using xVal for validation in my Asp.Net MVC 1.0 project. I am running into an issue where the default DataType validation and error message is being thrown and overriding my custom RegEx error validation and message.

Here is my model with the property I want to force validation on:

[Required(ErrorMessage = "Year Published is required.")]
        [RegularExpression("^(\\d{4})$", ErrorMessage = "Please enter a valid Year Published (XXXX)")]
        public virtual int YearPublished { get; set; }

And then in my view I have the following:

<form id="AdminBookInfoForm">
<label for="Admin.Publisher">Publisher</label>
                <%= Html.DropDownList("Admin.Publisher", Model.Publishers)%> 
</form>    
<%= Html.ClientSideValidation<Book>("Admin") %>

Here is the script that is generated by xVal at the bottom of the view:

xVal.AttachValidator("Admin",
    { "Fields": 
        [
            { "FieldName": "YearPublished", "FieldRules": 
                [
                    { "RuleName": "Required", "RuleParameters": {}, "Message": "Year Published is required." },
                    { "RuleName": "RegEx", "RuleParameters": { "Pattern": "^(\\d{4})$" }, "Message": "Please enter a valid Year Published (XXXX)" },
                    { "RuleName": "DataType", "RuleParameters": { "Type": "Integer"} }
                ]
            }
        ]
    },
    {}
)

The problem is that xVal is using the default DataType validation found in the xVal.jQuery.Validate.js instead of my custom validation that I specified as attributes for my YearPublished property. If I go into the xVal.jquery.Validate.js and comment out the following code, my custom validation works, but I don't think that this is the right way to get it to function the way it should.

case "DataType":
                    switch (ruleParams.Type) {
                        case "EmailAddress":
                            options.email = true;
                            options.messages = { email: errorText || xVal.Messages.DataType_EmailAddress };
                            break;
                        case "Integer":
                            options.xVal_regex = ["^\\-?\\d+$", ""];
                            options.messages = { xVal_regex: errorText || xVal.Messages.DataType_Integer || "Please enter a whole number." };
                            break;
                        case "Decimal":
                            options.number = true;
                            options.messages = { number: errorText || xVal.Messages.DataType_Decimal };
                            break;
                        case "Date":
                            options.date = true;
                            options.messages = { date: errorText || xVal.Messages.DataType_Date };
                            break;
                        case "DateTime":
                            options.xVal_regex = ["^\\d{1,2}/\\d{1,2}/(\\d{2}|\\d{4})\\s+\\d{1,2}\\:\\d{2}(\\:\\d{2})?$", ""];
                            options.messages = { xVal_regex: errorText || xVal.Messages.DataType_DateTime || "Please enter a valid date and time." };
                            break;
                        case "Currency":
                            options.xVal_regex = ["^\\D?\\s?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$", ""];
                            options.messages = { xVal_regex: errorText || xVal.Messages.DataType_Currency || "Please enter a currency value." };
                            break;
                        case "CreditCardLuhn":
                            options.xVal_creditCardLuhn = true;
                            if (errorText != null) options.messages = { xVal_creditCardLuhn: errorText };
                            break;
                    }
                    break;