From e83d93bf283b5389e4fdbc54f66fd38c4693a5c1 Mon Sep 17 00:00:00 2001 From: Nathan Heskew Date: Wed, 8 Dec 2010 13:24:00 -0800 Subject: [PATCH] Localizing validation error messages previously set in validation attributes. work item: 16850 --HG-- branch : dev --- .../CommentValidationAttributes.cs | 39 ++++++++ .../Orchard.Comments/Orchard.Comments.csproj | 1 + .../ViewModels/CommentsCreateViewModel.cs | 13 +-- .../Annotations/SetupValidationAttributes.cs | 88 +++++++++++++++++++ .../Orchard.Setup/Orchard.Setup.csproj | 1 + .../ViewModels/SetupViewModel.cs | 9 +- 6 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.Comments/Annotations/CommentValidationAttributes.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Setup/Annotations/SetupValidationAttributes.cs diff --git a/src/Orchard.Web/Modules/Orchard.Comments/Annotations/CommentValidationAttributes.cs b/src/Orchard.Web/Modules/Orchard.Comments/Annotations/CommentValidationAttributes.cs new file mode 100644 index 000000000..551afb931 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Comments/Annotations/CommentValidationAttributes.cs @@ -0,0 +1,39 @@ +using Orchard.Localization; + +namespace Orchard.Comments.Annotations { + public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute { + public RequiredAttribute() { + T = NullLocalizer.Instance; + } + + public Localizer T { get; set; } + + public override string FormatErrorMessage(string name) { + return T("You must provide a {0} in order to comment.", name).Text; + } + } + + public class CommentRequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute { + public CommentRequiredAttribute() { + T = NullLocalizer.Instance; + } + + public Localizer T { get; set; } + + public override string FormatErrorMessage(string name) { + return T("You must provide a Comment.", name).Text; + } + } + + public class RegularExpressionAttribute : System.ComponentModel.DataAnnotations.RegularExpressionAttribute { + public RegularExpressionAttribute(string pattern) : base(pattern) { + T = NullLocalizer.Instance; + } + + public Localizer T { get; set; } + + public override string FormatErrorMessage(string name) { + return T("The {0} is not valid.", name).Text; + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Comments/Orchard.Comments.csproj b/src/Orchard.Web/Modules/Orchard.Comments/Orchard.Comments.csproj index 5d05b1683..1e8557652 100644 --- a/src/Orchard.Web/Modules/Orchard.Comments/Orchard.Comments.csproj +++ b/src/Orchard.Web/Modules/Orchard.Comments/Orchard.Comments.csproj @@ -65,6 +65,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.Comments/ViewModels/CommentsCreateViewModel.cs b/src/Orchard.Web/Modules/Orchard.Comments/ViewModels/CommentsCreateViewModel.cs index db146acb7..51fb81d4c 100644 --- a/src/Orchard.Web/Modules/Orchard.Comments/ViewModels/CommentsCreateViewModel.cs +++ b/src/Orchard.Web/Modules/Orchard.Comments/ViewModels/CommentsCreateViewModel.cs @@ -1,22 +1,23 @@ using System.ComponentModel.DataAnnotations; +using Orchard.Comments.Annotations; namespace Orchard.Comments.ViewModels { public class CommentsCreateViewModel { - [Required(ErrorMessage="You must provide a Name in order to comment")] + [Annotations.Required] [StringLength(255)] public string Name { get; set; } - [RegularExpression(@"^[\w-]+@([\w-]+\.)+[\w]{2,4}$", ErrorMessage = "The Email is not valid")] + [Annotations.RegularExpression(@"^[\w-]+@([\w-]+\.)+[\w]{2,4}$")] [StringLength(255)] public string Email { get; set; } [StringLength(245)] - [RegularExpression(@"^(http(s)?://)?([\w-]+\.)+[\S]+$", ErrorMessage = "The Url is not valid")] + [Annotations.RegularExpression(@"^(http(s)?://)?([\w-]+\.)+[\S]+$")] public string SiteName { get; set; } - - [Required(ErrorMessage = "You must provide a Comment")] + + [CommentRequired] public string CommentText { get; set; } public int CommentedOn { get; set; } } -} +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Setup/Annotations/SetupValidationAttributes.cs b/src/Orchard.Web/Modules/Orchard.Setup/Annotations/SetupValidationAttributes.cs new file mode 100644 index 000000000..3f91ed9de --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Setup/Annotations/SetupValidationAttributes.cs @@ -0,0 +1,88 @@ +using Orchard.Localization; + +namespace Orchard.Setup.Annotations { + public class SiteNameValidAttribute : System.ComponentModel.DataAnnotations.RangeAttribute { + private string _value; + + public SiteNameValidAttribute(int maximumLength) + : base(1, maximumLength) { + T = NullLocalizer.Instance; + } + + public Localizer T { get; set; } + + public override bool IsValid(object value) { + _value = (value as string) ?? ""; + return base.IsValid(_value.Trim().Length); + } + + public override string FormatErrorMessage(string name) { + if (string.IsNullOrWhiteSpace(_value)) + return T("Site name is required.").Text; + + return T("Site name can be no longer than {0} characters.", Maximum).Text; + } + } + + public class UserNameValidAttribute : System.ComponentModel.DataAnnotations.RangeAttribute { + private string _value; + + public UserNameValidAttribute(int minimumLength, int maximumLength) + : base(minimumLength, maximumLength) { + T = NullLocalizer.Instance; + } + + public Localizer T { get; set; } + + public override bool IsValid(object value) { + _value = (value as string) ?? ""; + return base.IsValid(_value.Trim().Length); + } + + public override string FormatErrorMessage(string name) { + if (string.IsNullOrEmpty(_value)) + return T("User name is required.").Text; + + return _value.Length < (int)Minimum + ? T("User name must be longer than {0} characters.", Minimum).Text + : T("User name can be no longer than {0} characters.", Maximum).Text; + } + } + + public class PasswordValidAttribute : System.ComponentModel.DataAnnotations.RangeAttribute { + private string _value; + + public PasswordValidAttribute(int minimumLength, int maximumLength) + : base(minimumLength, maximumLength) { + T = NullLocalizer.Instance; + } + + public Localizer T { get; set; } + + public override bool IsValid(object value) { + _value = (value as string) ?? ""; + return base.IsValid(_value.Trim().Length); + } + + public override string FormatErrorMessage(string name) { + if (string.IsNullOrEmpty(_value)) + return T("Password is required.").Text; + + return _value.Length < (int)Minimum + ? T("Password must be longer than {0} characters.", Minimum).Text + : T("Password can be no longer than {0} characters.", Maximum).Text; + } + } + + public class PasswordConfirmationRequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute { + public PasswordConfirmationRequiredAttribute() { + T = NullLocalizer.Instance; + } + + public Localizer T { get; set; } + + public override string FormatErrorMessage(string name) { + return T("Password confirmation is required.").Text; + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Setup/Orchard.Setup.csproj b/src/Orchard.Web/Modules/Orchard.Setup/Orchard.Setup.csproj index 9a3f09c68..12269923f 100644 --- a/src/Orchard.Web/Modules/Orchard.Setup/Orchard.Setup.csproj +++ b/src/Orchard.Web/Modules/Orchard.Setup/Orchard.Setup.csproj @@ -61,6 +61,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.Setup/ViewModels/SetupViewModel.cs b/src/Orchard.Web/Modules/Orchard.Setup/ViewModels/SetupViewModel.cs index 44bd91ce8..6bd99e684 100644 --- a/src/Orchard.Web/Modules/Orchard.Setup/ViewModels/SetupViewModel.cs +++ b/src/Orchard.Web/Modules/Orchard.Setup/ViewModels/SetupViewModel.cs @@ -1,4 +1,3 @@ -using System.ComponentModel.DataAnnotations; using Orchard.Setup.Annotations; namespace Orchard.Setup.ViewModels { @@ -7,13 +6,13 @@ namespace Orchard.Setup.ViewModels { DatabaseOptions = true; } - [Required(ErrorMessage = "Site name is required."), StringLength(70, ErrorMessage = "Site name can be no longer than 70 characters.")] + [SiteNameValid(maximumLength: 70)] public string SiteName { get; set; } - [Required(ErrorMessage = "User name is required."), StringLengthMin(3, ErrorMessage = "User name must be longer than 3 characters."), StringLength(25, ErrorMessage = "User name can be no longer than 25 characters.")] + [UserNameValid(minimumLength: 3, maximumLength: 25)] public string AdminUsername { get; set; } - [Required(ErrorMessage = "Password is required."), StringLengthMin(6, ErrorMessage = "Password must be longer than 6 characters."), StringLength(50, ErrorMessage = "Password can be no longer than 50 characters.")] + [PasswordValid(minimumLength: 6, maximumLength: 50)] public string AdminPassword { get; set; } - [Required(ErrorMessage = "Password confirmation is required.")] + [PasswordConfirmationRequired] public string ConfirmPassword { get; set; } public bool DatabaseOptions { get; set; } [SqlDatabaseConnectionString]