public class BecomeAnOrganiser
{
[Display(Name = "First Name")]
[Required]
public string FirstName { get; set; }
[Display(Name = "Email")]
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; }
}
Then I put this following syntax into my Razor template...
@Html.ValidationSummary()
<div class="form-row">
@Html.LabelFor(x => x.FirstName) *
@Html.EditorFor(x => x.FirstName)
</div>
<div class="form-row">
@Html.LabelFor(x => x.Email) *
@Html.EditorFor(x => x.Email)
</div>
It's great that Umbraco allows developers to build forms in this manner with surface controllers as it's fairly standard asp.net mvc practice. However (here it comes...), I'd really like to be able to reference dictionary item values in the message strings defined in my model.
I've had a look through the code and can't see any existing implementation so I'm considering implementing this myself. I've some ideas, but I'd like to ask the community first if there's an obvious way of doing it, or precedent set elsewhere in the codebase that should dictate how we do it here.
My initial thought is to use a token syntax that gets replaced with the dictionary item when the form is rendered, then hook in the relevant bits of the MVC rendering engine to do this, for example...
[Display(Name = "{Email Labell}")]
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "{Email Required Error Message}")]
public string Email { get; set; }
Thanks,
Phil
--
You received this message because you are subscribed to the Google Groups "Umbraco development" group.
To post to this group, send email to umbra...@googlegroups.com.
To unsubscribe from this group, send email to umbraco-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/umbraco-dev/-/XtgwuFdiWpkJ.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Umbraco development" group.
To post to this group, send email to umbra...@googlegroups.com.
To unsubscribe from this group, send email to umbraco-dev...@googlegroups.com.
[Display(Name = "First Name")]
[Required]
public string FirstName { get; set; }
To view this discussion on the web visit https://groups.google.com/d/msg/umbraco-dev/-/a7qbYUls024J.
To view this discussion on the web visit https://groups.google.com/d/msg/umbraco-dev/-/CZuctg20KPQJ.
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.