Copied to clipboard

Flag this post as spam?

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


  • Albion 7 posts 77 karma points
    Aug 14, 2020 @ 18:15
    Albion
    0

    Best way to use Umbraco dictionary item value in Model class

    [Required(ErrorMessage = "test error message!")] public string TestVal { get; set; }

    Instead of using this static value "test error message!" what's the best way to use Umbraco dictionary item value in the model class?

    Thanks

  • Yakov Lebski 591 posts 2346 karma points
    Aug 17, 2020 @ 03:00
    Yakov Lebski
    1

    You are welcome to use this package https://our.umbraco.com/packages/developer-tools/umbraco-dictionary-metadataprovider/ this is very easy way to add translation using naming convention in dictionary

  • Albion 7 posts 77 karma points
    Aug 17, 2020 @ 14:55
    Albion
    0

    Hi Yakov, enter image description here

    Thanks a lot for your reply. Managed to install the package.

    One more question: is there any way to have the full validation message using the dictionary item value instead of a combination of RequiredAttribute validation message and dictionary value?

    Eg: "The PropertyReference field is required." - this is the full validation message "PropertyReference" - gets from the dictionary value, but rest is default RequiredAttribute validation message

  • Yakov Lebski 591 posts 2346 karma points
    Aug 18, 2020 @ 11:56
    Yakov Lebski
    1

    You should manage it in this way

    your model for example ContactModel, has 2 properties Name, Email

    you should create in dictionary

    -Contact

    --Contact.Name

    --Contact.Email

    if you have attribues (for example Required) on you model

    --Contact.Name.Required

    for name convention properties you can create

    RequiredFormat: {0} is required InvalidFormat: {0} is invalid

    {0} this is name of property

  • Albion 7 posts 77 karma points
    Aug 25, 2020 @ 14:32
    Albion
    0

    Found another way to use dictionary item values as validation messages

    First step is, inside the model class use "IValidatableObject" interface to implement your own validation checks. For example, I'm using a simple null check.

    public class LoginModel:IValidatableObject
    {
        [DisplayName("property ref number")]
        public string PropertyReference { get; set; }
    
        [DataType(DataType.Password)]
        [AllowHtml]
        public string Password { get; set; }
    
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (string.IsNullOrWhiteSpace(PropertyReference))
                yield return new ValidationResult(UmbracoDictionary.GetDictionaryValue("Login.PropertyReference"));
    
            if (string.IsNullOrWhiteSpace(Password))           
                yield return new ValidationResult(UmbracoDictionary.GetDictionaryValue("Login.Password"));         
        }
    }
    

    Then create a separate class "UmbracoDictionary" which will allow you to access Umbraco dictionary items

    public class UmbracoDictionary
    {
        private static UmbracoHelper _helper;
    
        private static UmbracoHelper Helper
        {
            get
            {
                if (_helper == null)
                {
                    _helper = new UmbracoHelper();
                }
                return _helper;
            }
        }
    
        public static string GetDictionaryValue(string resourceKey)
        {
            string key = Helper.GetDictionaryValue(resourceKey);
            if (!string.IsNullOrEmpty(key))
                return key;
            return resourceKey;
        }
    }
    

    Finally, you can use ValidationSummary() inside your view page to display validation messages

    @Html.ValidationSummary()
    
  • Yakov Lebski 591 posts 2346 karma points
    Aug 25, 2020 @ 18:03
    Yakov Lebski
    0

    This is instesting way, but you need implement all validations by yourself.

Please Sign in or register to post replies

Write your reply to:

Draft