Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can inherit custom attribute from <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx" rel="noreferrer">RequiredAttribute</a> and set your own localized message for property <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.errormessage.aspx" rel="noreferrer">ErrorMessage</a>. It can looks like this:</p> <pre><code>public class LocalizedRequiredAttribute : RequiredAttribute { public LocalizedRequiredAttribute() : base() { // prefix for the selection of localized messages from datebase // e.x. for "Required" string, localized messages will be: "RuRequired", "EnRequired" var currentCulture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; // logic to get value from datebase // e.x. using Linq2Sql using (var context = new dateBaseContext()) { var query = (from x in context.LocalizedStrings where x.NameKey == currentCulture + "Required" select x.NameValue).SingleOrDefault(); if (query != null) { base.ErrorMessage = query; } else { base.ErrorMessage = "UndefinedName"; } } } } </code></pre> <p>also and you inherit from <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.displaynameattribute.aspx" rel="noreferrer">DisplayNameAttribute</a> and override <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.displaynameattribute.displayname.aspx" rel="noreferrer">DisplayName</a> property:</p> <pre><code>public class LocalizedDisplayNameAttribute : DisplayNameAttribute { public LocalizedDisplayNameAttribute(string displayNameKey) : base(displayNameKey) { } public override string DisplayName { get { if (!string.IsNullOrEmpty(base.DisplayName)) { // prefix for the selection of localized messages from datebase // e.x. if DisplayName is "Country", localized messages will be: "RuCountry", "EnCountry" var currentCulture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; // logic to get value from datebase // e.x. using Linq2Sql using (var context = new dateBaseContext()) { var query = (from x in context.DisplayNames where x.DisplayNameKey == currentCulture + base.DisplayName select x.DisplayNameValue).SingleOrDefault(); if (query != null) { return query; } return base.DisplayName; } } return "UndefinedName"; } } } </code></pre> <p>also you can create your custom validation attributes that inherits from <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx" rel="noreferrer">ValidationAttribute</a> class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload