Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IValidatableObject in MVC3 - client side validation

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?

like image 240
Paul Hiles Avatar asked Jan 20 '11 15:01

Paul Hiles


People also ask

How client side validation is implemented in MVC?

Firstly, you just need to create an ASP.NET MVC application. To create a new ASP.NET MVC application, Open Visual Studio choose File, New, then Project. It will open a New Project window, from where you need to choose node Visual C# and then Web and from the right pane you need to choose ASP.NET Web Application.

How can use client side validation in asp net?

Some ASP.NET server controls use client side scripting to provide response to the users without posting back to the server. For example, the validation controls. Apart from these scripts, the Button control has a property OnClientClick, which allows executing client-side script, when the button is clicked.

Which method is used to enable/disable client side validation in MVC?

To disable client-side validation, set the page's ClientTarget property to 'Downlevel' ('Uplevel' forces client-side validation). Alternatively, you can set an individual validator control's EnableClientScript property to 'false' to disable client-side validation for that specific control.

Which validation provider is installed by default in asp net mvc3?

The client side validation is enabled by default in web.


Video Answer


1 Answers

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

like image 72
Paul Hiles Avatar answered Oct 04 '22 04:10

Paul Hiles