Fluent API is used to add validation at run time to a existing class. In our case we have WebinarViewmodel as below
public class WebinarViewModel : BaseViewModel
{
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Company { get; set; }
.....
}
So if we use this FluentAPI for validation rules theen we need to use this below line and need to pass the class name(WebinarViewModelValidator) which contains the validation rules. But if I am passing WebinarViewModelValidator as attribute parameter then this will apply to all the views whereever I use the class. But in my case I am using the same ViewModel in our of different application which are i the same solution and this viewmodel is in a common layer. And for each application I need different validation rules for the same ViewModel. So How can I pass different Validator class dynamically for different application in the solution?
[FluentValidation.Attributes.Validator(typeof(WebinarViewModelValidator))]
public class WebinarViewModelValidator : AbstractValidator<WebinarViewModel>
{
public WebinarViewModelValidator()
{
RuleFor(m => m.FirstName).NotEmpty().WithMessage("FirstName Can not be empty");
RuleFor(m => m.LastName).NotEmpty().WithMessage("LastName Can not be empty");
RuleFor(m => m.Phone).NotEmpty().WithMessage("Phone Can not be empty");
RuleFor(m => m.Company).NotEmpty().WithMessage("Company Can not be empty");
RuleFor(m => m.JobTitle).NotEmpty().WithMessage("JobTitle Can not be empty");
}
}
e.g viewmodel class is WebinarViewModel, validator is WebinarViewModelValidator
for application 1 : I need to add Required field validation for FirstName and LastName in WebinarViewModel class but For application 2 : I need add Required field validation for FirstName and Phone in same WebinarViewModel class.
Any help is much appreciated. Thanks in advance.