DisplayName

Jan 4, 2012 at 3:54 PM

is it possible to obtain the displayname at runtime ?

Im creating a custom validator for a column that has different meanings depending on what it is, its a Guid

 

eg

in the underlying table I might have 20 instances of CodeID that are types of occupation and then 30 instances that are types of industry

so for occupation id want a required field message of 'an occupation is required'

for industry id want 'an industry is required'

this is what Ive got so far

in my class

[DisplayName("Occupation")]

public CodeID Occupation { get; set; }


[DisplayName("Industry")]

public CodeID IndustryType { get; set; }

 

and my Metadata

[MetadataType(typeof(CodeIDMetaData))]
public partial class Code { }
public sealed class CodeIDMetaData
{
              
                
        [CodeIDRequiredAttribute("CodeID")]
        public Guid CodeID { get; set; }
}

Now my custom validator

public class CodeIDRequiredAttribute : ModelAwareValidationAttribute
    {
        //this is needed to register this attribute with foolproof's validator adapter
        static CodeIDRequiredAttribute() { Register.Attribute(typeof(CodeIDRequiredAttribute)); }

        private string _propertyName;

        public CodeIDRequiredAttribute(string propertyName) : base()
        {
            _propertyName = propertyName;
        }

        public override bool IsValid(object value, object container)
        {
            
            
            Type type = container.GetType();
            PropertyInfo propertyInfo = type.GetProperty(_propertyName);
            var model = Activator.CreateInstance(type);
            var provider = new DataAnnotationsModelMetadataProvider();
            var otherMetaData = provider.GetMetadataForProperty(() => model, type, _propertyName);
            string displayName = otherMetaData.DisplayName;
                       
            if (value == null)
            {
                return true;
            }
            else if ((Guid)value == Guid.Empty)//empty Guid
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

The Displayname is NULL !

 

How can i get hold of the displayname used for the property and use this in the message displayed to the users ??

Jan 5, 2012 at 9:31 AM

Does anyone use this forum ?

Coordinator
Jan 5, 2012 at 1:53 PM

I haven't played with Foolproof in conjunction with meta classes. I'll have to mess with it and see what I can come up with. I might have some time tonight.