Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
sipi41 opened this issue May 20, 2025 · 2 comments
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@sipi41
Copy link

sipi41 commented May 20, 2025

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

@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label May 20, 2025
@javiercn
Copy link
Member

@sipi41 thanks for contacting us.

Blazor does not support validation of complex object graphs. There is an experimental package https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/validation?view=aspnetcore-9.0#blazor-data-annotations-validation-package that you can for older versions.

This feature will be available in .NET 10 via integration with the new Validation API support that is being added to ASP.NET Core.

@javiercn javiercn added question ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels May 21, 2025
@sipi41
Copy link
Author

sipi41 commented May 21, 2025

SOLUTION: (at least for now)

a) Install Data Annotations Validation package
b) use on EditForm
c) Decorate complex property with [ValidateComplexType] attribute.

NOTE: To use, [ValidateComplexType] the project where the class resides, should accept the installation for the "Data Annotations Validation package", for example if the class resides in a "Class Library" project, you will not be able to install such package and therefore, will not be able to use the decorator.

@sipi41 sipi41 closed this as completed May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants