Correcting formatting and handling of error message.

Corrected the formatting and correct the handling of the error message.
This commit is contained in:
Jamie Phillips
2015-10-23 17:12:24 -04:00
parent 14c2ee7944
commit 4648a252b9

View File

@@ -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);
}
}
}