Adding token support custom validation messages.

This commit is contained in:
Sipke Schoorstra
2014-10-30 13:59:00 -07:00
parent 17e42b8f0b
commit 509dbf6bf7
8 changed files with 25 additions and 9 deletions

View File

@@ -1,9 +1,16 @@
using Orchard.DynamicForms.Services.Models;
using Orchard.Localization;
using Orchard.Tokens;
namespace Orchard.DynamicForms.Services {
public abstract class ValidationRule : Component, IValidationRule {
public string ErrorMessage { get; set; }
public abstract void Validate(ValidateInputContext context);
public virtual void RegisterClientAttributes(RegisterClientValidationAttributesContext context) { }
public ITokenizer Tokenizer { get; set; }
protected string Tokenize(string errorMessage, ValidationContext context) {
return Tokenizer.Replace(errorMessage, null);
}
}
}

View File

@@ -1,7 +1,13 @@
using System;
using Orchard.Tokens;
namespace Orchard.DynamicForms.Services {
public class ValidationRuleFactory : Component, IValidationRuleFactory {
private readonly ITokenizer _tokenizer;
public ValidationRuleFactory(ITokenizer tokenizer) {
_tokenizer = tokenizer;
}
public TRule Create<TRule>(Action<TRule> setup = null) where TRule : ValidationRule, new() {
return Create(errorMessage: null, setup: setup);
}
@@ -10,7 +16,8 @@ namespace Orchard.DynamicForms.Services {
var rule = new TRule {
T = T,
Logger = Logger,
ErrorMessage = errorMessage
ErrorMessage = errorMessage,
Tokenizer = _tokenizer
};
if (setup != null)

View File

@@ -22,7 +22,7 @@ namespace Orchard.DynamicForms.ValidationRules {
}
private LocalizedString GetValidationMessage(ValidationContext context) {
return T(ErrorMessage.WithDefault("{0} must match the value of {1}."), context.FieldName, TargetName);
return T(Tokenize(ErrorMessage.WithDefault(String.Format("{0} must match the value of {1}.", context.FieldName, TargetName)), context));
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
using Orchard.DynamicForms.Helpers;
using Orchard.DynamicForms.Services;
using Orchard.DynamicForms.Services.Models;
@@ -27,7 +28,7 @@ namespace Orchard.DynamicForms.ValidationRules {
}
private LocalizedString GetValidationMessage(ValidationContext context) {
return T(ErrorMessage.WithDefault("{0} is not a valid email address."), context.FieldName, Pattern);
return T(Tokenize(ErrorMessage.WithDefault(String.Format("{0} is not a valid email address.", context.FieldName)), context));
}
}
}

View File

@@ -18,7 +18,7 @@ namespace Orchard.DynamicForms.ValidationRules {
}
private LocalizedString GetValidationMessage(ValidationContext context) {
return T(ErrorMessage.WithDefault("{0} is a mandatory field."), context.FieldName);
return T(Tokenize(ErrorMessage.WithDefault(String.Format("{0} is a mandatory field.", context.FieldName)), context));
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
using Orchard.DynamicForms.Helpers;
using Orchard.DynamicForms.Services;
using Orchard.DynamicForms.Services.Models;
@@ -26,7 +27,7 @@ namespace Orchard.DynamicForms.ValidationRules {
}
private LocalizedString GetValidationMessage(ValidationContext context) {
return T(ErrorMessage.WithDefault("{0} must match the following pattern: {1}."), context.FieldName, Pattern);
return T(Tokenize(ErrorMessage.WithDefault(String.Format("{0} must match the following pattern: {1}.", context.FieldName, Pattern)), context));
}
}
}

View File

@@ -18,7 +18,7 @@ namespace Orchard.DynamicForms.ValidationRules {
}
private LocalizedString GetValidationMessage(ValidationContext context) {
return T(ErrorMessage.WithDefault("{0} is a required field."), context.FieldName);
return T(Tokenize(ErrorMessage.WithDefault(String.Format("{0} is a required field.", context.FieldName)), context));
}
}
}

View File

@@ -41,7 +41,7 @@ namespace Orchard.DynamicForms.ValidationRules {
private LocalizedString GetValidationMessage(ValidationContext context) {
if (!String.IsNullOrWhiteSpace(ErrorMessage))
return T(ErrorMessage, context.FieldName, Minimum, Maximum);
return T(Tokenize(String.Format(ErrorMessage, context.FieldName, Minimum, Maximum), context));
if(Minimum != null && Maximum != null)
return T("{0} must be between {1} and {2} characters long.", context.FieldName, Minimum, Maximum);