public override IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context)
        {
            var results = _innerValidator.Validate(context);

            if (results.Any())
            {
                if (string.IsNullOrWhiteSpace(_message))
                {
                    _onFailureSimple((T)context.Instance, context);
                }
                else
                {
                    var messageFormatter = new MessageFormatter();
                    messageFormatter.AppendPropertyName(context.PropertyName);
                    messageFormatter.AppendArgument("PropertyValue", context.PropertyValue);
                    try
                    {
                        messageFormatter.AppendAdditionalArguments(
                            _funcs.Select(func => func((T)context.Instance, (TProperty)context.PropertyValue)).ToArray());
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }

                    var msg = messageFormatter.BuildMessage(_message);
                    _onFailureComplex((T)context.Instance, context, msg);
                }
            }

            return(results);
        }
Пример #2
0
        public void Adds_PropertyName_to_message()
        {
            string result = formatter
                            .AppendPropertyName("foo")
                            .BuildMessage("{PropertyName}");

            result.ShouldEqual("foo");
        }
        public string ConstructMessage(string message, string propertyName, params string[] additionalPropertyNames)
        {
            if (additionalPropertyNames == null)
            {
                throw new ArgumentNullException("additionalPropertyNames");
            }
            var messageFormatter = new MessageFormatter();

            messageFormatter.AppendPropertyName(propertyName);
            messageFormatter.AppendAdditionalArguments(additionalPropertyNames);
            return(messageFormatter.BuildMessage(message));
        }
Пример #4
0
 static void FillValidation <TModel> (HtmlTag tag, PropertyRule r)
 {
     tag.Data("val", "true");
     foreach (var v in r.Validators)
     {
         var message          = v.ErrorMessageSource;
         var messageFormatter = new MessageFormatter();
         messageFormatter.AppendPropertyName(r.DisplayName.GetString());
         var formattedMessage = messageFormatter.BuildMessage(message.GetString());
         if (v is NotEmptyValidator)
         {
             tag.Data("val-required", formattedMessage);
         }
         if (v is EmailValidator)
         {
             tag.Data("val-email", formattedMessage);
         }
         if (v is RegularExpressionValidator)
         {
             tag.Data("val-regex", formattedMessage);
             tag.Data("val-regex-pattern", (v as RegularExpressionValidator).Expression);
         }
         if (v is LengthValidator)
         {
             var lv = v as LengthValidator;
             tag.Data("val-length", formattedMessage
                      .Replace("PropertyName", "0")
                      .Replace("MinLength", "1")
                      .Replace("MaxLength", "2")
                      .Replace("TotalLength", "3")
                      );
             tag.Data("val-length-max", lv.Max);
             tag.Data("val-length-min", lv.Min);
         }
         if (v is RemoteValidator)
         {
             var rv = v as RemoteValidator;
             tag.Data("val-remote", formattedMessage);
             tag.Data("val-remote-additionalfields", rv.AdditionalFields);
             tag.Data("val-remote-type", rv.HttpMethod);
             tag.Data("val-remote-url", rv.Action);
         }
     }
 }
Пример #5
0
 public static MessageFormatter AppendProperyName(this MessageFormatter formatter, string name)
 {
     return(formatter.AppendPropertyName(name));
 }