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);
        }
Exemple #2
0
        public string GetFormattedErrorMessage(Type type, MessageFormatter formatter)
        {
            string error = CustomError ?? ValidationMessageAttribute.GetMessage(type);

            if (customFormatArgs != null)
            {
                formatter.AppendAdditionalArguments(
                    customFormatArgs.Select(func => func(Instance)).ToArray()
                    );
            }
            return(formatter.BuildMessage(error));
        }
        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));
        }