Copied to clipboard

Flag this post as spam?

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


  • Chizzl 36 posts 107 karma points
    Jul 04, 2013 @ 14:38
    Chizzl
    0

    Using Dictionary items in a SurfaceController

    Just wondering how to deal with regionalised labels & validation in a custom controller

            [Required(ErrorMessage = "City is required")]

            [Display(Name = "City*")]

            public string City { get; set; }

    How can I translate the Name and ErrorMessage with the DictionaryItems in my Custom Controller?

    umbraco.library.GetDictionaryItem("City") ain't working...  umbraco.library.GetDictionaryItem(“City”); either

  • fewle 2 posts 22 karma points
    Jul 04, 2013 @ 14:42
    fewle
    0

    uQuery.GetDictionaryItem("Key", "FallbackValue", languageId)

  • Chizzl 36 posts 107 karma points
    Jul 04, 2013 @ 14:57
    Chizzl
    0

    This gives me "cannot resolve symbol GetDictionaryItem"

  • fewle 2 posts 22 karma points
    Jul 04, 2013 @ 15:05
    fewle
    0

    If this inside an project you need to add reference to the umbraco.dll

  • Chizzl 36 posts 107 karma points
    Jul 04, 2013 @ 15:23
    Chizzl
    0

    I have referenced the umbraco.dll, but I'm using the dataannotions in a seperate model class, and I'm using that model in my controller.  I didn't describe the issue correctly in my initial question I think.

  • Chizzl 36 posts 107 karma points
    Jul 04, 2013 @ 15:36
    Chizzl
    100

    Aha, got it;

    I have extended the required attribute on my project so that it grabs the error message from the dictionary:

    Add new Item >> new class >> LocalizedRequiredAttribute.cs

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    using System.Web.Mvc;

    namespace xxx.Website
    {
        public class LocalizedRequiredAttribute : RequiredAttribute, IClientValidatable
        {
            public override string FormatErrorMessage(string name)
            {
                return umbraco.library.GetDictionaryItem(this.ErrorMessage);
            }

            public IEnumerable GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                yield return new ModelClientValidationRule
                {
                    // format the error message to include the property's display name.
                    ErrorMessage = FormatErrorMessage(metadata.DisplayName),
                    // uses the required validation type.
                    ValidationType = "required"
                };
            }
        }
    }

    Then, on my model I just decorate the error message:

      [LocalizedRequired(ErrorMessage = "XXXRequired")]

  • Craig Cronin 304 posts 503 karma points
    Jan 14, 2015 @ 14:30
    Craig Cronin
    0

    This solution doesn't seem to be working in later versions of Umbraco 7.1.*, 7.2.*.

    Has anyone come across this and have any solutions?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies