Copied to clipboard

Flag this post as spam?

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


  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 25, 2013 @ 15:19
    Dave Woestenborghs
    0

    Surface controller and data annotations

    Hi,

    I have a surface controller and a custom model to display a form on the website. I decorate my model properties with data annotation attributes like Required, display name. 

    Model code:

    using System;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
    
        public class MyModel
        {
            [Required()]        
            public string Title { get; set; }
    
            [Required()]  
            public DateTime Date { get; set; }
    
            [Required()]  
            public string Intro { get; set; }
    
        }

    View code :

    @model MyModel
    
    
    @using (Html.BeginUmbracoForm("FormSubmit", "MyController"))
    {
        @Html.EditorFor(x => Model)
        <input type="submit" value="Submit"/>
    }
    
    If submit the form the validation message will say : The title field is required.
    Is there some way to get the text for these validation messages from the Umbraco dictionary instead of the default text. Should I create custom data annotations or a custom model meta data provider to look up the dictionary items ? Or is there already something available out of the box ?
    Otherwise surface controllers are useless in multilanguage solutions.
    Dave

     

     

     

  • Barry Fogarty 493 posts 1129 karma points
    Mar 25, 2013 @ 16:35
    Barry Fogarty
    0

    Hi Dave,

    Have a look at the following thread in the dev goup:

    https://groups.google.com/forum/#!msg/umbraco-dev/VOilW-tfRS4/CZuctg20KPQJ

    Phil Harvey had the same issue and has provided his solution code.  I also spoke to Ishmail Mayat about this issue at the Cogworks v6 hack day and he talked about about potentially extending the Data Annotations Extensions project (if I remember rightly)

    http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx

    This is on my list as well, so let us know how you get on.

  • Charles Afford 1163 posts 1709 karma points
    Mar 25, 2013 @ 22:41
    Charles Afford
    0

    Hi, you could add an errormessage property to the [Requied()] attribute

    So its something like: [Required(ErrorMessage = "My Custom Message")]

    and the in your view/partial instead of Html.EditorFor(x=>x.model);

    do

    Html.TextBoxFor(m=>m.yourmodelproperty);

    This would probably be your best bet? What sort of annotations are you trying to use? Thanks. Charlie :)  

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 26, 2013 @ 09:29
    Dave Woestenborghs
    0

    @Charles 

    I know I can do it like that, but that won't work in multilanguage solutions. So I want to pass in dictionary items.

    @Barry

    I was certain I saw this somewhere but couldn't find it anymore. Thanks for the link to the google groups.

    I also was thinking about creating my own data annotations, but personally I would like to see this in the core. There should be a out of the box solution.

    Dave

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 26, 2013 @ 09:50
    Dave Woestenborghs
    0

    Create a issue for it in Youtrack : http://issues.umbraco.org/issue/U4-2000

  • Charles Afford 1163 posts 1709 karma points
    Mar 26, 2013 @ 09:50
    Charles Afford
    0

    Ahhh i see did not realise it was multilanguage.  Where is the message going to come from?  Charlie :)

  • Seth Niemuth 275 posts 397 karma points
    Apr 30, 2013 @ 17:21
    Seth Niemuth
    2

    Charlie, He is wanting the message to come from the umbraco dictionary.

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

     using System.Collections.Generic;

        using System.ComponentModel.DataAnnotations;

        using System.Web.Mvc;

     

        public class LocalizedRequiredAttribute : RequiredAttribute, IClientValidatable

        {

            public override string FormatErrorMessage(string name)

            {

                return umbraco.library.GetDictionaryItem(this.ErrorMessage);

            }

            public IEnumerable<ModelClientValidationRule> 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 = "Forms.Enquiry.FirstName_Required")]

    public string FirstName { get; set; }

     

    Just posting this in case someone wants to use it without creating their own model bindings.

  • Charles Afford 1163 posts 1709 karma points
    Apr 30, 2013 @ 22:04
    Charles Afford
    0

    Thats brilliant thanks for this.  I will give it a try :)

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 01, 2013 @ 10:07
    Dave Woestenborghs
    0

    Hi Seth,

    Nice code sample. Only downside to this is that you will need to create this for every dataannotation.

    This code will handle all : http://pastebin.com/7gJ34M8x

  • Yakov Lebski 591 posts 2345 karma points
    Dec 12, 2014 @ 21:23
Please Sign in or register to post replies

Write your reply to:

Draft