Retrieve display names from Validator

Hi Experts,

I am running on dot net 4.6.1

I have the same problem described in the following question

https://stackoverflow.com/questions/21834189/retrieve-display-names-from-validator


Please help ASAP,

Best,
Anas TINAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technology NinjaCommented:
Hi Anas,

Please post the actual question. This way people will be able to help you easily.

Regards,
Chinmay.
it_saigeDeveloperCommented:
Are you wanting something like this:
using EE_Q29145830.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace EE_Q29145830
{
    class Program
    {
        static void Main(string[] args)
        {
            var user = new User();
            var errors = user.GetValidationErrors();
            foreach (var error in errors)
            {
                Console.WriteLine(error);
            }
            Console.ReadLine();
        }
    }

    class User
    {
        [Display(Name = "LOGIN_FIELD", ResourceType = typeof(Resources))]
        [Required(ErrorMessageResourceName = "MISSING_LOGIN", ErrorMessageResourceType = typeof(Resources))]
        [StringLength(20, MinimumLength = 3, ErrorMessageResourceName = "LOGIN_LENGTH_ERROR", ErrorMessageResourceType = typeof(Resources))]
        [RegularExpression("^(?i)[a-z0-9]{1,40}$", ErrorMessageResourceName = "LOGIN_FORMAT_ERROR", ErrorMessageResourceType = typeof(Resources))]
        public string LOGIN { get; set; }
    }

    static class Extensions
    {
        public static IEnumerable<dynamic> GetValidationErrors<T>(this T source)
        {
            var context = new ValidationContext(source);
            var results = new List<ValidationResult>();
            Validator.TryValidateObject(source, context, results, true);

            return (from result in results
                    from member in result.MemberNames
                    let property = typeof(T).GetProperty(member)
                    where !Equals(property, null)
                    from attribute in property.GetCustomAttributes(typeof(DisplayAttribute), false).Cast<DisplayAttribute>()
                    select new { MemberName = member, result.ErrorMessage, DisplayName = attribute.Name });
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.