Exemple #1
0
        public bool ValidateProperty(string propertyName)
        {
            bool isValid = false;

            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }
            try
            {
                var propertyInfo = this.GetType().GetRuntimeProperty(propertyName);
                if (propertyInfo == null)
                {
                    throw new ArgumentException("Invalid property name", propertyName);
                }

                var propertyErrors = new List <string>();
                isValid = TryValidateProperty(propertyInfo, propertyErrors);
                ErrorsContainer.SetErrors(propertyInfo.Name, propertyErrors);
            }
            catch (Exception ex)
            {
                PropertyInfo propertyInfo = (from row in this.GetType().GetRuntimeProperties()
                                             where row.Name == propertyName
                                             select row).SingleOrDefault();
                if (propertyInfo == null)
                {
                    throw new ArgumentException("Invalid property name", propertyName);
                }
            }
            return(isValid);
        }
Exemple #2
0
        public bool ValidateProperties()
        {
            var propertiesWithChangedErrors = new List <string>();

            // Get all the properties decorated with the ValidationAttribute attribute.
            if (PropertyInfos != null)
            {
                foreach (PropertyInfo propertyInfo in PropertyInfos)
                {
                    var propertyErrors = new List <string>();
                    TryValidateProperty(propertyInfo, propertyErrors);

                    // If the errors have changed, save the property name to notify the update at the end of this method.
                    ErrorsContainer.SetErrors(propertyInfo.Name, propertyErrors);
                }
            }

            var propertiesToValidate = this.GetType()
                                       .GetRuntimeProperties()
                                       .Where(c => c.GetCustomAttributes(typeof(ValidationAttribute)).Any());

            foreach (PropertyInfo propertyInfo in propertiesToValidate)
            {
                var propertyErrors = new List <string>();
                TryValidateProperty(propertyInfo, propertyErrors);

                // If the errors have changed, save the property name to notify the update at the end of this method.
                ErrorsContainer.SetErrors(propertyInfo.Name, propertyErrors);
            }

            return(ErrorsContainer.HasErrors);
        }
Exemple #3
0
        public bool ValidateProperty(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            var propertyInfo = this.GetType().GetRuntimeProperty(propertyName);

            if (propertyInfo == null)
            {
                throw new ArgumentException("Invalid property name", propertyName);
            }

            var  propertyErrors = new List <string>();
            bool isValid        = TryValidateProperty(propertyInfo, propertyErrors);

            ErrorsContainer.SetErrors(propertyInfo.Name, propertyErrors);

            return(isValid);
        }
Exemple #4
0
 public void SetErrors <TProperty>(Expression <Func <TProperty> > propertyExpression, IEnumerable <string> errors)
 {
     ErrorsContainer.SetErrors(propertyExpression, errors);
 }