Validation Settings

Starting from the 3.0.0 release the toolkit Nuget package provides a js and css bundles definitions and two Layout pages one for server controls and the other one for client controls, that define all needed javascript and css files. Anyway, for a better understanding below the headers required to add manually  unobtrusive validation:

 

<script type='text/javascript' src="~/Scripts/jquery-1.x.x.min.js"></script>
<script type='text/javascript' src="~/Scripts/jquery-ui-x.x.x.custom.min.js"></script>
<script type='text/javascript' src="~/Scripts/jquery.validate-x.x.x.min.js"></script>
<script type='text/javascript' src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type='text/javascript' src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

...
...
... <script type='text/javascript' > $.validator.setDefaults({ ignore: "not([data-elementispart]):hidden" }); </script>

 

Where:

 

<script type='text/javascript' src="~/Scripts/globalize.min.js"></script>
    @Html.GlobalizationScript("~/Scripts/globalize/cultures/")

 

Is needed only if you would like to globalize your application or if you would like to use it with a culture different from the English culture. For more information see Globalization.

 

<script type='text/javascript' >
$.validator.setDefaults({
    ignore: "not([data-elementispart]):hidden"
});
</script>

 

It is needed if you use a version of the validation library greater than 1.8. Infact, starting from the above release the validation library doesn't validate  :Hidden fields as default. Since, some Mvc Controls Toolkit Controls that need to be validated use Hidden input to store values, we must enable validation for them. Since all parts of a control are marked with the data-elementispart Html5 attribute the above javascript script restores validation just for all of them.

However, if you would like to restore validation for all :Hidden fields you might substitute the above script with:

 

<script type='text/javascript' >
$.validator.setDefaults({
    ignore: ""
});
</script>

 

This is usefull if you put fields to be validated into Widgets like the jQUery UI Tabs that hide all tabs that are not selected.

Last edited Jun 22, 2014 at 11:31 AM by frankabbruzzese, version 5

Comments

No comments yet.