/// <summary> /// Generates the validators. /// </summary> /// <param name="containerControl">The container control.</param> /// <param name="target">The target.</param> public void GenerateValidators(Control containerControl, Type targetType) { // Generate the ui controls foreach (ValidationAssociation association in controlList) { // Get the control from the ui Control control = GetControlRecursive <Control>(association.ControlID, containerControl); // Make sure we found the control Debug.Assert(null != control); // Get the property info (could be nested) PropertyInfo prop = GetNestedProperty(targetType, association.PropertyName); // Make sure we found the propertyu info Debug.Assert(null != prop); if (null != prop) { // Get the validation attributes for the property ValidatorAttribute[] attributes = (ValidatorAttribute[])prop.GetCustomAttributes(typeof(ValidatorAttribute), true); foreach (ValidatorAttribute attribute in attributes) { // The property we're validating attribute.PropertyToValidate = new PropertyInfoExtended(prop, null); // Add web UI validator for each property IValidatorCreator creator = GetValidator(control, attribute); if (creator != null) { creator.CreateValidator(); } } } } }
/// <summary> /// Carrega uma validação. /// </summary> /// <param name="validationId">Identificador da validação</param> /// <param name="culture"></param> /// <returns></returns> private IValidator GetValidator(int validationId, System.Globalization.CultureInfo culture) { IValidator validator = null; bool found = false; lock (_validationValidator) found = _validationValidator.TryGetValue(validationId, out validator); if (!found) { var validation = Settings.GetValidation(validationId); if (validation != null) { var validationType = Settings.GetValidationType(validation.ValidationTypeUid); if (validationType != null && validationType.Type != null) { IValidatorCreator creator = GetValidatorCreator(validationType, culture); if (creator != null) { var parameters = new List <ParameterValue>(); foreach (var currentParameter in validationType.Parameters) { var validationParameter = validation.Parameters.Where(f => f.Name == currentParameter.Name).FirstOrDefault(); parameters.Add(new ParameterValue(currentParameter.Name, (validationParameter == null) ? currentParameter.DefaultValue : validationParameter.Value)); } try { validator = creator.CreateValidator(parameters, culture); } catch (Exception ex) { _logger.Error(ResourceMessageFormatter.Create(() => Properties.Resources.GeneralValidationManager_CreateValidatorToValidationError, validation.Name, Diagnostics.ExceptionFormatter.FormatException(ex, true)), ex); } } } } lock (_validationValidator) if (_validationValidator.ContainsKey(validationId)) { _validationValidator.Add(validationId, validator); } } return(validator); }
/// <summary> /// Recupera o criador de validadores. /// </summary> /// <param name="validationType">Tipo de validação.</param> /// <param name="culture"></param> /// <returns></returns> private IValidatorCreator GetValidatorCreator(ValidationType validationType, System.Globalization.CultureInfo culture) { IValidatorCreator creator = null; bool found = false; lock (_validatorCreators) found = _validatorCreators.TryGetValue(validationType.ValidationTypeId, out creator); if (!found) { System.Reflection.Assembly assembly = null; if (Reflection.AssemblyLoader.Instance.TryGet(validationType.Type.AssemblyName.Name, out assembly)) { Type validatorType = null; try { validatorType = assembly.GetType(validationType.Type.FullName, true, false); } catch (Exception ex) { _logger.Error(ResourceMessageFormatter.Create(() => Properties.Resources.GeneralValidationManager_ValidationTypeGetTypeError, validationType.Type.FullName, validationType.Name, Diagnostics.ExceptionFormatter.FormatException(ex, true)), ex); } if (validationType != null) { creator = ValidatorCreator.CreateCreator(validatorType, culture); lock (_validatorCreators) if (!_validatorCreators.ContainsKey(validationType.ValidationTypeId)) { _validatorCreators.Add(validationType.ValidationTypeId, creator); } } } else { _logger.Error(ResourceMessageFormatter.Create(() => Properties.Resources.GeneralValidationManager_ValidationTypeAssemblyNotFound, validationType.Type.AssemblyName.Name, validationType.Name)); } } return(creator); }