0

I thought this was going to be pretty straight forward...

I have this decimal property:

[Column(TypeName = "decimal(18,2)")]
public decimal ProjectBudget { get; set; }

I want to validate the input to be a positive number. These are the two rules I have tried:

RuleFor(x => x.ProjectBudget.ToString())
    .Matches("^[0-9]*$").WithMessage("Only positive numbers, please.");

RuleFor(x => x.ProjectBudget)
    .GreaterThanOrEqualTo(0).WithMessage("Only positive numbers, please.");

In both cases, if I enter "sdfhj", this is the error message I get:

The value 'sdfhj' is not valid for ProjectBudget.

That's the error message from Asp.Net Core's built in validation.

Why doesn't my error message "Only positive numbers, please." appear?

(I know I can specify the error message on the property using something like [Range(0, (double)decimal.MaxValue, ErrorMessage = "Only positive numbers, please.")], but that's not what I'm asking.)

Stian
  • 1,522
  • 2
  • 22
  • 52
  • Post the full exception text and the code that performs the validation. FluentValidation doesn't use a *different* validation framework. The property you posted has no validation attributes. `TypeName` specifies the database type name, not some kind of restriction. In any case, `decimal` allows negative numbers. The validation error came from somewhere else – Panagiotis Kanavos Nov 07 '19 at 10:55
  • Where do you get that message? Server-side exception or client-side message? Are you using ASP.NET Core MVC or Razor Pages? The sections [Specifying a RuleSet for client-side messages](https://fluentvalidation.net/start#creating-your-first-validator) and [Use with Page Models](https://fluentvalidation.net/aspnet#use-with-page-models) in the [ASP.NET Integration](https://fluentvalidation.net/aspne) page explain there are some quirks – Panagiotis Kanavos Nov 07 '19 at 11:01
  • @PanagiotisKanavos There is no exception. None of my other properties have validation attributes, and they are validated as expected. – Stian Nov 07 '19 at 11:01
  • @PanagiotisKanavos I'm using MVC. The validation is server side. – Stian Nov 07 '19 at 11:02
  • And the `RuleSetForClientSideMessages` attribute? Have you checked the ASP.NET Integration page? How are the validators used? – Panagiotis Kanavos Nov 07 '19 at 11:03
  • If you check the test project in FluentValidator's Github repo you'll see that [the client-side test controller](https://github.com/JeremySkinner/FluentValidation/blob/master/src/FluentValidation.Tests.AspNetCore/Controllers/ClientsideController.cs) uses the `RuleSetForClientSideMessages` attribute for all actions. You an use this as a guide to find out what's missing in your project. Worst case, you can clone the repo and run the tests to see whether they fail – Panagiotis Kanavos Nov 07 '19 at 11:12
  • PS: I forgot you can use [WebApplicationFactory](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.0) to create and test controllers from the "client" side, the way FluentValidator does. You can use this to troubleshoot your issue without starting the app each time. Now I'm going to add this to *my* unit tests – Panagiotis Kanavos Nov 07 '19 at 11:16
  • @Stian: I had kind of issue with my api project, then I found the cause. Model validation is enabled by default, in my case I have disable it from the startup i.e `SuppressModelStateInvalidFilter = true`. – Divyang Desai Nov 07 '19 at 11:32
  • Take a look at this [answer](https://stackoverflow.com/a/41669552/219933) . The error message you are seeing is because of another error, the data conversion error, not the validation error which happens after successful conversion. – Mat J Nov 07 '19 at 11:35

1 Answers1

0

Try this.


            services.AddControllersWithViews()
               .AddFluentValidation(options =>
                {
                   options.RegisterValidatorsFromAssembly(Assembly.GetEntryAssembly());
                   options.ImplicitlyValidateChildProperties = true;
                   options.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
                });

Sefa Can
  • 37
  • 4