One solution is to build the js rules directly from the DataAnnotations - the following snippet of code does this and demonstrates loading both built in DataAnnotations and custom annotations, it generates a module that can be loaded using requirejs to provide rule data for jQuery validation.
A set of rules is generated for every object in the namespace suplied in "ModelNamespace" that has DataAnnotations.
The following example is best used in builds build from the command line - the reason being that the assembly cannot be unloaded following inspection (I have not been able to come up with a solution for this short of building a full plugin mechanism for loading the assembly to be inspected). My workaround for this is to only generate the js for release builds using the following task configuration:
<Target Name="AfterBuild" Condition="$(Configuration) == 'Release'">
<CreateJsFromAnnotations ModelAssembly="bin\WebInterface.dll" ModelNamespace="WebInterface.Models" OutputFile="Scripts\common\generated\ModelMetadata.js" />
</Target>
Sample output would be something along the lines of:
define(
[],
function () {
return {
"folderValidation": {
"folderapp": {
"locationparsing": "PathOnly",
"regex": "^[^\\s]*$",
"maxlength": 256,
"required": true
},
"folderproxy": {
"locationparsing": "PathOnly",
"regex": "^[^\\s]*$",
"maxlength": 256,
"required": true
}
}
};
}
);
No comments:
Post a Comment