RadzenRequiredValidator firing on duplicate property names #340

Closed
opened 2026-01-29 17:35:41 +00:00 by claunia · 7 comments
Owner

Originally created by @RangerPascalJ on GitHub (Mar 10, 2022).

Hi everyone,

I have a problem using the RadzenRequiredValidator.
When I use multiple input-fields with the same property name, then all validators with that property name will fire when I change one.
See this as an example:
image

When I change the value of the field "PhonePrefix" from null (unset) to 1 (or any other value) the other validators are fired. That's not what I want. The component names of the validators and the names of the RadzenTextBoxes match each other, but are all different. Even with different classes, for example PhoneNumberWithCountryCode the error occurs.

Here is my full source of Index.razor:
@page "/"
@using System.ComponentModel.DataAnnotations

<PageTitle>Index</PageTitle>

<h1>ValidatorTest</h1>

<h3>@_formIsValidText</h3>
<br />
<br />

<RadzenTemplateForm TItem="ContactData" Data="_contactData" Submit="FormSubmit" InvalidSubmit="InvalidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />

<span style="display: flex;">
    <span style="width: 50%; display: grid;">
        <RadzenLabel Text="@("PhonePrefix *")" />
        <RadzenTextBox @bind-Value="_contactData.Phone.Prefix" Name="PhonePrefix" />
        <RadzenRequiredValidator Component="PhonePrefix" Text="PhonePrefixIsRequired" />
    </span>

    <span style="width: 50px; display: grid;"></span>

    <span style="width: 50%; display: grid;">
        <RadzenLabel Text="@("PhoneNumber *")" />
        <RadzenTextBox @bind-Value="_contactData.Phone.Number" Name="PhoneNumber" />
        <RadzenRequiredValidator Component="PhoneNumber" Text="PhoneNumberIsRequired" />
    </span>
</span>
<br />

<span style="display: flex;">
    <span style="width: 50%; display: grid;">
        <RadzenLabel Text="@("FaxPrefix")" />
        <RadzenTextBox @bind-Value="_contactData.Fax.Prefix" Name="FaxPrefix" />
    </span>

    <span style="width: 50px; display: grid;"></span>

    <span style="width: 50%; display: grid;">
        <RadzenLabel Text="@("FaxNumber")" />
        <RadzenTextBox @bind-Value="_contactData.Fax.Number" Name="FaxNumber" />
    </span>
</span>
<br />

<span style="display: flex;">
    <span style="width: 50%; display: grid;">
        <RadzenLabel Text="@("MobilePrefix *")" />
        <RadzenTextBox @bind-Value="_contactData.Mobile.Prefix" Name="MobilePrefix" />
        <RadzenRequiredValidator Component="MobilePrefix" Text="MobilePrefixIsRequired" />
    </span>

    <span style="width: 50px; display: grid;"></span>

    <span style="width: 50%; display: grid;">
        <RadzenLabel Text="@("MobileNumber *")" />
        <RadzenTextBox @bind-Value="_contactData.Mobile.Number" Name="MobileNumber"  />
        <RadzenRequiredValidator Component="MobileNumber" Text="MobileNumberIsRequired" />
    </span>
</span>
<br/>
    
<span style="display: flex;">
    <span style="width: 50%; display: grid;">
        <RadzenLabel Text="@("InternationalPhonePrefix *")" />
        <RadzenTextBox @bind-Value="_contactData.InternationalPhone.Prefix" Name="InternationalPhonePrefix" />
        <RadzenRequiredValidator Component="InternationalPhonePrefix" Text="InternationalPhonePrefixIsRequired" />
    </span>

    <span style="width: 50px; display: grid;"></span>

    <span style="width: 50%; display: grid;">
        <RadzenLabel Text="@("InternationalPhoneNumber *")" />
        <RadzenTextBox @bind-Value="_contactData.InternationalPhone.Number" Name="InternationalPhoneNumber"  />
        <RadzenRequiredValidator Component="InternationalPhoneNumber" Text="InternationalPhoneNumberIsRequired" />
    </span>
</span>
<br/>

<RadzenButton Text="Submit" ButtonType="ButtonType.Submit" />

</RadzenTemplateForm>

@code
{
private readonly ContactData _contactData = new();
private string _formIsValidText = "Form not yet submitted";

private void FormSubmit()
{
    _formIsValidText = "Form is valid!";
}

private void InvalidSubmit()
{
    _formIsValidText = "Form is invalid!";
}

public class ContactData
{
    public PhoneNumber Phone { get; set; } = new();
    public PhoneNumber Fax { get; set; } = new();
    public PhoneNumber Mobile { get; set; } = new();
    public PhoneNumberWithCountryCode InternationalPhone { get; set; } = new();
}

public class PhoneNumber
{
    public string Prefix { get; set; }
    public string Number { get; set; }
}

public class PhoneNumberWithCountryCode
{
    public string CountryCode { get; set; }
    public string Prefix { get; set; }
    public string Number { get; set; }
}

}

Originally created by @RangerPascalJ on GitHub (Mar 10, 2022). Hi everyone, I have a problem using the RadzenRequiredValidator. When I use multiple input-fields with the same property name, then all validators with that property name will fire when I change one. See this as an example: ![image](https://user-images.githubusercontent.com/101333324/157660834-38490089-4db3-4cff-b624-596e9182c345.png) When I change the value of the field "PhonePrefix" from null (unset) to 1 (or any other value) the other validators are fired. That's not what I want. The component names of the validators and the names of the RadzenTextBoxes match each other, but are all different. Even with different classes, for example PhoneNumberWithCountryCode the error occurs. Here is my full source of Index.razor: \@page "/" \@using System.ComponentModel.DataAnnotations \<PageTitle>Index\</PageTitle> \<h1>ValidatorTest\</h1> \<h3>@_formIsValidText\</h3> \<br /> \<br /> \<RadzenTemplateForm TItem="ContactData" Data="_contactData" Submit="FormSubmit" InvalidSubmit="InvalidSubmit"> \<DataAnnotationsValidator /> \<ValidationSummary /> <span style="display: flex;"> <span style="width: 50%; display: grid;"> <RadzenLabel Text="@("PhonePrefix *")" /> <RadzenTextBox @bind-Value="_contactData.Phone.Prefix" Name="PhonePrefix" /> <RadzenRequiredValidator Component="PhonePrefix" Text="PhonePrefixIsRequired" /> </span> <span style="width: 50px; display: grid;"></span> <span style="width: 50%; display: grid;"> <RadzenLabel Text="@("PhoneNumber *")" /> <RadzenTextBox @bind-Value="_contactData.Phone.Number" Name="PhoneNumber" /> <RadzenRequiredValidator Component="PhoneNumber" Text="PhoneNumberIsRequired" /> </span> </span> <br /> <span style="display: flex;"> <span style="width: 50%; display: grid;"> <RadzenLabel Text="@("FaxPrefix")" /> <RadzenTextBox @bind-Value="_contactData.Fax.Prefix" Name="FaxPrefix" /> </span> <span style="width: 50px; display: grid;"></span> <span style="width: 50%; display: grid;"> <RadzenLabel Text="@("FaxNumber")" /> <RadzenTextBox @bind-Value="_contactData.Fax.Number" Name="FaxNumber" /> </span> </span> <br /> <span style="display: flex;"> <span style="width: 50%; display: grid;"> <RadzenLabel Text="@("MobilePrefix *")" /> <RadzenTextBox @bind-Value="_contactData.Mobile.Prefix" Name="MobilePrefix" /> <RadzenRequiredValidator Component="MobilePrefix" Text="MobilePrefixIsRequired" /> </span> <span style="width: 50px; display: grid;"></span> <span style="width: 50%; display: grid;"> <RadzenLabel Text="@("MobileNumber *")" /> <RadzenTextBox @bind-Value="_contactData.Mobile.Number" Name="MobileNumber" /> <RadzenRequiredValidator Component="MobileNumber" Text="MobileNumberIsRequired" /> </span> </span> <br/> <span style="display: flex;"> <span style="width: 50%; display: grid;"> <RadzenLabel Text="@("InternationalPhonePrefix *")" /> <RadzenTextBox @bind-Value="_contactData.InternationalPhone.Prefix" Name="InternationalPhonePrefix" /> <RadzenRequiredValidator Component="InternationalPhonePrefix" Text="InternationalPhonePrefixIsRequired" /> </span> <span style="width: 50px; display: grid;"></span> <span style="width: 50%; display: grid;"> <RadzenLabel Text="@("InternationalPhoneNumber *")" /> <RadzenTextBox @bind-Value="_contactData.InternationalPhone.Number" Name="InternationalPhoneNumber" /> <RadzenRequiredValidator Component="InternationalPhoneNumber" Text="InternationalPhoneNumberIsRequired" /> </span> </span> <br/> <RadzenButton Text="Submit" ButtonType="ButtonType.Submit" /> \</RadzenTemplateForm> @code { private readonly ContactData _contactData = new(); private string _formIsValidText = "Form not yet submitted"; private void FormSubmit() { _formIsValidText = "Form is valid!"; } private void InvalidSubmit() { _formIsValidText = "Form is invalid!"; } public class ContactData { public PhoneNumber Phone { get; set; } = new(); public PhoneNumber Fax { get; set; } = new(); public PhoneNumber Mobile { get; set; } = new(); public PhoneNumberWithCountryCode InternationalPhone { get; set; } = new(); } public class PhoneNumber { public string Prefix { get; set; } public string Number { get; set; } } public class PhoneNumberWithCountryCode { public string CountryCode { get; set; } public string Prefix { get; set; } public string Number { get; set; } } }
Author
Owner

@akorchev commented on GitHub (Mar 10, 2022):

Does it work with the built-in Blazor validation? This is what Radzen uses under the hood.

@akorchev commented on GitHub (Mar 10, 2022): Does it work with the built-in Blazor validation? This is what Radzen uses under the hood.
Author
Owner

@RangerPascalJ commented on GitHub (Mar 10, 2022):

You mean the EditForm-Tag instead of RadzenTemplateForm? Yes, this works well. But I don't want to attribute all my properties. So actually this is not a option for us.

P.S. Sorry for the bad formatting in the description. I will try to fix that =D

@RangerPascalJ commented on GitHub (Mar 10, 2022): You mean the EditForm-Tag instead of RadzenTemplateForm? Yes, this works well. But I don't want to attribute all my properties. So actually this is not a option for us. P.S. Sorry for the bad formatting in the description. I will try to fix that =D
Author
Owner

@RangerPascalJ commented on GitHub (Mar 10, 2022):

We suspect it has something to do with the ValidatorBase class and the ValidateField method. The conversion with FieldName and FieldIdentifier is not clear to us.
image

@RangerPascalJ commented on GitHub (Mar 10, 2022): We suspect it has something to do with the ValidatorBase class and the ValidateField method. The conversion with FieldName and FieldIdentifier is not clear to us. ![image](https://user-images.githubusercontent.com/101333324/157671801-fdd21fc1-6b5c-476b-9745-d709b5861bb8.png)
Author
Owner

@akorchev commented on GitHub (Mar 10, 2022):

FieldIdentifier is initialized by the Blazor runtime. According to this by default it doesn't support nested properties: https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0#nested-models-collection-types-and-complex-types-1

@akorchev commented on GitHub (Mar 10, 2022): FieldIdentifier is initialized by the Blazor runtime. According to this by default it doesn't support nested properties: https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0#nested-models-collection-types-and-complex-types-1
Author
Owner

@RangerPascalJ commented on GitHub (Mar 11, 2022):

Hi again,
we made it work for us. We cloned your repo and changed the ValidateField-method as follows:

private void ValidateField(object sender, FieldChangedEventArgs args)
{
var component = Form.FindComponent(Component);
if (component == null)
return;

if (args.FieldIdentifier.Equals(component.FieldIdentifier))
{
    ValidateModel(sender, ValidationRequestedEventArgs.Empty);
}

}

With these changes, the other validators that have the same property name of another instance will no longer be fired.
It would be great if you check these changes and accept them if necessary.
Thanks very much.

@RangerPascalJ commented on GitHub (Mar 11, 2022): Hi again, we made it work for us. We cloned your repo and changed the ValidateField-method as follows: private void ValidateField(object sender, FieldChangedEventArgs args) { var component = Form.FindComponent(Component); if (component == null) return; if (args.FieldIdentifier.Equals(component.FieldIdentifier)) { ValidateModel(sender, ValidationRequestedEventArgs.Empty); } } With these changes, the other validators that have the same property name of another instance will no longer be fired. It would be great if you check these changes and accept them if necessary. Thanks very much.
Author
Owner

@akorchev commented on GitHub (Mar 16, 2022):

You can perhaps prepare a pull request for us to review.

@akorchev commented on GitHub (Mar 16, 2022): You can perhaps prepare a pull request for us to review.
Author
Owner

@akorchev commented on GitHub (Jun 15, 2022):

Closing due to inactivity. Please feel free to open a pull request with the suggested fix/workaround for us to review.

@akorchev commented on GitHub (Jun 15, 2022): Closing due to inactivity. Please feel free to open a pull request with the suggested fix/workaround for us to review.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#340