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)));
            }
        }
예제 #2
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 = 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
			}
		}
예제 #4
0
        public void GuardClauses()
        {
            //Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { new CompareAttribute(null); }, "otherProperty");

            Assert.ThrowsArgumentNullOrEmpty(
                delegate { CompareAttribute.FormatPropertyForClientValidation(null); }, "property");
        }
예제 #5
0
        public void GuardClauses()
        {
            //Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                new CompareAttribute(null);
            }, "otherProperty");

            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                delegate {
                CompareAttribute.FormatPropertyForClientValidation(null);
            }, "property");
        }
 /// <summary>
 /// Map rules for the default attributes.
 /// </summary>
 protected virtual void MapDefaultRules()
 {
     AddDelegateRule <RangeAttribute>((attribute, errMsg) =>
     {
         var attr = (RangeAttribute)attribute;
         return(new[]
         {
             new ModelClientValidationRangeRule(errMsg,
                                                attr.Minimum,
                                                attr.Maximum)
         });
     });
     AddDelegateRule <RegularExpressionAttribute>((attribute, errMsg) =>
     {
         var attr = (RegularExpressionAttribute)attribute;
         return(new[]
         {
             new ModelClientValidationRegexRule(
                 errMsg, attr.Pattern)
         });
     });
     AddDelegateRule <RequiredAttribute>((attribute, errMsg) =>
     {
         var attr = (RequiredAttribute)attribute;
         return(new[]
         {
             new ModelClientValidationRequiredRule(errMsg)
         });
     });
     AddDelegateRule <StringLengthAttribute>((attribute, errMsg) =>
     {
         var attr = (StringLengthAttribute)attribute;
         return(new[]
         {
             new ModelClientValidationStringLengthRule(
                 errMsg, attr.MinimumLength,
                 attr.MaximumLength)
         });
     });
     AddDelegateRule <CompareAttribute>((attribute, errMsg) =>
     {
         var attr = (CompareAttribute)attribute;
         return(new[]
         {
             new ModelClientValidationEqualToRule(errMsg, CompareAttribute.FormatPropertyForClientValidation(attr.OtherProperty))
         });
     });
 }
        /// <summary>
        /// When implemented in a derived class, returns metadata for client validation.
        /// </summary>
        /// <returns>
        /// The metadata for client validation.
        /// </returns>
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            ValidatorProperties validatorProperties       = ValidationRule.Validator.GetValidatorProperties();
            MemberInfo          memberToCompareMemberInfo = validatorProperties.GetPropertyValue <MemberInfo>(Constants.ValidationMessageParameterNames.MEMBER_TO_COMPARE_MEMBER_INFO);
            PropertyInfo        propertyInfoToCompare     = memberToCompareMemberInfo as PropertyInfo;

            if (propertyInfoToCompare != null)
            {
                string message   = ValidationRule.GetValidationMessage(Metadata.Model);
                Type   ownerType = validatorProperties.GetPropertyValue <Type>(Constants.ValidationMessageParameterNames.OWNER_TYPE);

                string validationModelPropertyName = propertyInfoToCompare.Name;
                string propertyName = GetTransformedPropertyName(validationModelPropertyName, ownerType) ?? validationModelPropertyName;

                string propertyForClientValidation = CompareAttribute.FormatPropertyForClientValidation(propertyName);
                yield return(new ModelClientValidationEqualToRule(message, propertyForClientValidation));
            }
        }
    public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
    {
        if (!this.ShouldGenerateClientSideRules())
        {
            yield break;
        }
        var validator    = Validator as LessThanOrEqualValidator;
        var errorMessage = new MessageFormatter()
                           .AppendPropertyName(this.Rule.GetDisplayName())
                           .BuildMessage(validator.ErrorMessageSource.GetString());
        var rule = new ModelClientValidationRule
        {
            ErrorMessage   = errorMessage,
            ValidationType = "lessthanorequaldate"
        };

        rule.ValidationParameters["other"] = CompareAttribute.FormatPropertyForClientValidation(validator.MemberToCompare.Name);
        yield return(rule);
    }
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            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 formatter = new MessageFormatter()
                                .AppendPropertyName(Rule.PropertyDescription)
                                .AppendArgument("PropertyValue", propertyToCompare.Name);


                string message = formatter.BuildMessage(EqualValidator.ErrorMessageSource.GetString());
                yield return(new ModelClientValidationEqualToRule(message, CompareAttribute.FormatPropertyForClientValidation(propertyToCompare.Name)));
            }
        }
예제 #10
0
        public void FormatPropertyForClientValidationPrependsStarDot()
        {
            string prepended = CompareAttribute.FormatPropertyForClientValidation("test");

            Assert.AreEqual(prepended, "*.test");
        }