Copied to clipboard

Flag this post as spam?

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


  • Scott 95 posts 277 karma points
    Oct 23, 2013 @ 18:31
    Scott
    0

    MVC model form jquery validation localized

    Hi all,

    I have been trying to create a form with a model a view and a controller.

    Great I've got the form with validation. However now I try to change the language to Danish but it stays English.

    View:

    @using (Html.BeginUmbracoForm("SendEmail", "ContactFormSurface"))
    {
        @Html.AntiForgeryToken();
        @Html.EditorFor(model => Model);
        <input type="submit" name="Submit"/>
    }
    

    Model:

    public class ContactFormModel
    {
        [Required]
        [Display(Name = "Navn")]
        public string Name { get; set; }
    
        [Required]
        [Display(Name = "E-mail")]
        [RegularExpression(@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$")]
        public string Email { get; set; }
    
        [Required]
        [Display(Name = "Telefonnummer")]
        public string TelephoneNumber { get; set; }
    
        [Required]
        [Display(Name = "Besked")]
        [DataType(DataType.MultilineText)] 
        public string Message { get; set; }
    }
    

    I have included this in web.config

    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    

    I have added these scripts to the page:

    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/localization/messages_da.js"></script>
    

    So what my problem here is that the form is written out just fine, but the validation is always English. How can I change that based on the user language?

    I have tried by setting the Thread like this:

    @{
        CultureInfo ci = new CultureInfo("da-DK");
    
        Thread.CurrentThread.CurrentUICulture = ci;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
    }
    

    And like this in web.config:

    <globalization requestEncoding="UTF-8" responseEncoding="UTF-8" culture="da-DK" />
    

    But honestly until now no luck!

    Can anybody help me?

    Thanks

  • Kasper Holm 47 posts 180 karma points
    Nov 26, 2013 @ 08:09
    Kasper Holm
    0

    Hello Scott

    Great to se more people using umbraco MVC :)

    what i think you missing is a resurce name in your atribute like

    [Required(ErrorMessageResourceName = "resourceName")] public string LinkOne { get; set; }

    you will offcourse need a resource file also with the message in :)

    hope this helps.

  • Scott 95 posts 277 karma points
    Nov 26, 2013 @ 08:29
    Scott
    0

    Hi Kasper,

    Thanks for pitching in really.

    Even though I solved it otherwise, a silly thing really, I had changed the default language in Umbraco from English (US) to Danish (DK) and that basically rules.

    But when I then added the language instead and set the Hostname with culture to the newly added language then it worked.

    As I said, it really is silly and about that I had to add Danish instead of changing the default language in Umbraco is probably a bug.

    I did get it to work out of the box with no need to use the resource approach with of course works great.

    Thank you

    Regards,

  • Scott 95 posts 277 karma points
    Nov 26, 2013 @ 13:04
    Scott
    0
  • 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