0

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.

Chitta
  • 185
  • 2
  • 15
  • What about using conditions inside validator? You can add conditions using `When` to validation rules. – Adil Mammadov Sep 15 '16 at 13:57
  • please refer this link: http://stackoverflow.com/questions/39113921/fluent-validation-doesnt-validate-the-entire-form-the-first-time/39121738#39121738 – Sunil Kumar Sep 15 '16 at 13:59
  • @Adil, But this is in common layer so we have to do it at run time only, right? so how can I pass a parameter based on the application? – Chitta Sep 15 '16 at 14:21

0 Answers0