Copied to clipboard

Flag this post as spam?

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


  • Rob Shaw 37 posts 170 karma points c-trib
    Nov 16, 2019 @ 15:36
    Rob Shaw
    0

    Advice on injecting a localisation service for a custom class

    Hi All,

    I am creating a very simple custom required attribute which I have got working but it currently involves an anti-patter that I am trying to replace with an injected localisation service.

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    
    using Current = Umbraco.Web.Composing.Current;
    
    namespace MyUmbraco.DataAnnotations.Attributes
    {
      public class UmbracoRequiredAttribute : RequiredAttribute, IClientValidatable
      {
        public string DictionaryKey { get; set; } = "Validation.Required";
    
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
          var ErrorMessage = Current.UmbracoHelper.GetDictionaryValue(DictionaryKey);
          yield return new ModelClientValidationRequiredRule(ErrorMessage);
        }
      }
    }
    

    As you can see I am using the Current.UmbracoHelper.GetDictionary() method which I know is a big "No No!". I would like to know if anyone has had experience with injecting the localisation service into a custom class?

    Thanks,

    Rob

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Nov 22, 2019 @ 00:03
    Paul Seal
    1

    Try something like this.

    using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; using Umbraco.Core.Composing; using Umbraco.Core.Services;

    namespace CleanBlog.Core
    {
        public class UmbracoRequiredAttribute : RequiredAttribute, IClientValidatable
        {
            public UmbracoRequiredAttribute(ILocalizationService localizationService)
            {
                _localizationService = localizationService;
            }
    
            private readonly ILocalizationService _localizationService;
    
            public string DictionaryKey { get; set; } = "Validation.Required";
    
            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                //you would need a way to know the current language, but for now we have the default one
                var defaultLanguageId = _localizationService.GetDefaultLanguageId();
    
                //this gets the item from the dictionary by using the key and then returns the translation for the default language.
                var ErrorMessage = _localizationService.GetDictionaryItemByKey(DictionaryKey).Translations.Where(x => x.LanguageId == defaultLanguageId).FirstOrDefault().Value;
                yield return new ModelClientValidationRequiredRule(ErrorMessage);
            }
        }
    }
    

    Cheers

    Paul

  • Rob Shaw 37 posts 170 karma points c-trib
    Nov 22, 2019 @ 12:44
    Rob Shaw
    0

    Hi Paul!

    Thanks for the reply, but it's a path I've been down already and I get the following issue:

    Attribute Issue Because we are injecting the ILocalizationService into the constructor it expects one to be passed in when I decorate Model properties 😅 Does anyone know a way around this? I've seen mentions of using Action Filters...but really hoping for another solution!

    BTW loving the Umbraco 8 videos you've been creating, keep up the good work!

    Cheers, Rob

Please Sign in or register to post replies

Write your reply to:

Draft