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; }
}
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.
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)
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.
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:
View code :
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.
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 :)
@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
Create a issue for it in Youtrack : http://issues.umbraco.org/issue/U4-2000
Ahhh i see did not realise it was multilanguage. Where is the message going to come from? Charlie :)
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.
Thats brilliant thanks for this. I will give it a try :)
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
Hi I create package based on this article http://our.umbraco.org/forum/templating/templates-and-document-types/39555-Surface-controller-and-data-annotations
is working on a reply...