Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentValidation on number issue

I am using FluentValidation in my ASP.NET MVC 3 application.

I have a MaxNumberTeamMembers property in my view model as such:

/// <summary>
/// Gets or sets the maximum number of team members.
/// </summary>
public int MaxNumberTeamMembers { get; set; }

I want to know if the following ruleset is possible:

  • On the front end view, if the textbox is empty then I want a "MaxNumberTeamMembers is required" message to be displayed
  • If the number entered is less than 1 then I want a message to display "MaxNumberTeamMembers should be greater or equal to 1".

What would the ruleset for the above look like?

I have the following but it does not work on the GreaterThan part if I enter 0:

RuleFor(x => x.MaxNumberTeamMembers)
     .NotEmpty()
     .WithMessage("Max. number of team members is required")
     .GreaterThan(0)
     .WithMessage("Max. number of team members must be greater than 0");

UPDATE 2011-02-14:

RuleFor(x => x.MinNumberCharactersCitation)
   .NotNull()
   .WithMessage("Min. number of characters for citation is required")
   .GreaterThanOrEqualTo(1)
   .WithMessage("Min. number of characters for citation must be greater than or equal to 1")
   .LessThanOrEqualTo(x => x.MaxNumberCharactersCitation)
   .WithMessage("Min. number of characters must be less than or equal to max. number of characters");
like image 310
Brendan Vogt Avatar asked Feb 09 '11 11:02

Brendan Vogt


People also ask

Does fluentvalidation work with ASP NET?

For automatic validation with ASP.NET, FluentValidation supports ASP.NET running on .NET Core 3.1, .NET 5 or .NET 6. If you’re new to using FluentValidation, check out the Creating your first validator page.

What is the default check type in fluentvalidation?

In FluentValidation 9, the ASP.NET Core-compatible “simple” check is the default mode. In FluentValidation 8.x (and older), the Regex mode is the default. Checks whether a string property could be a valid credit card number.

What platforms does fluentvalidation 10 support?

FluentValidation 10 supports the following platforms: .NET Core 3.1 For automatic validation with ASP.NET, FluentValidation supports ASP.NET running on .NET Core 3.1, .NET 5 or .NET 6.

Is regex-based email validation supported in fluentvalidation?

Note that this approach is deprecated and will generate a warning as regex-based email validation is not recommended. In FluentValidation 9, the ASP.NET Core-compatible “simple” check is the default mode. In FluentValidation 8.x (and older), the Regex mode is the default.


2 Answers

If you want to handle the empty case you need a nullable integer on your model because otherwise it is the default model binder that will automatically add a validation error when it tries to parse the empty string to a non-nullable integer:

public int? MaxNumberTeamMembers { get; set; }

and then you could have the following validation rules on this property:

RuleFor(x => x.MaxNumberTeamMembers)
    .NotEmpty()
    .WithMessage("Max. number of team members is required")
    .Must(x => x.Value > 0)
    .When(x => x.MaxNumberTeamMembers != null)
    .WithMessage("Max. number of team members must be greater than 0");


UPDATE:

The following works fine with the latest version of FluentValidation:

RuleFor(x => x.MaxNumberTeamMembers)
    .NotNull()
    .WithMessage("Max. number of team members is required")
    .GreaterThan(0)
    .WithMessage("Max. number of team members must be greater than 0");
like image 184
Darin Dimitrov Avatar answered Nov 11 '22 09:11

Darin Dimitrov


it is work with FluentValidation version 3.2

RuleFor(x => x.MaxNumberTeamMembers)
    .NotNull()
    .WithMessage("Please Enter Value")
    .InclusiveBetween(1, 500)
    .WithMessage("Value must be number Beetween 1 , 500"); 
like image 43
Morteza Avatar answered Nov 11 '22 08:11

Morteza