Thanks a lot for your reply. Managed to install the package.
One more question: is there any way to have the full validation message using the dictionary item value instead of a combination of RequiredAttribute validation message and dictionary value?
Eg:
"The PropertyReference field is required." - this is the full validation message
"PropertyReference" - gets from the dictionary value, but rest is default RequiredAttribute validation message
Found another way to use dictionary item values as validation messages
First step is, inside the model class use "IValidatableObject" interface to implement your own validation checks. For example, I'm using a simple null check.
public class LoginModel:IValidatableObject
{
[DisplayName("property ref number")]
public string PropertyReference { get; set; }
[DataType(DataType.Password)]
[AllowHtml]
public string Password { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (string.IsNullOrWhiteSpace(PropertyReference))
yield return new ValidationResult(UmbracoDictionary.GetDictionaryValue("Login.PropertyReference"));
if (string.IsNullOrWhiteSpace(Password))
yield return new ValidationResult(UmbracoDictionary.GetDictionaryValue("Login.Password"));
}
}
Then create a separate class "UmbracoDictionary" which will allow you to access Umbraco dictionary items
public class UmbracoDictionary
{
private static UmbracoHelper _helper;
private static UmbracoHelper Helper
{
get
{
if (_helper == null)
{
_helper = new UmbracoHelper();
}
return _helper;
}
}
public static string GetDictionaryValue(string resourceKey)
{
string key = Helper.GetDictionaryValue(resourceKey);
if (!string.IsNullOrEmpty(key))
return key;
return resourceKey;
}
}
Finally, you can use ValidationSummary() inside your view page to display validation messages
Best way to use Umbraco dictionary item value in Model class
[Required(ErrorMessage = "test error message!")] public string TestVal { get; set; }
Instead of using this static value "test error message!" what's the best way to use Umbraco dictionary item value in the model class?
Thanks
You are welcome to use this package https://our.umbraco.com/packages/developer-tools/umbraco-dictionary-metadataprovider/ this is very easy way to add translation using naming convention in dictionary
Hi Yakov,
Thanks a lot for your reply. Managed to install the package.
One more question: is there any way to have the full validation message using the dictionary item value instead of a combination of RequiredAttribute validation message and dictionary value?
Eg: "The PropertyReference field is required." - this is the full validation message "PropertyReference" - gets from the dictionary value, but rest is default RequiredAttribute validation message
You should manage it in this way
your model for example ContactModel, has 2 properties Name, Email
you should create in dictionary
-Contact
--Contact.Name
--Contact.Email
if you have attribues (for example Required) on you model
--Contact.Name.Required
for name convention properties you can create
RequiredFormat: {0} is required InvalidFormat: {0} is invalid
{0} this is name of property
Found another way to use dictionary item values as validation messages
First step is, inside the model class use "IValidatableObject" interface to implement your own validation checks. For example, I'm using a simple null check.
Then create a separate class "UmbracoDictionary" which will allow you to access Umbraco dictionary items
Finally, you can use ValidationSummary() inside your view page to display validation messages
This is instesting way, but you need implement all validations by yourself.
is working on a reply...