private string GetErrorMessage(ClientModelValidationContext context)
        {
            var    formatter = ValidatorOptions.MessageFormatterFactory().AppendPropertyName(Rule.GetDisplayName());
            string messageTemplate;

            try {
                messageTemplate = Validator.Options.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                messageTemplate = ValidatorOptions.LanguageManager.GetStringForValidator <NotEmptyValidator>();
            }
            var message = formatter.BuildMessage(messageTemplate);

            return(message);
        }
예제 #2
0
        public override void AddValidation(ClientModelValidationContext context)
        {
            var formatter = ValidatorOptions.MessageFormatterFactory().AppendPropertyName(Rule.GetDisplayName());

            string messageTemplate;

            try {
                messageTemplate = EmailValidator.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                messageTemplate = ValidatorOptions.LanguageManager.GetStringForValidator <EmailValidator>();
            }

            string message = formatter.BuildMessage(messageTemplate);

            MergeAttribute(context.Attributes, "data-val", "true");
            MergeAttribute(context.Attributes, "data-val-email", message);
        }
        private string GetErrorMessage(ClientModelValidationContext context)
        {
            var formatter = ValidatorOptions.MessageFormatterFactory()
                            .AppendPropertyName(Rule.GetDisplayName())
                            .AppendArgument("ComparisonValue", RangeValidator.ValueToCompare);

            string message;

            try {
                message = RangeValidator.Options.ErrorMessageSource.GetString(null);
            } catch (FluentValidationMessageFormatException) {
                message = ValidatorOptions.LanguageManager.GetStringForValidator <GreaterThanOrEqualValidator>();
            }

            message = formatter.BuildMessage(message);

            return(message);
        }
예제 #4
0
        private string GetErrorMessage(LengthValidator lengthVal, ClientModelValidationContext context)
        {
            var formatter = ValidatorOptions.MessageFormatterFactory()
                            .AppendPropertyName(Rule.GetDisplayName())
                            .AppendArgument("MinLength", lengthVal.Min)
                            .AppendArgument("MaxLength", lengthVal.Max);

            bool needsSimplifiedMessage = lengthVal.Options.ErrorMessageSource is LanguageStringSource;

            string message;

            try {
                message = lengthVal.Options.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                if (lengthVal is ExactLengthValidator)
                {
                    message = ValidatorOptions.LanguageManager.GetString("ExactLength_Simple");
                }
                else
                {
                    message = ValidatorOptions.LanguageManager.GetString("Length_Simple");
                }

                needsSimplifiedMessage = false;
            }

            if (needsSimplifiedMessage && message.Contains("{TotalLength}"))
            {
                if (lengthVal is ExactLengthValidator)
                {
                    message = ValidatorOptions.LanguageManager.GetString("ExactLength_Simple");
                }
                else
                {
                    message = ValidatorOptions.LanguageManager.GetString("Length_Simple");
                }
            }

            message = formatter.BuildMessage(message);
            return(message);
        }
예제 #5
0
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var    formatter = ValidatorOptions.MessageFormatterFactory().AppendPropertyName(Rule.GetDisplayName());
            string message;

            try {
                message = Validator.Options.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                // User provided a message that contains placeholders based on object properties. We can't use that here, so just fall back to the default.
                message = ValidatorOptions.LanguageManager.GetStringForValidator <NotEmptyValidator>();
            }
            message = formatter.BuildMessage(message);
            yield return(new ModelClientValidationRequiredRule(message));
        }
예제 #6
0
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var formatter = ValidatorOptions.MessageFormatterFactory()
                            .AppendPropertyName(Rule.GetDisplayName())
                            .AppendArgument("ComparisonValue", AbstractComparisonValidator.ValueToCompare);
            string message;

            try {
                message = AbstractComparisonValidator.Options.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                message = GetDefaultMessage();
            }
            message = formatter.BuildMessage(message);
            yield return(new ModelClientValidationRangeRule(message, MinValue, MaxValue));
        }
        private string GetErrorMessage(LengthValidator lengthVal, ClientModelValidationContext context)
        {
            var formatter = ValidatorOptions.MessageFormatterFactory()
                            .AppendPropertyName(Rule.GetDisplayName())
                            .AppendArgument("MinLength", lengthVal.Min)
                            .AppendArgument("MaxLength", lengthVal.Max);

            bool messageNeedsSplitting = lengthVal.ErrorMessageSource.ResourceType == typeof(LanguageManager);

            string message;

            try {
                message = lengthVal.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                if (lengthVal is ExactLengthValidator)
                {
                    message = ValidatorOptions.LanguageManager.GetStringForValidator <ExactLengthValidator>();
                }
                else
                {
                    message = ValidatorOptions.LanguageManager.GetStringForValidator <LengthValidator>();
                }

                messageNeedsSplitting = true;
            }

            if (messageNeedsSplitting && message.Contains("{TotalLength}") && message.Contains("."))
            {
                // If we're using the default resources then the mesage for length errors will have two parts, eg:
                // '{PropertyName}' must be between {MinLength} and {MaxLength} characters. You entered {TotalLength} characters.
                // We can't include the "TotalLength" part of the message because this information isn't available at the time the message is constructed.
                // Instead, we'll just strip this off by finding the index of the period that separates the two parts of the message.

                message = message.Substring(0, message.IndexOf(".") + 1);
            }

            message = formatter.BuildMessage(message);
            return(message);
        }
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var formatter = ValidatorOptions.MessageFormatterFactory()
                            .AppendPropertyName(Rule.GetDisplayName())
                            .AppendArgument("From", RangeValidator.From)
                            .AppendArgument("To", RangeValidator.To);

            var messageNeedsSplitting = RangeValidator.Options.ErrorMessageSource is LanguageStringSource;

            string message;

            try {
                message = RangeValidator.Options.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                // Use provided a message that contains placeholders based on object properties. We can't use that here, so just fall back to the default.
                message = ValidatorOptions.LanguageManager.GetStringForValidator <InclusiveBetweenValidator>();
                messageNeedsSplitting = true;
            }

            if (messageNeedsSplitting && message.Contains(".") && message.Contains("{Value}"))
            {
                // If we're using the default resources then the mesage for length errors will have two parts, eg:
                // '{PropertyName}' must be between {From} and {To}. You entered {Value}.
                // We can't include the "Value" part of the message because this information isn't available at the time the message is constructed.
                // Instead, we'll just strip this off by finding the index of the period that separates the two parts of the message.

                message = message.Substring(0, message.IndexOf(".") + 1);
            }

            message = formatter.BuildMessage(message);

            yield return(new ModelClientValidationRangeRule(message, RangeValidator.From, RangeValidator.To));
        }
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var propertyToCompare = EqualValidator.MemberToCompare as PropertyInfo;

            if (propertyToCompare != null)
            {
                // If propertyToCompare is not null then we're comparing to another property.
                // If propertyToCompare is null then we're either comparing against a literal value, a field or a method call.
                // We only care about property comparisons in this case.

                var comparisonDisplayName =
                    ValidatorOptions.DisplayNameResolver(Rule.TypeToValidate, propertyToCompare, null)
                    ?? propertyToCompare.Name.SplitPascalCase();

                var formatter = ValidatorOptions.MessageFormatterFactory()
                                .AppendPropertyName(Rule.GetDisplayName())
                                .AppendArgument("ComparisonValue", comparisonDisplayName);


                string message;
                try {
                    message = EqualValidator.Options.ErrorMessageSource.GetString(null);
                }
                catch (FluentValidationMessageFormatException) {
                    // User provided a message that contains placeholders based on object properties. We can't use that here, so just fall back to the default.
                    message = ValidatorOptions.LanguageManager.GetStringForValidator <EqualValidator>();
                }
                message = formatter.BuildMessage(message);
#pragma warning disable 618
                yield return(new ModelClientValidationEqualToRule(message, CompareAttribute.FormatPropertyForClientValidation(propertyToCompare.Name)));

#pragma warning restore 618
            }
        }
예제 #10
0
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var    formatter = ValidatorOptions.MessageFormatterFactory().AppendPropertyName(Rule.GetDisplayName());
            string message;

            try {
                message = Validator.Options.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                message = ValidatorOptions.LanguageManager.GetStringForValidator <CreditCardValidator>();
            }
            message = formatter.BuildMessage(message);


            yield return(new ModelClientValidationRule {
                ValidationType = "creditcard",
                ErrorMessage = message
            });
        }
예제 #11
0
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            // Don't generate clientside rules if min/max are lazily loaded.
            var lengthVal = LengthValidator as LengthValidator;

            if (lengthVal != null && lengthVal.MaxFunc != null && lengthVal.MinFunc != null)
            {
                yield break;
            }

            var formatter = ValidatorOptions.MessageFormatterFactory()
                            .AppendPropertyName(Rule.GetDisplayName())
                            .AppendArgument("MinLength", LengthValidator.Min)
                            .AppendArgument("MaxLength", LengthValidator.Max);

            var    messageNeedsSplitting = LengthValidator.ErrorMessageSource.ResourceType == typeof(LanguageManager);
            string message;

            try {
                message = LengthValidator.ErrorMessageSource.GetString(null);
            }
            catch (FluentValidationMessageFormatException) {
                // Use provided a message that contains placeholders based on object properties. We can't use that here, so just fall back to the default.
                if (lengthVal is MinimumLengthValidator)
                {
                    message = ValidatorOptions.LanguageManager.GetStringForValidator <MinimumLengthValidator>();
                }
                else if (lengthVal is MaximumLengthValidator)
                {
                    message = ValidatorOptions.LanguageManager.GetStringForValidator <MaximumLengthValidator>();
                }
                else if (lengthVal is ExactLengthValidator)
                {
                    message = ValidatorOptions.LanguageManager.GetStringForValidator <ExactLengthValidator>();
                }
                else
                {
                    message = ValidatorOptions.LanguageManager.GetStringForValidator <LengthValidator>();
                }
                messageNeedsSplitting = true;
            }

            if (messageNeedsSplitting)
            {
                // If we're using the default resources then the mesage for length errors will have two parts, eg:
                // '{PropertyName}' must be between {MinLength} and {MaxLength} characters. You entered {TotalLength} characters.
                // We can't include the "TotalLength" part of the message because this information isn't available at the time the message is constructed.
                // Instead, we'll just strip this off by finding the index of the period that separates the two parts of the message.

                message = message.Substring(0, message.IndexOf(".") + 1);
            }

            message = formatter.BuildMessage(message);

            ModelClientValidationRule rule;

            if (lengthVal is MinimumLengthValidator)
            {
                rule = new ModelClientValidationMinLengthRule(message, LengthValidator.Min);
            }
            else if (lengthVal is MaximumLengthValidator)
            {
                rule = new ModelClientValidationMaxLengthRule(message, LengthValidator.Max);
            }
            else
            {
                rule = new ModelClientValidationStringLengthRule(message, LengthValidator.Min, LengthValidator.Max);
            }
            yield return(rule);
        }