public string GetMessage <T>(string propertyName, int lcid) where T : ValidationAttribute, new()
        {
            var da = new DataAnnotationDefaultStrings();

            var context = new GetMessageContext(new T(), typeof(TestModel), propertyName,
                                                new CultureInfo(lcid));

            return(da.GetMessage(context));
        }
Exemple #2
0
        /// <summary>
        /// Gets a list of validators.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="context">The context.</param>
        /// <param name="attributes">The list of validation attributes.</param>
        /// <returns>
        /// A list of validators.
        /// </returns>
        protected override IEnumerable <ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context,
                                                                      IEnumerable <Attribute> attributes)
        {
            var items = attributes.ToList();

            if (AddImplicitRequiredAttributeForValueTypes && metadata.IsRequired &&
                !items.Any(a => a is RequiredAttribute))
            {
                items.Add(new RequiredAttribute());
            }



            var validators = new List <ModelValidator>();

            foreach (var attr in items.OfType <ValidationAttribute>())
            {
                // custom message, use the default localization
                if (attr.ErrorMessageResourceName != null && attr.ErrorMessageResourceType != null)
                {
                    validators.Add(new DataAnnotationsModelValidator(metadata, context, attr));
                    continue;
                }

                // specified a message, do nothing
                if (attr.ErrorMessage != null && attr.ErrorMessage != WorkaroundMarker)
                {
                    validators.Add(new DataAnnotationsModelValidator(metadata, context, attr));
                    continue;
                }


                var ctx = new GetMessageContext(attr, metadata.ContainerType, metadata.PropertyName,
                                                Thread.CurrentThread.CurrentUICulture);

                var errorMessage   = ValidationMessageProviders.GetMessage(ctx);
                var formattedError = errorMessage == null
                                         ? GetMissingTranslationMessage(metadata, attr)
                                         : FormatErrorMessage(metadata, attr, errorMessage);

                var clientRules = GetClientRules(metadata, context, attr,
                                                 formattedError);
                validators.Add(new MyValidator(attr, formattedError, metadata, context, clientRules));
            }


            if (metadata.Model is IValidatableObject)
            {
                validators.Add(new Griffin.MvcContrib.Localization.ValidatableObjectAdapter(metadata, context));
            }


            return(validators);
        }