mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Adding token support custom validation messages.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user