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 = new MessageFormatter()
                                .AppendPropertyName(Rule.GetDisplayName())
                                .AppendArgument("ComparisonValue", comparisonDisplayName);


                string message = formatter.BuildMessage(EqualValidator.ErrorMessageSource.GetString());
                yield return(new ModelClientValidationEqualToRule(message, CompareAttribute.FormatPropertyForClientValidation(propertyToCompare.Name)));
            }
        }
Example #2
0
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var propertyToCompare = ActiveAndStatusMatchValidator.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 = new MessageFormatter()
                                .AppendPropertyName(Rule.GetDisplayName())
                                .AppendArgument("PropertyNameTo", comparisonDisplayName);

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

                var rule = new ModelClientValidationRule
                {
                    ErrorMessage   = message,               // default error message
                    ValidationType = "activeandstatusmatch" // name of the validatoin which will be used inside unobtrusive library
                };

                rule.ValidationParameters["comparewith"] = propertyToCompare.Name; // html element which includes prefix information
                yield return(rule);
            }
        }
        public override void AddValidation(ClientModelValidationContext context)
        {
            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 messageTemplate;
                try {
                    messageTemplate = EqualValidator.ErrorMessageSource.GetString(null);
                }
                catch (FluentValidationMessageFormatException) {
                    messageTemplate = ValidatorOptions.LanguageManager.GetStringForValidator <EqualValidator>();
                }
                string message = formatter.BuildMessage(messageTemplate);
                MergeAttribute(context.Attributes, "data-val", "true");
                MergeAttribute(context.Attributes, "data-val-equalto", message);
                MergeAttribute(context.Attributes, "data-val-equalto-other", propertyToCompare.Name);
            }
        }
		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
			}
		}
Example #5
0
        public override void AddValidation(ClientModelValidationContext context)
        {
            var propertyToCompare = this.Validator.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(this.Validator.MemberToCompare.DeclaringType, propertyToCompare, null)
                    ?? propertyToCompare.Name.SplitPascalCase();

                var formatter = new MessageFormatter()
                                .AppendPropertyName(context.ModelMetadata.PropertyName)
                                .AppendArgument("ComparisonValue", comparisonDisplayName);


                string message = formatter.BuildMessage(this.Validator.Options.ErrorMessageSource.GetString(null));
                context.Attributes.AddIfNotExisting("data-val", "true");
                context.Attributes.AddIfNotExisting("data-val-equalto", message);
                context.Attributes.AddIfNotExisting("data-val-equalto-other", $"*.{propertyToCompare.Name}");
            }
        }
Example #6
0
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var propertyToCompare = EqualAfterValidator.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 = new MessageFormatter()
                                .AppendPropertyName(Rule.GetDisplayName())
                                .AppendArgument("ComparisonValue", comparisonDisplayName);


                string message = formatter.BuildMessage(EqualAfterValidator.ErrorMessageSource.GetString());
                var    rule    = new ModelClientValidationRule
                {
                    ErrorMessage   = message,       // default error message
                    ValidationType = "equaltoafter" // name of the validatoin which will be used inside unobtrusive library
                };
                rule.ValidationParameters["other"] = CompareAttribute.FormatPropertyForClientValidation(propertyToCompare.Name);
                yield return(rule);
            }
        }
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            var propertyToCompare = CheckBoxCheckValidator.MemberToCompare as PropertyInfo;

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

                var formatter = new MessageFormatter()
                                .AppendPropertyName(Rule.GetDisplayName())
                                .AppendArgument("PropertyNameTo", comparisonDisplayName);

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

                var rule = new ModelClientValidationRule
                {
                    ErrorMessage   = message,
                    ValidationType = "checkboxcheck"
                };

                rule.ValidationParameters["comparewith"] = propertyToCompare.Name;
                yield return(rule);
            }
        }
Example #8
0
        /// <summary>
        /// Creates a new property rule.
        /// </summary>
        /// <param name="member">Property</param>
        /// <param name="propertyFunc">Function to get the property value</param>
        /// <param name="expression">Lambda expression used to create the rule</param>
        /// <param name="cascadeModeThunk">Function to get the cascade mode.</param>
        /// <param name="typeToValidate">Type to validate</param>
        /// <param name="containerType">Container type that owns the property</param>
        public PropertyRule(MemberInfo member, Func <object, object> propertyFunc, LambdaExpression expression, Func <CascadeMode> cascadeModeThunk, Type typeToValidate, Type containerType)
        {
            Member                = member;
            PropertyFunc          = propertyFunc;
            Expression            = expression;
            OnFailure             = x => { };
            TypeToValidate        = typeToValidate;
            this.cascadeModeThunk = cascadeModeThunk;

            PropertyName = ValidatorOptions.PropertyNameResolver(containerType, member, expression);
            DisplayName  = new LazyStringSource(() => ValidatorOptions.DisplayNameResolver(containerType, member, expression));
        }
        /// <summary>
        /// Creates a new property rule.
        /// </summary>
        /// <param name="member">Property</param>
        /// <param name="propertyFunc">Function to get the property value</param>
        /// <param name="expression">Lambda expression used to create the rule</param>
        /// <param name="cascadeModeThunk">Function to get the cascade mode.</param>
        /// <param name="typeToValidate">Type to validate</param>
        /// <param name="containerType">Container type that owns the property</param>
        public PropertyRule(MemberInfo member, Func <object, object> propertyFunc, LambdaExpression expression, Func <CascadeMode> cascadeModeThunk, Type typeToValidate, Type containerType)
        {
            Member                = member;
            PropertyFunc          = propertyFunc;
            Expression            = expression;
            OnFailure             = x => { };
            TypeToValidate        = typeToValidate;
            this.cascadeModeThunk = cascadeModeThunk;

            PropertyName = ValidatorOptions.PropertyNameResolver(containerType, member, expression);
            string displayName = ValidatorOptions.DisplayNameResolver(containerType, member, expression);

            if (!string.IsNullOrEmpty(displayName))
            {
                DisplayName = new StaticStringSource(displayName);
            }
        }
Example #10
0
        /// <summary>
        /// Add client validation
        /// </summary>
        /// <param name="context"></param>
        protected override void AddClientValidation(ClientModelValidationContext context)
        {
            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 = new MessageFormatter()
                                .AppendPropertyName(Rule.GetDisplayName())
                                .AppendArgument("ComparisonValue", comparisonDisplayName);

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


                MergeClientAttribute(context, "data-val", "true");
                MergeClientAttribute(context, "data-val-equalto", message);
                MergeClientAttribute(context, "data-val-equalto-other", propertyToCompare.Name);
            }
        }