public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ErrorMessage = UmbracoValidationHelper.GetDictionaryItem(_errorMessageDictionaryKey);

            var error   = FormatErrorMessage(metadata.DisplayName);
            var rule    = new ModelClientValidationStringLengthRule(error, MinimumLength, MaximumLength);

            yield return rule;
        }
 public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
     ModelMetadata metadata,
     ControllerContext context)
 {
     var rule = new ModelClientValidationStringLengthRule(
         ErrorMessageString,
         MinimumLength,
         MaximumLength);
     return new[] { rule };
 }
예제 #3
0
        public static IValidator StringLength(int maxLength, int minLength = 0, string errorMessage = null)
        {
            if (minLength == 0)
            {
                errorMessage = String.Format(CultureInfo.CurrentCulture, DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_StringLength), maxLength);
            }
            else
            {
                errorMessage = DefaultIfEmpty(errorMessage, WebPageResources.ValidationDefault_StringLengthRange);
                errorMessage = String.Format(CultureInfo.CurrentCulture, errorMessage, minLength, maxLength);
            }
            var clientAttributes = new ModelClientValidationStringLengthRule(errorMessage, minLength, maxLength);

            // We don't care if the value is unsafe when checking the length of the request field passed to us.
            return new ValidationAttributeAdapter(new StringLengthAttribute(maxLength) { MinimumLength = minLength }, errorMessage, clientAttributes,
                                                  useUnvalidatedValues: true);
        }
        public void AttributeWithIClientValidatableGetsClientValidationRules() {
            // Arrange
            var expected = new ModelClientValidationStringLengthRule("Error", 1, 10);
            var context = new ControllerContext();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(string));
            var attribute = new Mock<ValidationAttribute> { CallBase = true };
            attribute.As<IClientValidatable>()
                     .Expect(cv => cv.GetClientValidationRules(metadata, context))
                     .Returns(new[] { expected })
                     .Verifiable();
            var validator = new DataAnnotations4ModelValidator(metadata, context, attribute.Object);

            // Act
            ModelClientValidationRule actual = validator.GetClientValidationRules().Single();

            // Assert
            attribute.Verify();
            Assert.AreSame(expected, actual);
        }