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

Commented Unassigned: New feature request : Track current failures in validation context [7164]

$
0
0
Currently, while executing validation rules, one can not check if any previous failure has happened. My suggestion is extend the validation context to have all current failures. And add the validation context as an additional parameter to the "when" method.

This way, user can use something like

```
RuleFor(t => t.Name).NotEmpty();
RuleFor(t => t.Email).NotEmpty().Email();
RuleFor(t => t.Email).Must( e => check exists in the system ).When( ( m, ctx ) => ctx.HasFailure( m => m.Email) == false ) && ctx.HasFailure(m => m.Name) == false ) )
```

Currently I could find only one way to do this:

use private props in the class

```
private bool nameInvalid = false
private bool emailInvalid = false

RuleFor(t => t.Name).NotEmpty().OnAnyFailure(t => nameInvalid = true);
RuleFor(t => t.Email).NotEmpty().Email().OnAnyFailure(t => emailInvalid = true);
RuleFor(t => t.Email).Must( e => check email exists in the database ).When( m => !nameInvalid && !emailInvalid )
```

But this depends on private properties so when using composition with reusable validators, a validator instance can not access state of another validator instance.


Comments: I like this - it's a good idea. Unfortunately I don't have the time to add this at the moment, but if anyone else wants to have a go at it then feel free to submit a pull request

Viewing all articles
Browse latest Browse all 1917

Trending Articles



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