Skip to content

Validation not firing when pressing submit button on nested numeric property #62039

Closed
@sipi41

Description

@sipi41

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Dear friends, I would like to report what I consider a bug in the blazor model validation when using InputNumber in a nested object.

Consider the following simple object composed of 2 classes as follow:

public class ObjectInv
{
public string id { get; set; }
[Required(ErrorMessage ="Product Name is mandatory!", AllowEmptyStrings =false)]
public string name { get; set; }
public Data? data { get; set; } = null;
}

and the property of type " Data" is defined as follows:

public class Data
{
.... MORE PROPS....

[  Required(ErrorMessage = "Product price cannot be blank!", AllowEmptyStrings =false), Range(0, 99.99, ErrorMessage ="Price must be between {1} and {2}!!")]
public double? price { get; set; } 

.... MORE PROPS....

}

The expected behavior is that when I press the submit button in the form, it fires the validation as shown in the following picture:

Image

Unfortunately when I press the submit button (in this case, the " add me!" button) only the property in the main object (Product Name) is validated, the other one, is completely ignored, as shown below:

Image

The validation message is shown only if I manually input for example a negative number or remove a number and/or use the up/down number selector on the right of the control... but as soon as I press the submit button, it voids the error message and says all is good, as shown below

Image
and when I press the button, the Product Price says to be ok... even knowing that before it was saying it was not...
Image

I tried to use DataAnnotationsValidator and the ObjectGraphDataAnnotationsValidator but the behavior is the same, tried also v7 server side, not 8, but the behavior is there too.

I created another property, a copy of the actual price property from the Data object, and put it in the root of the main object, and there it behaves correctly, in the following example, you will see 2 prices, the first one is the problematic, and the other one is a property called price2, in the root

Image

Expected Behavior

The expected behavior is that the validation should fire correctly for numeric properties in the root object and should also work inside a nested property (in this case, Data > price ) when we press the submit button in the form as shown in the following image:

Image

Steps To Reproduce

Please download the project files, run the project and try to validate PRODUCT PRICE (price) located in the Data object of the root object.

BlazorSampleApp1.zip

Exceptions (if any)

No response

.NET Version

8 and 7

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions