-1

I am using MVC form in Sitecore.

In order to validate fields i am using attributes over model fields. My requirement is to get the error message from Sitecore instead of hardcoding it.

I am currently using Required,Regex,RequiredIf,Remote validation of MVC.

Can I achieve this? The error messages are in an item inside different field names

1 Answer 1

0

Certainly, just use constants and get copy from Sitecore dictionries. Habitat has a great example. https://github.com/Sitecore/Habitat/blob/master/src/Feature/Accounts/code/Models/LoginInfo.cs

namespace Sitecore.Feature.Accounts.Models
{
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using Sitecore.Feature.Accounts.Attributes;
    using Sitecore.Foundation.Dictionary.Repositories;

    public class LoginInfo
    {
        [Display(Name = nameof(EmailCaption), ResourceType = typeof(LoginInfo))]
        [Required(ErrorMessageResourceName = nameof(Required), ErrorMessageResourceType = typeof(LoginInfo))]
        [EmailAddress(ErrorMessageResourceName = nameof(InvalidEmailAddress), ErrorMessageResourceType = typeof(LoginInfo))]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Display(Name = nameof(PasswordCaption), ResourceType = typeof(LoginInfo))]
        [Required(ErrorMessageResourceName = nameof(Required), ErrorMessageResourceType = typeof(LoginInfo))]
        [PasswordMinLength(ErrorMessageResourceName = nameof(MinimumPasswordLength), ErrorMessageResourceType = typeof(LoginInfo))]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        public string ReturnUrl { get; set; }
        public IEnumerable<FedAuthLoginButton> LoginButtons { get; set; }

        public static string EmailCaption => DictionaryPhraseRepository.Current.Get("/Accounts/Login/Email", "E-mail");
        public static string PasswordCaption => DictionaryPhraseRepository.Current.Get("/Accounts/Login/Password", "Password");
        public static string MinimumPasswordLength => DictionaryPhraseRepository.Current.Get("/Accounts/Login/Minimum Password Length", "Please enter a password with at lease {1} characters");
        public static string Required => DictionaryPhraseRepository.Current.Get("/Accounts/Login/Required", "Please enter a value for {0}");
        public static string InvalidEmailAddress => DictionaryPhraseRepository.Current.Get("/Accounts/Login/Invalid Email Address", "Please enter a valid email address");
    }
}
2
  • My error messages are clubbed in an item i.e inside different fields. Commented Jul 9, 2020 at 18:10
  • That is still fine, it just needs to be a static call.
    – Chris Auer
    Commented Jul 9, 2020 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.