Skip to content

Added support for RegularExpression, MaxLength, MinLength and FileExtensions attributes #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion jQuery.Validation.Unobtrusive.Native/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static RouteValueDictionary GetUnobtrusiveValidationAttributes<TModel, TP
foreach (var rule in validators.SelectMany(v => v.GetClientValidationRules()))
{
// if we have an error message for this rule then create a data-msg-/rulename/ attribute to store it
if (!string.IsNullOrEmpty(rule.ErrorMessage) && rule.ValidationType != "length") // length is the only exception to this standard rule
if (!string.IsNullOrEmpty(rule.ErrorMessage) && rule.ValidationType != "length" && rule.ValidationType != "regex") // length & regex are exceptions to this mapping
attributes.AddIfNotPresent("data-msg-" + rule.ValidationType, rule.ErrorMessage);

switch (rule.ValidationType)
Expand All @@ -50,6 +50,8 @@ public static RouteValueDictionary GetUnobtrusiveValidationAttributes<TModel, TP
attributes.AddIfNotPresent("data-rule-equalto", equalToSelector);
break;

case "minlength":
case "maxlength":
case "length":
// length is mapped to minlength and maxlength
if (rule.ValidationParameters.ContainsKey("min"))
Expand All @@ -70,6 +72,17 @@ public static RouteValueDictionary GetUnobtrusiveValidationAttributes<TModel, TP
"[{0},{1}]", rule.ValidationParameters["min"], rule.ValidationParameters["max"]));
break;

case "extension":
// Use the older "extensions" validator in Additional-Methods, and only spit out a string of extensions.
attributes.AddIfNotPresent("data-rule-extension", rule.ValidationParameters["extension"]);
break;

case "regex":
// Additional-Methods provides support for "pattern" with the regex passed in as a string.
attributes.AddIfNotPresent("data-rule-pattern", rule.ValidationParameters["pattern"]);
attributes.AddIfNotPresent("data-msg-pattern", rule.ErrorMessage);
break;

// Standard handling of rules

default:
Expand Down
14 changes: 13 additions & 1 deletion jVUNDemo/Controllers/DemoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ActionResult MaxLengthMinLength()
{
ViewBag.Title = "MaxLength and MinLength";

return View(new MaxLengthMinLengthModel { MaxLength = "12345678", MaxAndMinLength = "123" });
return View(new MaxLengthMinLengthModel { StringLengthMaxOnly = "12345678", StringLengthMaxAndMinLength = "123", MaxLength = "1234567890", MinLength = "12345"});
}

public ActionResult EqualTo()
Expand Down Expand Up @@ -107,6 +107,18 @@ public ActionResult CreditCard()
return View(new CreditCardModel());
}

public ActionResult RegularExpression()
{
ViewBag.Title = "Regular Expression";

return View(new RegularExpressionModel { RegularExpression = "goodbye" });
}

public ActionResult FileExtensions()
{
ViewBag.Title = "File Extensions";

return View(new FileExtensionsModel());
}
}
}
11 changes: 11 additions & 0 deletions jVUNDemo/Models/FileExtensionsModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;

namespace jQuery.Validation.Unobtrusive.Native.Demos.Models
{
public class FileExtensionsModel
{
// Note that this won't validate a HttpPostedFileBase - see http://stackoverflow.com/a/13650754/33051 for an example that will.
[FileExtensions(Extensions = "txt,pdf")]
public string FileExtensions { get; set; }
}
}
10 changes: 8 additions & 2 deletions jVUNDemo/Models/MaxLengthMinLengthModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ namespace jQuery.Validation.Unobtrusive.Native.Demos.Models
public class MaxLengthMinLengthModel
{
[StringLength(7)]
public string MaxLength { get; set; }
public string StringLengthMaxOnly { get; set; }

[StringLength(10, MinimumLength = 5)]
public string MaxAndMinLength { get; set; }
public string StringLengthMaxAndMinLength { get; set; }

[MinLength(6)]
public string MinLength { get; set; }

[MaxLength(9)]
public string MaxLength { get; set; }
}
}
10 changes: 10 additions & 0 deletions jVUNDemo/Models/RegularExpressionModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;

namespace jQuery.Validation.Unobtrusive.Native.Demos.Models
{
public class RegularExpressionModel
{
[RegularExpression("^hello$")]
public string RegularExpression { get; set; }
}
}
Loading