Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

Commented Unassigned: Set Validator with Arguments [7160]

$
0
0
Hello,

I created a validator to specifically validate a User Email within many UserModels:

```
public class UserEmailValidator : AbstractValidator<String> {

public UserEmailValidator(Func<IDispatcher> dispatcher, Func<IWebUser> user) {

RuleFor(x => x)
.EmailAddress().WithMessage("The email is invalid")
.Must(x => dispatcher().Send<ValidateUserEmailReply>(new ValidateUserEmailQuery(new Guid(user().Identity.GetUserId()), x)).Valid).WithMessage("The email is unavailable");

}
}
```

Then in a User Model Validator I have:

```
RuleFor(x => x.Email)..SetValidator(new UserEmailValidator());
```

But this does not compile because the UserEmailValidator does not have an empty constructor.

Is there a way to attach this validator and use it without passing the dispatcher and webuser from the parent validator?

What approach would you use?

Thank You,
Miguel




Comments: Hi No - you'd need to take these as arguments to the parent validator too.

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>