Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Armitage 510 posts 2081 karma points
    Feb 28, 2022 @ 12:27
    David Armitage
    0

    Umbraco 9 - Dictionary Labels in DataAnnotation Attributes for form labels and validation messages

    Hi All,

    Has anyone figured out how to use dictionary labels in basic form models for DataAnnotation error messages etc.

    Eg.

    [DisplayName("First Name")]
    [Required(ErrorMessage = "Please fill out the required field")]
    [StringLength(50, ErrorMessage = "Please reduce to a maximum of 50 characters")]
    public string FirstName { get; set; }
    

    So something on the lines of this that obviously doesn't work.

    [DisplayName("First Name")]
    [Required(ErrorMessage = _umbracoHelper.GetDictionaryValue("First Name"))]
    [StringLength(50, ErrorMessage = _umbracoHelper.GetDictionaryValue("Please reduce to a maximum of 50 characters"))]
    public string FirstName { get; set; }
    

    Any help would be much appreciated. Its always a pain to get attributes working like this. I assumed Unmbraco may have found a good way of handling this now?

    Thanks in advance.

    David

  • Philip Hayton 98 posts 435 karma points
    Mar 16, 2022 @ 16:09
    Philip Hayton
    0

    Don't suppose you found a solution to this did you?

  • Philip Hayton 98 posts 435 karma points
    Mar 25, 2022 @ 15:15
    Philip Hayton
    0

    Just want to update this thread with the following package which solved this problem for me.

    https://our.umbraco.com/packages/developer-tools/umbraco-validation-attributes/

    Thank you so much to Martino 🙏🏻

  • Anatoliy 6 posts 26 karma points
    Apr 23, 2024 @ 12:38
    Anatoliy
    0

    It is not help, it provides you own annotation attributes instead of original. For umbraco 7 i had spent couple of hours to make it works with original .net data annotation. Idea was to register own annotation provider. Strange that umbraco doesn't contains that from start. but.

  • Anatoliy 6 posts 26 karma points
    Apr 24, 2024 @ 09:16
    Anatoliy
    0

    Following provider could be used from .net core version:

    public class UmbracoModelMetadataProvider : EmptyModelMetadataProvider {        
    public override IEnumerable<ModelMetadata> GetMetadataForProperties(Type modelType)
    {
        var mla = modelType.GetCustomAttribute<Xpand.UmbracoLocalizableAttribute>();
        if (mla == null || mla.Enable == false)
            return base.GetMetadataForProperties(modelType);
        else
            return base.GetMetadataForProperties(modelType).Select(EnsureLocalizedAttributes).ToArray();
    }
    
    private ModelMetadata EnsureLocalizedAttributes(ModelMetadata mmd)
    {
        if (mmd is DefaultModelMetadata dmmd && dmmd != null)
        {
            if (!XApp.Get<IUmbracoHelperAccessor>().TryGetUmbracoHelper(out var helper)) return mmd;
            foreach (var attribute in dmmd.Attributes.Attributes)
            {
                var resulta = CloneAttribute(dmmd, attribute, helper);
            }                
        }
        return mmd;
    }
    protected virtual object CloneAttribute(DefaultModelMetadata propMetadata, object iattr, UmbracoHelper uhelper)
    {
        var d = iattr as DisplayAttribute;
        if (d != null) return CloneDisplayAttribute(propMetadata, d, uhelper);
        var v = iattr as ValidationAttribute;
        if (v != null) return CloneValidationAttribute(v, uhelper);
        return iattr;
    }
    
    private DisplayAttribute CloneDisplayAttribute(DefaultModelMetadata propMetadata, DisplayAttribute attr, UmbracoHelper uhelper)
    {
        if (attr.ResourceType != null) return attr;//it should be localized differently - from resources
    
        attr.Name = uhelper.GetDictionaryValueOrKey(attr.Name);
        attr.Description = uhelper.GetDictionaryValueOrKey(attr.Description);
        attr.ShortName = uhelper.GetDictionaryValueOrKey(attr.ShortName);
        attr.Prompt = uhelper.GetDictionaryValueOrKey(attr.Prompt);
        attr.GroupName = uhelper.GetDictionaryValueOrKey(attr.GroupName);
    
        propMetadata.DisplayMetadata.DisplayName =()=> attr.Name;
        propMetadata.DisplayMetadata.Placeholder= () => attr.Prompt;
        propMetadata.DisplayMetadata.Description= () => attr.Description;            
        return attr;
    }
    private ValidationAttribute CloneValidationAttribute(ValidationAttribute attr, UmbracoHelper uhelper)
    {
        if (attr.ErrorMessageResourceType != null) return attr;//it should be localized differently - from resources            
        return ClonValidationMessage(attr, attr, uhelper);            
    }
    
    private ValidationAttribute ClonValidationMessage(ValidationAttribute dattr, ValidationAttribute sattr, UmbracoHelper uhelper)
    {
        dattr.ErrorMessage = uhelper.GetDictionaryValueOrKey(sattr.ErrorMessage);
        return dattr;
    }}
    

    Class has to be registered instead of orginial service as singletone: builder.Register(x => new UmbracoModelMetadataProvider()).AsSelf().As

  • Yakov Lebski 591 posts 2347 karma points
    Apr 23, 2024 @ 13:20
    Yakov Lebski
    0

    We developed dictionary data anotation for umbraco 10+ - you can try to use it https://www.nuget.org/packages/Dyfort.Umbraco.DictionaryMetadataProvider/1.0.0-beta1

Please Sign in or register to post replies

Write your reply to:

Draft