public static void StringValidatorInstance()
        {
            //<Snippet14>

            ConfigurationValidatorBase valBase;
            StringValidatorAttribute   strValAttr =
                new StringValidatorAttribute();

            long   badValue  = 10;
            string goodValue = "10";

            try
            {
                valBase = strValAttr.ValidatorInstance;
                valBase.Validate(goodValue);
                // valBase.Validate(badValue);
            }
            catch (ArgumentException e)
            {
                // Display error message.
                string msg = e.ToString();
#if DEBUG
                Console.WriteLine(msg);
#endif
            }
            //</Snippet14>
        }
Example #2
0
        public void InvalidCharacters_GetAndSet()
        {
            StringValidatorAttribute attribute = new StringValidatorAttribute();

            attribute.InvalidCharacters = "_-";
            Assert.Equal("_-", attribute.InvalidCharacters);
        }
Example #3
0
        public void MinLength_SmallerThanMaxLength()
        {
            StringValidatorAttribute attribute = new StringValidatorAttribute();

            attribute.MaxLength = 10;
            Assert.Throws <ArgumentOutOfRangeException>(() => attribute.MinLength = 11);
        }
Example #4
0
        public void MaxLength_BiggerThanMinLength()
        {
            StringValidatorAttribute attribute = new StringValidatorAttribute();

            attribute.MinLength = 5;
            Assert.Throws <ArgumentOutOfRangeException>(() => attribute.MaxLength = 4);
        }
Example #5
0
        public void MinLength_GetAfterSet()
        {
            StringValidatorAttribute attribute = new StringValidatorAttribute();

            attribute.MinLength = 5;
            Assert.Equal(5, attribute.MinLength);
        }
Example #6
0
        public void MaxLength_GetAfterSet()
        {
            StringValidatorAttribute attribute = new StringValidatorAttribute();

            attribute.MaxLength = 10;
            Assert.Equal(10, attribute.MaxLength);
        }
        public void AttachValidators(StringValidatorAttribute validator, WebControl control)
        {
            control.Attributes.Add("minlength", validator.MinSize.ToString(CultureInfo.InvariantCulture));
            control.Attributes.Add("maxlength", validator.MaxSize.ToString(CultureInfo.InvariantCulture));

            if (validator.IsRequired)
            {
                control.Attributes.Add("required", "required");
            }

            control.Attributes.Add("data-error", "test message");
        }
        private void AttachValidators(StringValidatorAttribute validator, Object control)
        {
            FieldHelper.ApplyWebControlAttribute(control, "minlength", validator.MinSize.ToString(CultureInfo.InvariantCulture));
            FieldHelper.ApplyWebControlAttribute(control, "maxlength", validator.MaxSize.ToString(CultureInfo.InvariantCulture));

            if (validator.IsRequired)
            {
                FieldHelper.ApplyWebControlAttribute(control, "required", "required");
            }

            FieldHelper.ApplyWebControlAttribute(control, "data-error", "test message");
        }
 private bool IsValid(object value, bool allowEmpty, string expression)
 {
     try {
         StringValidatorAttribute validator =
             new StringValidatorAttribute();
         validator.AllowEmpty = allowEmpty;
         validator.Expression = expression;
         validator.Validate(value);
         return(true);
     } catch (ValidationException) {
         return(false);
     }
 }
        private static void ValidateStringFieldsAndProperties(User user, dynamic member, StringValidatorAttribute validator)
        {
            string value = member.GetValue(user);

            if (value.Length > validator.MaxLength)
            {
                throw new ValidationException(member.ToString());
            }
        }
Example #11
0
        public void MaxLengthNotSpecified_Get()
        {
            StringValidatorAttribute attribute = new StringValidatorAttribute();

            Assert.Equal(int.MaxValue, attribute.MaxLength);
        }
Example #12
0
        public void MinLengthNotSpecified_Get()
        {
            StringValidatorAttribute attribute = new StringValidatorAttribute();

            Assert.Equal(0, attribute.MinLength);
        }
Example #13
0
 private void AttachValidators(StringValidatorAttribute validator, object control)
 {
     FieldHelper.SetPropertyValue(control, "MaxLength", validator.MaxSize.ToString(CultureInfo.InvariantCulture));
 }