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.)