This project is read-only.

xVal vs AJAX

May 21, 2009 at 9:45 AM

I'm not sure if this is the correct place but here goes..

At the moment my app is doing server side validation.  This is required a)in case javascript is turned off b)security reasons c)seems the logical place to put it.  What I have done is made the forms post via AJAX to the Controller.  This then runs the DataAnnotationsValidationRunner to check my my data annotations.  I also do other business rule validation and throw a RuleException if needed.  When the ModelState is invalid I loop through it getting the errors and returning it as a ContentResult.  On the Success event from the AJAX post it updates a DIV which uses the jQuery UI Themes(Error/Highlight) styles kind of like a nice looking ValidationSummary.  I think this looks very good. 

I have tried putting xVal into my project but have been unable to get it to work, it always does a full post.  The source shows the ClientSideValidation outputting the rules etc so I don't know why it doesn't validate client side.  Firstly will xVal handle business rule validation client side or does it just handle the DataAnnotations?  Is there a way to get Xval to put its results into the fancy looking ValidationSummary? 

At the moment I have MVC ValidationMessages next to my textboxes but because I am handling the post via AJAX when it returns errors these are not being highlighted, you only get the ValidationSummary type result.  If xVal cannot do what I have asked does anyone know of a way to highlight these ValidationMessages?


May 22, 2009 at 9:04 AM

Sounds like xVal isnt attaching to your form fields correctly. Have you checked the names of the form fields against the names outputted in the xVal javascript. Also check that you have the xVal javascript running after the form has loaded or it will try and attach the validators before they are rendered on the page.

Failing that can you run up a small test project and I would be happy to have a look for you.

Thanks, Mark

May 22, 2009 at 9:27 AM

I'm not sure what was going on but I have made a slightly alternative xVal project.  When the form is posted via an AJAX post, the controller serializes the ModelState into a JSON object.  On the OnSuccess event of the AJAX post it will loop through the JSON list and update a ValidationSummary div with the errors. It will also look for span's with the prefix val_ and the Model property name and set them to visible.  This way you get a ValidationSummary and ValidationMessages.  Not directly client side I know but still pretty cool in my humble opinion.