/// <summary> /// The method receive the ViewModel to input validation and add on errors list /// (if there are errors after validation ViewModel.) /// </summary> /// <param name="viewModel">The ViewModel to input validation</param> protected void ValidateInput <T>(T viewModel) where T : LightViewModel <T>, new() { viewModel.InputErrors = _inputValidationErrors; viewModel.Validate(); InputResultValidation result = viewModel.Validator.Validate(viewModel); if (!result.IsValid) { foreach (var error in result.Errors) { ///receive the error code to add on errors list of input validation. AddInputValidationErrorCode(error); } } //By reflection, browse viewModel by identifying all attributes and lists for validation. foreach (PropertyInfo fieldInfo in viewModel.GetType().GetProperties()) { dynamic child = fieldInfo.GetValue(viewModel); //When the child is a list, validate each of its members if (child is IList) { var children = (IList)fieldInfo.GetValue(viewModel); foreach (var item in children) { //Check, if the property is a Light ViewModel, only they will validation Lights ViewModel if ((item.GetType().BaseType != typeof(object)) && (item.GetType().BaseType != typeof(System.ValueType)) && (item.GetType().BaseType.GetGenericTypeDefinition() == typeof(LightViewModel <>))) { dynamic obj = item; //Check, if the attribute is null for verification of the type. if (obj != null) { ValidateInput(obj); } } } } else { //Otherwise, validate the very child once. if (child != null) { //Check, if the property is a Light ViewModel, only they will validation Lights ViewModel if ((child.GetType().BaseType != typeof(object)) && (child.GetType().BaseType != typeof(System.ValueType)) && (child.GetType().BaseType.IsGenericType&& child.GetType().BaseType.GetGenericTypeDefinition() == typeof(LightViewModel <>))) { ValidateInput(child); } } } } }
/// <summary> /// The method receives the ViewModel to input validation and add on errors list. /// (if there are errors after validation ViewModel.) /// </summary> /// <param name="viewModel">The ViewModel to input validation</param> protected void ValidateInput <T>(T viewModel) where T : LightViewModel <T>, ILightViewModel, new() { viewModel.InputErrors = _inputValidationErrors; viewModel.Validate(); InputResultValidation result = viewModel.Validator.Validate(viewModel); if (!result.IsValid) { foreach (var error in result.Errors) { /// The method receive the error code to add on errors list of input validation. AddInputValidationErrorCode(error); } } //By reflection, browse viewModel by identifying all attributes and lists for validation. foreach (PropertyInfo fieldInfo in viewModel.GetType().GetProperties()) { dynamic child = fieldInfo.GetValue(viewModel); //Encoding of Special Characters if (child != null && child.GetType() == typeof(string)) { if (Regex.IsMatch((string)child, (@"[^a-zA-Z0-9]"))) { var encoder = HtmlEncoder.Create(allowedRanges: new[] { System.Text.Unicode.UnicodeRanges.BasicLatin, System.Text.Unicode.UnicodeRanges.Latin1Supplement }); child = encoder.Encode(child); fieldInfo.SetValue(viewModel, child); } } //When the child is a list, validate each of its members if (child is IList) { var children = (IList)fieldInfo.GetValue(viewModel); foreach (var item in children) { //Check, if the property is a Light ViewModel, only they will validation Lights ViewModel if ((item.GetType().BaseType != typeof(object)) && (item.GetType().BaseType != typeof(System.ValueType)) && (item.GetType().BaseType.GetGenericTypeDefinition() == typeof(LightViewModel <>))) { dynamic obj = item; //Check, if the attribute is null for verification of the type. if (obj != null) { ValidateInput(obj); } } } } else { //Otherwise, validate the very child once. if (child != null) { //Check, if the property is a Light ViewModel, only they will validation Lights ViewModel if ((child.GetType().BaseType != typeof(object)) && (child.GetType().BaseType != typeof(System.ValueType)) && (child.GetType().BaseType.IsGenericType&& child.GetType().BaseType.GetGenericTypeDefinition() == typeof(LightViewModel <>))) { ValidateInput(child); } } } } }