protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var finalValue = validationContext.ObjectType.GetProperty(fieldName).GetValue(validationContext.ObjectInstance, null).ToString();

            if (finalValue != valueToCompare || (regularExpressionAttribute.GetValidationResult(value, validationContext) == ValidationResult.Success))
            {
                return(ValidationResult.Success);
            }

            return(new ValidationResult(
                       this.FormatErrorMessage(validationContext.DisplayName)));
        }
示例#2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!(value is ICollection coll))
            {
                return(ValidationResult.Success);
            }
            ICollectionPropertyNamingStrategy cpns = validationContext.GetService(typeof(ICollectionPropertyNamingStrategy)) as ICollectionPropertyNamingStrategy;
            string message = "";
            int    idx     = 0;
            var    members = new List <string>();

            foreach (var c in coll)
            {
                RegularExpressionAttribute attr = new RegularExpressionAttribute(_pattern);
                var res     = attr.GetValidationResult(c, validationContext);
                var invalid = !string.IsNullOrWhiteSpace(res?.ErrorMessage);
                if (invalid)
                {
                    message = res.ErrorMessage;
                    if (cpns != null)
                    {
                        members.Add(cpns.CreateName(validationContext.MemberName, idx, ""));
                    }
                    else
                    {
                        members.Add($"{validationContext.MemberName}[{idx}]");
                    }
                }
                idx++;
            }

            if (members.Any())
            {
                return(new ValidationResult(message, members));
            }
            return(ValidationResult.Success);
        }