Example #1
0
        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            Object instance     = context.ObjectInstance;
            Type   type         = instance.GetType();
            Object proprtyvalue = type.GetProperty(PropertyName).GetValue(instance, null);

            if (proprtyvalue.ToString() == DesiredValue.ToString() && (value == null || value.ToString().Length < MinLength))
            {
                if (ErrorMessageResourceType != null && !string.IsNullOrEmpty(ErrorMessageResourceName))
                {
                    var field = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName);
                    if (field != null)
                    {
                        var val = field.GetValue(ErrorMessageResourceType, null) as string;
                        return(new ValidationResult(val));
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(ErrorMessage))
                    {
                        return(new ValidationResult(ErrorMessage));
                    }
                }
            }
            return(ValidationResult.Success);
        }
Example #2
0
        protected override string GetMessage(object objectToValidate, string key)
        {
            // стойността на за съобщението за грешка се извлича от ресурсите
            PropertyInfo nameProperty = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName, BindingFlags.Static | BindingFlags.Public);
            string       msg          = (string)nameProperty.GetValue(nameProperty.DeclaringType, null);

            return(String.Format(CultureInfo.CurrentUICulture, msg, key, this.DateFormat));
        }
        public override string FormatErrorMessage(string name)
        {
            // стойността на за съобщението за грешка се извлича от ресурсите
            PropertyInfo nameProperty = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName, BindingFlags.Static | BindingFlags.Public);
            string       msg          = (string)nameProperty.GetValue(nameProperty.DeclaringType, null);

            return(String.Format(CultureInfo.CurrentUICulture, msg, name));
        }
Example #4
0
        private string GetErrorMessageResource()
        {
            var errorMessageRes = ErrorMessageResourceType != null && ErrorMessageResourceName != null
                                ? ErrorMessageResourceType.GetProperty(ErrorMessageResourceName)
                                : null;

            return(errorMessageRes != null
                       ? errorMessageRes.GetValue(errorMessageRes.DeclaringType, null).ToString()
                       : ErrorMessage);
        }
Example #5
0
        public string GetErrorMessage()
        {
            if (string.IsNullOrEmpty(ErrorMessage))
            {
                PropertyInfo prop = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName);
                return(prop.GetValue(null, null).ToString());
            }

            return(ErrorMessage);
        }
        protected override string GetMessage(object objectToValidate, string key)
        {
            //Извличане на името на полето от типа му или от ресурсен файл, ако е от прост тип
            if (FieldNameResourceType != null && !string.IsNullOrEmpty(FieldNameResourceName))
            {
                _fieldName = FieldNameResourceType.GetProperty(FieldNameResourceName).GetValue(FieldNameResourceType, null).ToString();
            }

            PropertyInfo nameProperty = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName, BindingFlags.Static | BindingFlags.Public);
            string       msg          = (string)nameProperty.GetValue(nameProperty.DeclaringType, null);

            return(String.Format(CultureInfo.CurrentUICulture, msg, _fieldName));
        }
        protected override string GetMessage(object objectToValidate, string key)
        {
            if (FieldNameResourceType != null && !string.IsNullOrEmpty(FieldNameResourceName))
            {
                _fieldName = FieldNameResourceType.GetProperty(FieldNameResourceName).GetValue(FieldNameResourceType, null).ToString();
            }

            // стойността на за съобщението за грешка се извлича от ресурсите
            PropertyInfo nameProperty = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName, BindingFlags.Static | BindingFlags.Public);
            string       msg          = (string)nameProperty.GetValue(nameProperty.DeclaringType, null);

            return(String.Format(CultureInfo.CurrentUICulture, msg, !string.IsNullOrEmpty(_fieldName) ? _fieldName : key, this.UpperBound));
        }
        protected override string GetMessage(object objectToValidate, string key)
        {
            //Извличане на името на полето от типа му или от ресурсен файл, ако е от прост тип
            if (FieldType != null)
            {
                this._fieldName = FieldType.GetDisplayString();
            }
            else if (FieldNameResourceType != null && !string.IsNullOrEmpty(FieldNameResourceName))
            {
                _fieldName = FieldNameResourceType.GetProperty(FieldNameResourceName).GetValue(FieldNameResourceType, null).ToString();
            }

            // стойността на за съобщението за грешка се извлича от ресурсите
            PropertyInfo nameProperty = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName, BindingFlags.Static | BindingFlags.Public);
            string       msg          = (string)nameProperty.GetValue(nameProperty.DeclaringType, null);

            return(String.Format(CultureInfo.CurrentUICulture, msg, !string.IsNullOrEmpty(_fieldName) ? _fieldName : key, _sectionName));
        }
Example #9
0
        /// <summary>
        /// Genera el mensaje de error cuando la propiedad no es válida.
        /// Primero intenta obtener el texto de un recurso mediante ErrorMessageResourceType y ErrorMessageResourceName.
        /// Si no se han especificado valores para esas propiedades, se intenta obtener el texto de ErrorMessage.
        /// Si tampoco tiene valor, se utiliza un texto por defecto (internacionalizado).
        /// </summary>
        public override string FormatErrorMessage(string propertyDisplayName)
        {
            string plantillaMensajeError;

            if (ErrorMessageResourceType != null && !string.IsNullOrWhiteSpace(ErrorMessageResourceName))
            {
                PropertyInfo prop = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName, BindingFlags.Public | BindingFlags.Static);
                plantillaMensajeError = (string)prop.GetValue(obj: null, index: null);
            }
            else if (ErrorMessage != null)
            {
                plantillaMensajeError = ErrorMessage;
            }
            else
            {
                plantillaMensajeError = TextoErrorPorDefecto;
            }
            return(string.Format(plantillaMensajeError, propertyDisplayName));
        }
        protected string GetErrorString()
        {
            if (ErrorMessageResourceType == null)
            {
                return(ErrorMessage);
            }
            var prop = ErrorMessageResourceType.GetRuntimeProperty(ErrorMessage);

            if (prop == null)
            {
                throw new InvalidOperationException(
                          string.Format("The type {0} does not contain a property named {1}", ErrorMessageResourceType.FullName, ErrorMessage));
            }
            var value = prop.GetValue(null);

            if (value == null)
            {
                return(null);
            }
            return(value.ToString());
        }
Example #11
0
        /// <summary>Gets the message based on the resource.</summary>
        /// <remarks>
        /// This is more or less one-to-on copied from <see cref="ValidationAttribute"/>.
        /// </remarks>
        protected Func <string> GetResourceErrorMessage()
        {
            if (ErrorMessageResourceType == null || string.IsNullOrEmpty(ErrorMessageResourceName))
            {
                return(null);
            }
            var property = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);

            if (property != null)
            {
                var propertyGetter = property.GetGetMethod(true /*nonPublic*/);
                // We only support internal and public properties
                if (propertyGetter == null || (!propertyGetter.IsAssembly && !propertyGetter.IsPublic))
                {
                    // Set the property to null so the exception is thrown as if the property wasn't found
                    property = null;
                }
                if (property != null)
                {
                    if (property.PropertyType != typeof(string))
                    {
                        throw new InvalidOperationException(
                                  string.Format(
                                      CultureInfo.CurrentCulture,
                                      QowaivComponentModelMessages.ValidationRule_ResourcePropertyNotStringType,
                                      ErrorMessageResourceType.FullName,
                                      property.Name));
                    }
                    return(() => (string)property.GetValue(null, null));
                }
            }
            throw new InvalidOperationException(
                      string.Format(
                          CultureInfo.CurrentCulture,
                          QowaivComponentModelMessages.ValidationRule_ResourceTypeDoesNotHaveProperty,
                          ErrorMessageResourceType.FullName,
                          ErrorMessageResourceName));
        }