0

Im trying to see if there is a way to using FluentValidation without explicitly creating a validation model for every object type in my application.

Is this technically and currently possible?

Updated

To rephrase this, is it possible for FluentValidation to validate Rules WITHOUT a IValidator context? Instead, i would like to pass in the item instance to be validated and use validation Rules which are constructed on-the-fly.

Resolved

I was able to resolve by doing a kind of a hack solution. Here are basic details of what i did:

1) I created a class (GenericModel) which has a collection of objects representing properties in a model. 2) I created a validator class that inherits from AbstractValidator. 3) Based on GenericModel's collection of "property objects" + additional metadata about each property's validation rules and error messages, i was able to add FluentValues Rules, all at run-time. 4). At the EditForm i handled the OnSubmit event. In that event handler i execute validation via FluentValidation's ValidateAsync() method. 5). Finally, i iterate thru the validation results and update each field with appropriate CSS, error messages, highlighting...etc.

AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140
  • Possible duplicate of [Generic Validator for Model T Using Fluent Validation?](https://stackoverflow.com/questions/49108649/generic-validator-for-model-t-using-fluent-validation) – jazb Jul 24 '19 at 06:37
  • Is this link useful? https://chrissainty.com/using-fluentvalidation-for-forms-validation-in-razor-components/ – dani herrera Jul 25 '19 at 05:01
  • This is the article i used to get started with FL. But it doesnt address the technical issue for validation T type models. – AlvinfromDiaspar Jul 31 '19 at 19:16
  • I tried going thru the 1st article (the answer posted by Steve Harris). But i simply couldnt understand it. – AlvinfromDiaspar Jul 31 '19 at 19:19
  • Please post your edited answered as a seperate answer and mark it as solution – citronas Aug 07 '19 at 18:48

1 Answers1

0

I was able to resolve by doing a kind of a hack solution. Here are basic details of what i did:

  1. I created a class (GenericModel) which has a collection of objects representing properties in a model.

  2. I created a validator class that inherits from AbstractValidator.

  3. Based on GenericModel's collection of "property objects" + additional metadata about each property's validation rules and error messages, i was able to add FluentValues Rules, all at run-time.

  4. At the EditForm i handled the OnSubmit event. In that event handler i execute validation via FluentValidation's ValidateAsync() method.

  5. Finally, i iterate thru the validation results and update each field with appropriate CSS, error messages, highlighting...etc.

AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140