Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created February 13, 2013 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/4945077 to your computer and use it in GitHub Desktop.
Save einarwh/4945077 to your computer and use it in GitHub Desktop.
Implementation of IClientValidatable.GetClientValidationRules to enable client-side validation in Mkay.
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
ModelMetadata metadata, ControllerContext context)
{
var propName = metadata.PropertyName;
var builder = new JsonBuilder(propName);
var visitor = new ExpVisitor<JObject>(builder);
Tree.Accept(visitor);
var ast = visitor.GetResult();
var json = new JObject(
new JProperty("rule", _ruleSource),
new JProperty("property", propName),
new JProperty("ast", ast));
var rule = new ModelClientValidationRule
{
ErrorMessage = ErrorMessage ?? _defaultErrorMessage,
ValidationType = "mkay"
};
rule.ValidationParameters["rule"] = json.ToString();
yield return rule;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment