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
21
votes
3 answers

FluentValidation how to check for Length if string is not null?

I'm testing a PUT with two string: company.CurrencyCode = request.CurrencyCode ?? company.CurrencyCode; company.CountryIso2 = request.Country ?? company.CountryIso2; and I tried with a rule like: public UpdateCompanyValidator() { …
balexandre
  • 73,608
  • 45
  • 233
  • 342
20
votes
3 answers

Pass an element of the object to a FluentValidation SetValidator's constructor

I'm using FluentValidation to validate a collection inside of an object, comparing an element of the collection items to an element of the parent object. The goal output is to receive ValidationFailures for each failed item in the collection, not…
friggle
  • 3,362
  • 3
  • 35
  • 47
20
votes
5 answers

Debugging fluent validation rules

The problem I'm struggling to make my Fluent Validation RuleSet work, currently it doesn't, and I don't have any idea why is that happening, everything seems all right. I would like to somehow step into the code that performs the validation itself,…
Lu4
  • 14,873
  • 15
  • 79
  • 132
20
votes
3 answers

FluentValidation - Check value is a date only if NOT NULL

I have a bool along with a nullable DateTime property. The DateTime is only required if the bool is set to true... And I want to validate the date if the bool is true. I have this expression so far... When(p => p.HasVisa == true, () => RuleFor(p =>…
Greg Quinn
  • 1,927
  • 1
  • 23
  • 26
19
votes
1 answer

Custom message with fluent validation collection

I am using the SetCollectionValidator for a generic collection. My collection is a list of: public class Answer { public string QuestionConst { get; set; } public string QuestionName { get; set; } public bool Required { get; set; } public…
zgirod
  • 4,189
  • 3
  • 28
  • 36
19
votes
3 answers

How to validate a string as DateTime using FluentValidation

With FluentValidation, is it possible to validate a string as a parseable DateTime without having to specify a Custom() delegate? Ideally, I'd like to say something like the EmailAddress function, e.g.: RuleFor(s =>…
19
votes
4 answers

FluentValidation multiple validators

Can I add more than one validator to an object? For example: public interface IFoo { int Id { get; set; } string Name { get; set; } } public interface IBar { string Stuff { get; set; } } public class FooValidator :…
Bjarki Heiðar
  • 3,117
  • 6
  • 27
  • 40
19
votes
4 answers

How do you validate against each string in a list using Fluent Validation?

I have an MVC3 view model defined as: [Validator(typeof(AccountsValidator))] public class AccountViewModel { public List Accounts { get; set; } } With the validation defined using FluentValidation (v3.3.1.0) as: public class…
Dangerous
  • 4,818
  • 3
  • 33
  • 48
18
votes
2 answers

FluentValidation allow null OR specify length?

I have a rule like this: RuleFor(m => m.Title).Length(1, 75); However, if the Title is null, I still get the validation stating the Title length must be between 1 and 75 characters, you entered 0. How can I change the rule so it allows for null…
BBauer42
  • 3,549
  • 10
  • 44
  • 81
18
votes
1 answer

FluentValidation NotEmpty and EmailAddress example

I am using FluentValidation with a login form. The email address field is Required and Must be a valid email address. I want to display a custom error message in both cases. The code I have working is: RuleFor(customer => customer.email) …
Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
17
votes
2 answers

ASP.net MVC Validation Hook

I have the following view in ASP.net MVC 3: @model Models.CreateProjectViewModel