Skip to content
AlexBar edited this page Jun 12, 2012 · 1 revision

Remote validation are available with MvcExtensions. There are a bunch of methods that help you configure rules for this type of validation.

Configuration via strong-typed methods:

// Additional fields will automatically parsed from controller action method signature. 
// Parameters names will be automatically capitalized, that is, 
// if a method has the following signature `SomeAction(string param1, string param2)`, 
// you will get parameters as `Param1` and `Param2` in javascript (`*.Param1,*.Param2`)
.Remote(c => c.For<SomeController>(a => a.SomeAction(null, null))); 
.Remote(c => c.For<SomeController>(a => a.SomeAction(null, null), "area"));

// explicitly-defined additional fields 
//(use these methods if you don't need automatically generated additional field names)
.Remote(c => c.For<SomeController>(a => a.SomeAction(null, null), 
                                   new[] { additionalField1Name, additionalField2Name }));
.Remote(c => c.For<SomeController>(a => a.SomeAction(null, null), "area", 
                                   new[] { additionalField1Name, additionalField2Name }));

// ...

// controller can look like as:
public class SomeController : Controller
{
    public JsonResult SomeAction(string param1, string param2)
    {
        if (param1 != param2)
            return Json(true, JsonRequestBehavior.AllowGet);
        return Json(false);
    } 
}
//

Simplified version when no any additional fields needed:

 .Remote(c => c.For<SomeController>(x => x.SomeSimpleAction));
 .Remote(c => c.For<SomeController>(x => x.SomeSimpleAction, "areaName"));

// ...
// controller can look like as:
public class SomeController : Controller
{
    public JsonResult SomeSimpleAction(string someParam)
    {
        if (someParam == "boo")
            return Json(true, JsonRequestBehavior.AllowGet);
        return Json(false);
    } 
}
//

Route-based configuration:

// pass any route name to use this methods
.Remote(c => c.For("routeName"));
.Remote(c => c.For("routeName", new[] { "additionalField1Name", "additionalField2Name" }));

Configuration via string-based methods:

.Remote(c => c.For("controllerName", "actionName"));
.Remote(c => c.For("controllerName", "actionName", "areaName"));
.Remote(c => c.For("controllerName", "actionName", new[]{ "additionalFieldName" }));
.Remote(c => c.For("controllerName", "actionName", new[] { "additionalField1Name", "additionalField2Name" }));
.Remote(c => c.For("controllerName", "actionName", "areaName", new[] { "additionalFieldName" }));

To set http method you can use .HttpMethod() method:

.Remote(c => c.HttpMethod("POST").For(x => x.CheckUsername1));

Validation messages can be set as usual for MvcExtensios. For example:

.Remote(c => c.For(x => x.CheckUsername1), () => Resources.SomeErrorMessage);
.Remote(c => c.For(x => x.CheckUsername1), "Some error message");
...

Example:

public class AuthController : Controller
{
    public JsonResult CheckUsername(string username)
    {
        if (IsUserNameValid(username))
            return Json(true, JsonRequestBehavior.AllowGet);
        return Json(false);
    } 
}

Model metadata for remote validation:

public RegisterUserMetadata : MvcExtensions.ModelMetadataConfiguration<RegisterUser>
{
    public RegisterUserMetadata()
    {
       // ...
       Configure(x => x.Username)
          .Remote(c => c.For<AuthController>(a => a.CheckUsername), () => ResMsg.UsernameNotAvailable);
    }
}