33

With MVC3 you have two new mechanisms for adding custom validation. These are (1) subclass ValidationAttribute or (2) implement IValidatableObject.

The ValidationAttribute allows you to add client side validation relatively simply by implementing IClientValidatable (and registering a new adapter and method via jQuery).

IValidatableObject is more suited to one-off validation requirements where reuse is not an option. It also results in slighlty simpler code. It would therefore be my choice for a number of scenarios. Unfortunately, I do not see an easy way of implementing client side validation using this method.

So the question is what am I missing and how DO you get JS validation when using IValidatableObject?

Paul Hiles
  • 9,558
  • 7
  • 51
  • 76

3 Answers3

23

As I did not get a (valid) answer here, I asked a couple of people from Microsoft and they confirmed that it was not currently possible.

Brad Wilson:

At this time, only property level validators can emit client-side validation (as that lines up much better with the idea of input validation in the form of the browser... there is no "model" to speak of, from the browser's point of view).

Stuart Leeks:

I don't believe you can hook up client validation with IValidatableObject

Adam Naylor
  • 6,172
  • 10
  • 49
  • 69
Paul Hiles
  • 9,558
  • 7
  • 51
  • 76
  • Does this mean that there is no easy way for hooking a custom javascript validation on the view? I would like to have a simple function that I could plug into the existing client side validation mechanism. – pauloya Aug 18 '11 at 11:35
  • [An ASP.NET forums post](http://forums.asp.net/post/5593598.aspx): *currently jquery validation only validates input fields (no model level validation), so there is no natural mapping for class level validation. A common way to add model level validation is add a hidden field, and map model level validation to the hidden.* http://stackoverflow.com/a/5817958/11683 demostrates this approach - with the exception that the field is not hidden. – GSerg Nov 20 '14 at 20:14
  • Using a [Remote] attribute on the model could help; read more here: http://msdn.microsoft.com/en-us/library/gg508808%28VS.98%29.aspx – Tohid Jan 19 '15 at 21:57
4

http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx http://blogs.msdn.com/b/stuartleeks/archive/2010/07/28/asp-net-mvc-adding-client-side-validation-to-validatepasswordlengthattribute-in-asp-net-mvc-3-preview-1.aspx

ASP.NET MVC 3 now honors the IValidateObject interface when model binding (in addition to all of the other validation approaches it already supported with MVC 2), and will retrieve validation errors from it and automatically flag/highlight impacted fields within a view using the built-in HTML form helpers.

ASP.NET MVC 3 also introduces a new IClientValidatable interface that allows ASP.NET MVC to discover at runtime whether a validator has support for client validation. This interface has been designed so that it can be integrated with a variety of validation frameworks. MVC 3 also introduces a new IMetadataAware interface that simplifies how you can contribute to the ModelMetadata creation process.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • 3
    This is all related to adding client side validation to Validation Attributes which as I stated in my question, works very well. I am asking about adding client side validation to IValidatableObject. – Paul Hiles Jan 20 '11 at 15:26
0

This article describes a way to access the container object in client side validation in ASP.NET MVC. You can probaly use this to get started on using your own client side validation

edosoft
  • 17,121
  • 25
  • 77
  • 111
  • 1
    Again, this is related to data annotations and is MVC2 specific. There is a completely different mechanism for adding client side validation for attribute based validation in MVC3 (IClientValidatable) which is much easier to implement than the old DataAnnotationsModelValidator approach. HOWEVER, I am not talking about attributes, I am talking about the other MVC3 way of validating: IValidatableObject. – Paul Hiles Jan 27 '11 at 11:51
  • @PaulHiles - Hi Paul, Did you find anything suitable for the client side validation in mvc ? – Smoking monkey Sep 11 '14 at 04:46