/// <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); }