Question

I have got some problem with FluentValidation. Does anyone have idea about this problem ?

I have Resource files in App_GlobalResources folder.

Error : "Only MemberExpressions an be passed to BuildResourceAccessor, eg () => Messages.MyResource"

 public class ProductValidator : AbstractValidator<Product>
    {
        public static string prefix = "{PropertyName}";
        public ProductValidator()
        {
            RuleFor(product => product.Name).NotNull().NotEmpty().WithLocalizedMessage(() => Resources.Validators.NotNullOrEmpty.Replace(prefix,Resources.Entity_Product.Name));
        }
    }
Was it helpful?

Solution

I found the solution;

ResourceManager validatorResources = new ResourceManager("OpusERP.Web.UI.App_GlobalResources.Validators", Assembly.GetExecutingAssembly());

    string message = NotNullOrEmpty.Replace(prefix, productResources.GetString("Name", Thread.CurrentThread.CurrentCulture));

    RuleFor(product => product.Name).NotNull().NotEmpty().WithMessage(message); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top