public void AddRegexData(IEnumerable<ValidationAttribute> propertyValidators, HtmlTag htmlTag, RequestData request)
 {
     var regex = propertyValidators.OfType<RegularExpressionAttribute>().FirstOrDefault();
     if (regex != null)
     {
         var msg = regex.ErrorMessage ?? string.Format("The field '{0}' did not match the regular expression '{1}'", request.Accessor.InnerProperty.Name, regex.Pattern);
         if (_msUnobtrusive)
             htmlTag.Data("val", true).Data("val-regex", msg).Data("val-regex-pattern", regex.Pattern);
         else
             htmlTag.Data("rule-regex", regex.Pattern).Data("msg-regex", msg);
     }
 }
示例#2
0
 /// <summary>
 /// Run the specified validator, tag and formattedMessage.
 /// </summary>
 /// <param name="validator">A validator.</param>
 /// <param name="tag">A tag.</param>
 /// <param name="formattedMessage">Formatted message.</param>
 public void Run(FluentValidation.Validators.IPropertyValidator validator, HtmlTag tag, string formattedMessage)
 {
     if (validator is CpfValidator)
     {
         tag.Data("val-cpf", formattedMessage);
     }
     else if (validator is ValidDate)
     {
         tag.Data("val-dateBR", formattedMessage);
     }
     else if (validator is ValidNomeConjuge)
     {
         tag.Data("val-nomeConjuge", formattedMessage);
     }
 }
示例#3
0
        public virtual void Modify(HtmlTag form)
        {
            if (!_modify) return;

            form.Data("validation-mode", _value.ToLower());
            form.AddClass("validated-form");
        }
 public void AddEqualToDataAttr(IEnumerable<ValidationAttribute> propertyValidators, HtmlTag htmlTag, RequestData request)
 {
     var equal = propertyValidators.OfType<CompareAttribute>().FirstOrDefault();
     if (equal != null)
     {
         var formatErrorMessage = equal.FormatErrorMessage(request.Accessor.Name);
         if (_msUnobtrusive)
         {
             htmlTag.Data("val", true);
             htmlTag.Data("val-equalto", formatErrorMessage);
             if (request.Accessor.PropertyNames.Length > 1)
             {
                 htmlTag.Data("val-equalto-other", request.Id.Replace("_" + request.Accessor.Name, "") + "_" + equal.OtherProperty);
             }
             else
             {
                 htmlTag.Data("val-equalto-other", "*." + equal.OtherProperty);
             }
         }
         else
         {
             htmlTag.Data("msg-equalto", formatErrorMessage);
             if (request.Accessor.PropertyNames.Length > 1)
                 htmlTag.Data("rule-equalto", "#" + request.Id.Replace("_" + request.Accessor.Name, "") + "_" + equal.OtherProperty);
             else
                 htmlTag.Data("rule-equalto", "#" + equal.OtherProperty);
         }
     }
 }
        public void AddEqualToDataAttr(IEnumerable<PropertyValidatorResult> propertyValidators, HtmlTag htmlTag, RequestData request)
        {
            var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is EqualValidator);
            if (result != null)
            {
                var equal = result.PropertyValidator.As<EqualValidator>();
                MessageFormatter formatter = new MessageFormatter()
                    .AppendPropertyName(result.DisplayName)
                    .AppendArgument("ComparisonValue", equal.ValueToCompare);

                string message = formatter.BuildMessage(equal.ErrorMessageSource.GetString());

                if (_msUnobtrusive)
                {
                    htmlTag.Data("val", true);
                    htmlTag.Data("val-equalto", message);
                    if (request.Accessor.PropertyNames.Length > 1)
                        htmlTag.Data("val-equalto-other", request.Id.Replace("_" + request.Accessor.Name, "") + "_" + equal.MemberToCompare.Name);
                    else
                        htmlTag.Data("val-equalto-other", "*." + equal.MemberToCompare.Name);
                }
                else
                {
                    htmlTag.Data("msg-equalto", message);
                    if (request.Accessor.PropertyNames.Length > 1)
                        htmlTag.Data("rule-equalto", "#" + request.Id.Replace("_" + request.Accessor.Name, "") + "_" + equal.MemberToCompare.Name);
                    else
                        htmlTag.Data("rule-equalto", "#" + equal.MemberToCompare.Name);
                }
            }
        }
 public void AddCreditCardClass(IEnumerable<PropertyValidatorResult> propertyValidators, HtmlTag htmlTag, RequestData requestData)
 {
     var lengthValidator = propertyValidators.Select(x => x.PropertyValidator).OfType<CreditCardValidator>().FirstOrDefault();
     if (lengthValidator != null)
     {
         if (requestData.ViewContext.UnobtrusiveJavaScriptEnabled)
         {
             if (!_msUnobtrusive)
             {
                 htmlTag.Data("rule-creditcard", true);
             }
         }
         else
         {
             htmlTag.AddClass("creditcard");
         }
     }
 }
 public void AddRequiredClass(IEnumerable<ValidationAttribute> propertyValidators, HtmlTag htmlTag, RequestData request)
 {
     var required = propertyValidators.OfType<RequiredAttribute>().FirstOrDefault();
     if (required != null)
     {
         if (request.ViewContext.UnobtrusiveJavaScriptEnabled)
         {
             var msg = required.ErrorMessage ?? string.Format("The field '{0}' is required", request.Accessor.InnerProperty.Name);
             if (_msUnobtrusive)
                 htmlTag.Data("val", true).Data("val-required", msg);
             else
                 htmlTag.Data("rule-required", true).Data("msg-required", msg);
         }
         else 
             htmlTag.AddClass("required");
     }
 }
 public void AddEmailData(IEnumerable<PropertyValidatorResult> propertyValidators, HtmlTag htmlTag, RequestData requestData)
 {
     var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is EmailValidator);
     if (result != null)
     {
         if (requestData.ViewContext.UnobtrusiveJavaScriptEnabled)
         {
             var msg = GetMessage(requestData, result) ?? string.Format("The value is not a valid email address");
             if (_msUnobtrusive)
                 htmlTag.Data("val", true).Data("val-email", msg);
             else
                 htmlTag.Data("rule-email", true).Data("msg-email", msg);
         }
         else
             htmlTag.AddClass("email");
     }
 }
        public void AddRequiredClass(IEnumerable<PropertyValidatorResult> propertyValidators, HtmlTag htmlTag, RequestData requestData)
        {
            var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is NotEmptyValidator
                                                             || x.PropertyValidator is NotNullValidator);

            if (result != null)
            {
                if (requestData.ViewContext.UnobtrusiveJavaScriptEnabled)
                {
                    if (_msUnobtrusive)
                        htmlTag.Data("val", true).Data("val-required", GetMessage(requestData, result) ?? string.Empty);
                    else
                        htmlTag.Data("rule-required", true).Data("msg-required", GetMessage(requestData, result) ?? string.Empty);
                }
                else
                    htmlTag.AddClass("required");
            }
        }
        public void AddRegexData(IEnumerable<PropertyValidatorResult> propertyValidators, HtmlTag htmlTag, RequestData requestData)
        {
            var result = propertyValidators.FirstOrDefault(x => x.PropertyValidator is RegularExpressionValidator);

            if (result != null && requestData.ViewContext.UnobtrusiveJavaScriptEnabled)
            {
                var regex = result.PropertyValidator.As<RegularExpressionValidator>();
                var msg = GetMessage(requestData, result) ?? string.Format("The value did not match the regular expression '{0}'", regex.Expression);
                if (_msUnobtrusive)
                    htmlTag.Data("val", true).Data("val-regex", msg).Data("val-regex-pattern", regex.Expression);
                else
                    htmlTag.Data("rule-regex", regex.Expression).Data("msg-regex", msg);
            }
        }