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.)
I think it is because the type of property is decimal and .net core is not accepting the request. Fluent validation even not acting on that.
Try this.
services.AddControllersWithViews()
.AddFluentValidation(options =>
{
options.RegisterValidatorsFromAssembly(Assembly.GetEntryAssembly());
options.ImplicitlyValidateChildProperties = true;
options.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.