Beispiel #1
0
        private void ValidateProperties(dynamic model, ref Dictionary <string, object> validationDictionary)
        {
            if (model == null)
            {
                return;
            }

            foreach (var property in model.GetType().GetProperties())
            {
                if (property.PropertyType.GetConstructor(Type.EmptyTypes) != null &&
                    !property.PropertyType.GetType().IsAbstract)
                {
                    this.ValidateProperties(property.GetValue(model), ref validationDictionary);
                }

                var isValidated = false;
                foreach (var attribute in property.GetCustomAttributes(false))
                {
                    if (isValidated == true)
                    {
                        break;
                    }

                    var value = property.GetValue(model);

                    if (attribute is RequireInputAttribute)
                    {
                        var result = TfValidationResult.FieldRequired(property.Name, value);
                        if (result != null)
                        {
                            this.AddModelErrors(property.Name, result, ref validationDictionary);
                            isValidated = true;
                        }
                    }

                    if (attribute is ValidateInputAttribute && value != null)
                    {
                        var validateInputAttribute = attribute as ValidateInputAttribute;
                        var result = TfValidationResult.ValidateInput(validateInputAttribute.InputType, value, property.Name);
                        if (result != null)
                        {
                            this.AddModelErrors(property.Name, result, ref validationDictionary);
                            isValidated = true;
                        }
                    }
                }
            }
        }