Copied to clipboard

Flag this post as spam?

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


  • Amalie Wowern 144 posts 273 karma points c-trib
    Oct 15, 2015 @ 12:33
    Amalie Wowern
    0

    Use Dictionary item in Model

    Hi

    I'm creating a login form, I started with the Build in form from Umbraco. But i want a error messaged bassed on the language..

    So i made a a CustomLoginModel.

    and in the model i want the error message to use Dictionary items.

    But i get this error The type or namespace name 'GetDictionaryValue' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)

        public class CustomLoginModel
    {
        [Required(ErrorMessage = Umbraco.GetDictionaryValue("Username is missing")), Display(Name = Umbraco.GetDictionaryValue("Username"))]
        public string Username { get; set; }
        public string Password { get; set; }
        public string RedirectUrl { get; set; }
    }
    
  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Oct 15, 2015 @ 12:44
    Bjarne Fyrstenborg
    0

    Hi Amalie

    Have you included the "using Umbraco.Core" ? I think it is the namespace.

    You could also use UmbracoHelper to get dictionary items.

    /Bjarne

  • Amalie Wowern 144 posts 273 karma points c-trib
    Oct 15, 2015 @ 12:48
    Amalie Wowern
    0

    Hi Bjarne

    no I had not included Umbraco.Core but the error is still there.

    I haved tried Umbraco.Web

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Oct 15, 2015 @ 12:55
    Bjarne Fyrstenborg
    0

    Okay, what if you create another dictionary item "UsernameIsMissing" without spaces?

    Not sure if it solves the issue, but usually I don't use spaces in dictionary item keys.

    /Bjarne

  • Amalie Wowern 144 posts 273 karma points c-trib
    Oct 15, 2015 @ 12:58
    Amalie Wowern
    0

    That does change anything. But thanks the suggestions

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Oct 15, 2015 @ 13:08
    Bjarne Fyrstenborg
    0

    Can you try including using Umbraco.Web.Mvc or Umbraco.Web.UnbracoHelper?

    https://our.umbraco.org/Documentation/Reference/Templating/Mvc/views

    /Bjarne

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Oct 15, 2015 @ 14:39
    David Brendel
    0

    Hi Amalie,

    i wouldn't recommend using the dictionary like this and i'm quite sure this isn't possible at all.

    What I had done to achieve this was to create custom data annotations which are using the dictionary to provide the messages.

    Can't provide example right now cause in mobile but update as soon as possible.

    Regards David

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Oct 15, 2015 @ 18:26
    David Brendel
    2

    Hi Amalie,

    as said here is an example of a custom required annotation that uses the umbraco dictionary.

    public class UmbracoRequiredLocalised : RequiredAttribute, IClientValidatable
    {
        public UmbracoRequiredLocalised(string umbracoDictionaryKey)
        {
            this.ErrorMessage = Helper.GetDictionaryItem(umbracoDictionaryKey);
        }
    
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            yield return new ModelClientValidationRule()
            {
                ErrorMessage = this.ErrorMessage,
                ValidationType = "required"
            };
        }
    }
    

    Regards David

  • Jamie Howarth 306 posts 773 karma points c-trib
    Oct 16, 2015 @ 00:30
    Jamie Howarth
    0

    Hi Amalie,

    David's answer is a great example. Short answer, you can't use function evaluations as parameters to attributes - i.e. you can't do Umbraco.GetDictionaryValue() as your ErrorMessage, in the same way you couldn't use Request.Url or any other parameters. Values passed to attributes must be values or value types (e.g. const values or structs).

    David's answer is the way to go, then you would do:

        public class CustomLoginModel
    {
        [UmbracoRequiredLocalised("Username is missing"), Display(Name = Umbraco.GetDictionaryValue("Username"))]
        public string Username { get; set; }
        public string Password { get; set; }
        public string RedirectUrl { get; set; }
    }
    

    This way the UmbracoRequiredLocalised would be invoked at runtime, and get the dictionary parameter you wanted.

  • Remi Martel 32 posts 104 karma points
    Mar 20, 2018 @ 15:15
    Remi Martel
    0

    Is there a specific using I should add to my model. I get an error saying Helper does not contain a definition for GetDictionnaryItem

  • Poornima Nayar 106 posts 276 karma points MVP 5x c-trib
    Mar 21, 2018 @ 09:25
    Poornima Nayar
    0

    There is a good GitHub from Warren Buckley which can help you do this. I have used this in the past and it is very good. It uses Dictionary Items to store the language specific error messages https://github.com/warrenbuckley/Umbraco-Validation-Attributes

  • Remi Martel 32 posts 104 karma points
    Mar 21, 2018 @ 12:24
    Remi Martel
    0

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft