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.

5
  • Possible duplicate of Generic Validator for Model T Using Fluent Validation? Commented Jul 24, 2019 at 6:37
  • Is this link useful? chrissainty.com/… Commented Jul 25, 2019 at 5:01
  • This is the article i used to get started with FL. But it doesnt address the technical issue for validation T type models. Commented Jul 31, 2019 at 19:16
  • I tried going thru the 1st article (the answer posted by Steve Harris). But i simply couldnt understand it. Commented Jul 31, 2019 at 19:19
  • Please post your edited answered as a seperate answer and mark it as solution Commented Aug 7, 2019 at 18:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you share the codes of this solution?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.