Questions tagged [fluentvalidation]

Fluent Validation is an ASP.NET MVC integrated validation framework that allows the developer to set validation rules using expressions. It is testable and completely compatible with the inversion of control (dependency injection) pattern and TDD (test-driven development) technique.

Fluent Validation is an open-source, ASP.NET MVC / ASP.NET Core integrated validation framework that allows the developer to set validation rules using expressions. It is testable and completely compatible with the Inversion-of-Control (dependency injection) pattern and TDD (test-driven development) technique.

It is one of a number of validation options, including Data Annotations.

Visit the official website to learn more.

Source code can be found here.

1427 questions
162
votes
2 answers

Fluent Validation vs. Data Annotations

What are the operative differences between these two validation packages when used for ASP.NET MVC validatation? They seem to have similar objects, all the way to their object names. Is one related to another? What are their differences? In what way…
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
108
votes
1 answer

Conditional Validation using Fluent Validation

What I need is a way to conditionally validate fields depending on if other fields are filled in. Ex. I have a dropdown and a date field that are related. If none of the fields are set then the form should pass validation. However, if one of the…
The Sheek Geek
  • 4,126
  • 6
  • 38
  • 48
88
votes
7 answers

ViewModel validation for a List

I have the following viewmodel definition public class AccessRequestViewModel { public Request Request { get; private set; } public SelectList Buildings { get; private set; } public List Persons { get; private set; } } So in my…
ryan
  • 6,541
  • 5
  • 43
  • 68
86
votes
4 answers

FluentValidation rule for multiple properties

I have a FluentValidator that has multiple properties like zip and county etc. I want to create a rule that takes two properties just like a RuleFor construct public class FooArgs { public string Zip { get; set; } public System.Guid CountyId…
Sofia Khwaja
  • 1,909
  • 3
  • 17
  • 20
81
votes
3 answers

C# FluentValidation for a hierarchy of classes

I have a hierarchy of data classes public class Base { // Fields to be validated } public class Derived1 : Base { // More fields to be validated } public class Derived2 : Base { // More fields to be validated } What would be the…
Nikolay Nahimov
  • 810
  • 1
  • 6
  • 6
73
votes
6 answers

FluentValidation: Check if one of two fields are empty

I have this model public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } I want to create a validation where either FirstName or LastName must be filled in by…
Cybercop
  • 8,475
  • 21
  • 75
  • 135
51
votes
11 answers

FluentValidation rule for null object

I've been trying to work out how to create a FluentValidation rule that checks if the instance of an object it's validating is not null, prior to validating it's properties. I'd rather encapsulate this null validation in the Validator rather then…
Bern
  • 7,808
  • 5
  • 37
  • 47
49
votes
5 answers

FluentValidation - Validating a View Model that contains a list of an Object

I am trying out FluentValidation on a project that contains complex view models and I read the documentation here but I don't see how to set up the rules to validate a list of objects declared in my view model. In my example below, the list in the…
Slinky
  • 5,662
  • 14
  • 76
  • 130
44
votes
3 answers

unobtrusive client validation using fluentvalidation and asp.net mvc LessThanOrEqualTo not firing

I have the following rules the 1st does work using unobtrusive, client side validation, the second does not any ideas why? RuleFor(x => x.StartDate) .LessThanOrEqualTo(x => x.EndDate.Value) .WithLocalizedMessage(() =>…
user156888
41
votes
5 answers

Child Model Validation using Parent Model Values. Fluent Validation. MVC4

Below is a simplified version of my problem. I can not flatten the model. There is a List of "children" that I need to validate a birthday. I can not seem to reference the date in the Parent class and was wondering how this is done in Fluent…
40
votes
4 answers

checking if parameter is one of 3 values with fluent validation

I have a class containing one string property: public class Bla { public string Parameter { get; set; } } I would like to write a custom AbstractValidator, which checks that Parameter is equal to either one of these strings: str1, str2, str3 I…
cs0815
  • 16,751
  • 45
  • 136
  • 299
37
votes
3 answers

Add validation to a MediatR behavior pipeline?

I'm using ASP.NET Core, the built-in container, and MediatR 3 which supports "behavior" pipelines: public class MyRequest : IRequest { // ... } public class MyRequestHandler : IRequestHandler { public string…
grokky
  • 8,537
  • 20
  • 62
  • 96
36
votes
2 answers

FluentValidation string NotNull versus NotEmpty

Originally when writing validation logic for strings I settled on using NotEmpty for any string that was required. When using .NotEmpty().Length(min, max) this will cause two errors to be returned instead of just one when an empty string is passed…
Brett Allen
  • 5,297
  • 5
  • 32
  • 62
35
votes
2 answers

FluentValidation - validating across multiple properties

Have a form where a user can enter start date/time and end date/time for an event. Here's the validator so far: public class EventModelValidator : AbstractValidator { public EventModelValidator() { …
seekay
  • 2,493
  • 3
  • 27
  • 33
35
votes
3 answers

Stop Fluent Validation on first failure

i'm defining a validation for my Request objects. I would like the validator to stop on the very first failure, not only the one on the same chain. In the example below, if my TechnicalHeader object is null, i get a NullReference exception when the…
pizzaboy
  • 995
  • 1
  • 9
  • 20
1
2 3
95 96