Exemplo n.º 1
0
        public void Validate(ValidateEvent e)
        {
            foreach (var property in properties)
            {
                var propertyValidations = new List<Failure>();

                foreach (var validationAttribute in property.PropertyInfo.GetCustomAttributes(typeof(ValidationAttribute), false).Cast<ValidationAttribute>())
                {
                    var validationContext = new ValidationContext(this, null, null) { MemberName = property.Name };
                    var result = validationAttribute.GetValidationResult(property.Value, validationContext);
                    if (result != ValidationResult.Success)
                    {
                        var validation = new Failure(result.ErrorMessage, property);
                        e.AddValidation(validation);
                        propertyValidations.Add(validation);
                    }
                }

                if (typeof(Model).IsAssignableFrom(property.PropertyInfo.PropertyType))
                {
                    var submodel = (Model)property.Value;
                    if (submodel != null)
                        submodel.Validate(e);
                }

                property.NotifyValidated(propertyValidations.ToArray());
            }
        }
Exemplo n.º 2
0
        public void Validate(ValidateEvent e)
        {
            foreach (var property in properties)
            {
                var propertyValidations = new List <Failure>();

                foreach (var validationAttribute in property.PropertyInfo.GetCustomAttributes(typeof(ValidationAttribute), false).Cast <ValidationAttribute>())
                {
                    var validationContext = new ValidationContext(this, null, null)
                    {
                        MemberName = property.Name
                    };
                    var result = validationAttribute.GetValidationResult(property.Value, validationContext);
                    if (result != ValidationResult.Success)
                    {
                        var validation = new Failure(result.ErrorMessage, property);
                        e.AddValidation(validation);
                        propertyValidations.Add(validation);
                    }
                }

                if (typeof(Model).IsAssignableFrom(property.PropertyInfo.PropertyType))
                {
                    var submodel = (Model)property.Value;
                    if (submodel != null)
                    {
                        submodel.Validate(e);
                    }
                }

                property.NotifyValidated(propertyValidations.ToArray());
            }
        }