Home > Software engineering >  Setting DisplayName on InputNumber control in Blazor form is NOT used in ErrorMessage upon submit/va
Setting DisplayName on InputNumber control in Blazor form is NOT used in ErrorMessage upon submit/va

Time:10-19

Upon validation of my SUBMIT for the CRUD page...

<EditForm Model="CustomerData" OnValidSubmit="HandleValidSubmit" OnInvalidSubmit="HandleInvalidSubmit">
   <DataAnnotationsValidator />
   <ValidationSummary />

I get this ERROR for a value of 0 (zero) -- which does NOT show the field's "DisplayName":

'ID_CUSTOMER's value must be greater or equal to 1.

The InputNumber control with DisplayName is:

<InputNumber  
@bind-Value="CustomerData.ID_CUSTOMER" DisplayName="FCI Key" />

The data-model field is defined as follows:

[Required(ErrorMessage = "'{0}' is required.")]
[Range(1, 9999, ErrorMessage = "'{0}'s value must be greater or equal to {1}.")]
public int ID_CUSTOMER { get; set; }

As you can see the ErrorMessage shows the model bound field-name (ID_CUSTOMER) not the DisplayName (FCI Key).

The reason I did NOT enter the [DisplayName...] in the model, is that I may use a different DisplayName based on a user's role in the application.

So I suppose my question is...Why is the InputNumber "DisplayName" NOT being used in the validation ErrorMessage {0} substitution?

Thanks...John

CodePudding user response:

You may misunderstand the InputBase.DisplayName. This attribute can change the error message field name with InputBase build-in error. For example, InputNumber must be a number, InputDate must be a date. If you do not fill in with a number/date, it will generate the error message with DisplayName. InputBase.DisplayName cannot make effect on the Data Annotation. Also this attribute supports from .net 5.

Refer to:

Display name support

  • Related