views:

167

answers:

3

I have a custom validation check that I perform on the server sidie (.net mvc).

If it fails, I want to notify the user with a message next to the input box (just how jqueries validation plugin works out of box).

Is there a built in function (in the validation plugin) I can call to do this?

Update The solution would ideally be using the plugin itself, maybe there is a method call I can use?

+1  A: 

There is an open source project called xVal that does this. Here's an excerpt from the project home page:

Project Description xVal is a validation framework for ASP.NET MVC applications. It makes it easy to link up your choice of server-side validation mechanism with your choice of client-side validation library, neatly fitting both into ASP.NET MVC architecture and conventions. Features

  • Define your validation rules using attributes on model properties, e.g.,

    [Required] [StringLength(50)] public string Name { get; set; }

(Or, if you prefer, you can supply rules programatically or you can just hard-code them in specific views)

  • Designed to fit into ASP.NET MVC conventions for handling form posts and storing and retrieving error information in ModelState
  • Use your choice of server-side validation framework. Out of the box, xVal lets you use .NET 3.5's built-in DataAnnotations classes or Castle Validator (or both). If you want to use something different, you can create your own provider just by implementing IRulesProvider.
  • Use your choice of client-side validation library. Out of the box, xVal lets you use jQuery Validation or ASP.NET's native client-side validation library (a.k.a. WebUIValidation.js, as used by WebForms). Or, use any other client-side validation library by writing a plug-in to configure it using xVal's standard JSON rules description format.
  • Supports localized error messages using resource files. Vary the language of your validation messages according to the current thread's culture.
  • Add custom validation logic that runs both on the server and on the client, either by subclassing an existing rule or by referencing a custom JavaScript function
jrummell
A: 

You could create a custom validation function and couple it with an ajax call. In this case the ajax call will return the text 'true' or 'false'.

var customFunction = function(value, element, param) {
    var isValid = false;
    $.ajax({
        url: '/something',
        data:{value: value},
        success: function(data, textStatus) {isValid = data;},
        dataType: 'json',
     async: false
 });
    return isValid;
};

$.validator.addMethod("customFunction", customFunction, "That isn't valid.");

$('form').validate({
    rules: {
        fieldname: {
            customFunction: true
        }
    }
});

Edit: Actually, use the remote method that svinto mentions instead. I didn't even know that existed. I'm a fool.

jammus
+2  A: 

Yes, there is an example of this.

You basically rules: {FIELD: {remote: "URL"}}, then on validation a call is made to URL?FIELD=value and on the server you return true or false.

svinto