#1) I've till now, seen absolutely 0 examples of how to handle any sort of validation, and would like to see anythign that would help out there. #2) Can server controls be used?
If you meant the validation controls, they cannot be used (yet?) because they validate other server controls, which in turn require a form with runat=server.
It's possible and probable that the MVC team will provide validation helpers (or even the MVC cotrib project) for client-side validation. The other half of the story is the server-side validation, which will be split into model code that validates the fields
and controller code that reports validation problems to the user. See the following fictitious code:
Look at jquery validate... Its the best client side validation framework I have found thus far. As we work on Client Script support for Mvc Contrib I am hoping to include validation in some way to make it easier.
ronnyek
Member
67 Points
24 Posts
MVC Form Validation
Dec 25, 2007 05:50 AM|LINK
MVC validation server controls
Ikarii Software
dimi3
Member
318 Points
166 Posts
Re: MVC Form Validation
Dec 25, 2007 12:27 PM|LINK
Excellent remark! I am also looking for something for validating my forms with the MVC Framework. Can someone help?
sergiopereira
Member
227 Points
61 Posts
Re: MVC Form Validation
Dec 25, 2007 04:23 PM|LINK
If you meant the validation controls, they cannot be used (yet?) because they validate other server controls, which in turn require a form with runat=server.
It's possible and probable that the MVC team will provide validation helpers (or even the MVC cotrib project) for client-side validation. The other half of the story is the server-side validation, which will be split into model code that validates the fields and controller code that reports validation problems to the user. See the following fictitious code:
[ControllerAction] public void Create() { Product prod = new Product(); prod.UpdateFrom(Request.Form); ValidationResult result; result = product.Validate(); if(result.Success) { ProductRepository.Save(prod); RedirectToAction("List"); } else //validation failed { TempData["validationErrors"] = result.GetErrorMessages(); RedirectToAction("New"); } }Sergio Pereira
http://devlicio.us/blogs/sergio_pereira/
stribny
Member
87 Points
24 Posts
Re: MVC Form Validation
Dec 25, 2007 05:43 PM|LINK
abombss
Member
574 Points
164 Posts
Re: MVC Form Validation
Dec 25, 2007 06:16 PM|LINK
Look at jquery validate... Its the best client side validation framework I have found thus far. As we work on Client Script support for Mvc Contrib I am hoping to include validation in some way to make it easier.
dimi3
Member
318 Points
166 Posts
Re: MVC Form Validation
Dec 28, 2007 10:16 AM|LINK
@ Sergio Pereira:
How can I use ValidationResult ?
(I use LINQ).
It would be great if you could provide another fictitious code.
Thank you.
sergiopereira
Member
227 Points
61 Posts
Re: MVC Form Validation
Dec 28, 2007 03:22 PM|LINK
OK. More fictitious code to the rescue.
public partial class Product { public ValidationResult Validate() { ValidationResult result = new ValidationResult(); if(this.Name.IndexOfAny('~', '!', '^')) { result.AddError("The product Name has invalid characters."); } if(this.UnitPrice < 0.0) { result.AddError("The product price cannot be negative."); } //..and so on... return result; } } public class ValidationResult { public ValidationResult() { } private List<string> messages = new List<string>(); public bool Success {get{ return messages.Count > 0;} } public void AddError(string errorMsg) { messages.Add(errorMsg); } public string[] GetErrorMessages() { return messages.ToArray(); } }Needless to say, this is not tested and based on more complex real code.
Sergio Pereira
http://devlicio.us/blogs/sergio_pereira/
dimi3
Member
318 Points
166 Posts
Re: MVC Form Validation
Dec 28, 2007 03:45 PM|LINK
@ Sergio Pereira:
Thanks! It looks great!
However, there is a little something I quite don't understand.
Is there something in common between your class ValidationResult and this one:
http://msdn2.microsoft.com/en-us/library/system.windows.controls.validationresult.aspx
?
sergiopereira
Member
227 Points
61 Posts
Re: MVC Form Validation
Dec 28, 2007 04:22 PM|LINK
Yes, the name :) The class you mention appears to be part of WPF.
Sergio Pereira
http://devlicio.us/blogs/sergio_pereira/
dimi3
Member
318 Points
166 Posts
Re: MVC Form Validation
Dec 28, 2007 04:29 PM|LINK
Ok :)
Thank you for your answers.
(I personally consider this post as Resolved)
I wish you a happy new year.