I’m getting odd behavior with my validation in my view.
My model has this property.
[Display(Name = "Overflow Capacity")]
[RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")]
[Range(0,9999.99,ErrorMessage = "Value must be between 0 - 9,999.99")]
public decimal OverFlowCapacity { get; set; }
My view has this:
<tr>
<td>@Html.LabelFor(m=> m.OverFlowCapacity)</td>
<td>@Html.EditorFor(m=>m.OverFlowCapacity)</td>
<td> @Html.ValidationMessageFor(model => model.OverFlowCapacity)</td>
</tr>
If I enter a value like ‘ABC’, I get the validation message ‘Number required’
If I enter a value of 999999, I get the validation message ‘Value must be between 0 – 9,999.99’
Both of those messages are received when I tab off the text box as expected.
When I leave the text box value empty and tab off, I get no errors, as expected.
However, when I submit, I get a validation message ‘The Overflow Capacity field is required.’
I don’t know where this is coming from. I’ve tried removing all validation attributes from the model, and still get the ‘required’ message. I’m at a loss.
Here are the scripts I’ve referenced.
I have other issues with mvcfoolproof that I may post later. I’m wondering if this isn’t somehow responsible for my problems.
What’s happening to you now is the post validation is kicking in after the form has been submitted and determining that the
decimalvalue cannot benull. Right now you are using adecimaltype which is non-nullable. If you want this behavior and you want to see the validation before you submit the form then add the[Required]attribute to the property. However if you don’t want this functionality and it can possibly benull, then change your type fromdecimaltodecimal?orNullable<decimal>.Don’t allow nulls and have the pre-submit validation:
Allow nulls and get rid of post-submit validation error: