mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Correcting formatting and handling of error message.
Corrected the formatting and correct the handling of the error message.
This commit is contained in:
@@ -5,10 +5,8 @@ using Orchard.DynamicForms.Services;
|
||||
using Orchard.DynamicForms.Services.Models;
|
||||
using Orchard.Localization;
|
||||
|
||||
namespace Orchard.DynamicForms.ValidationRules
|
||||
{
|
||||
public class UrlAddress : ValidationRule
|
||||
{
|
||||
namespace Orchard.DynamicForms.ValidationRules {
|
||||
public class UrlAddress : ValidationRule {
|
||||
public UrlAddress() {
|
||||
RegexOptions = RegexOptions.Singleline | RegexOptions.IgnoreCase;
|
||||
Pattern = @"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$";
|
||||
@@ -17,26 +15,22 @@ namespace Orchard.DynamicForms.ValidationRules
|
||||
public string Pattern { get; set; }
|
||||
public RegexOptions RegexOptions { get; set; }
|
||||
|
||||
public override void Validate(ValidateInputContext context)
|
||||
{
|
||||
if (!Regex.IsMatch(context.AttemptedValue, Pattern, RegexOptions))
|
||||
{
|
||||
public override void Validate(ValidateInputContext context) {
|
||||
if (!Regex.IsMatch(context.AttemptedValue, Pattern, RegexOptions)) {
|
||||
var message = GetValidationMessage(context);
|
||||
context.ModelState.AddModelError(context.FieldName, message.Text);
|
||||
}
|
||||
}
|
||||
|
||||
public override void RegisterClientAttributes(RegisterClientValidationAttributesContext context)
|
||||
{
|
||||
public override void RegisterClientAttributes(RegisterClientValidationAttributesContext context) {
|
||||
context.ClientAttributes["data-val-regex"] = GetValidationMessage(context).Text;
|
||||
context.ClientAttributes["data-val-regex-pattern"] = Pattern;
|
||||
}
|
||||
|
||||
private LocalizedString GetValidationMessage(ValidationContext context)
|
||||
{
|
||||
private LocalizedString GetValidationMessage(ValidationContext context) {
|
||||
return String.IsNullOrWhiteSpace(ErrorMessage)
|
||||
? T("{0} is not a valid URL.", context.FieldName)
|
||||
: new LocalizedString(Tokenize(ErrorMessage, context));
|
||||
: T(ErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user