dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

Blazor <InputText> is not validating for childs in a loop #28117

Closed M4tthiasStahl closed 3 years ago

M4tthiasStahl commented 3 years ago

In my Blazor server side project the is not validating [Required] in my Child (List) element. The same is working instead for .

Sample:

public class Person
{
     public List<Address> Addresses {get; set; }
}

public class Address
{
     [Required]
     public int MyNumber  {get; set; }

     [Required]
     public string MyText  {get; set; }
}

<EditForm Model="@person" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
    <DataAnnotationsValidator />
    <div class="saveButton">
        <button class="btn btn-primary" type="submit">Save</button>
    </div>

             @foreach (var address in person.Addresses)
            {
                         // Validation is working
                        <InputNumber class="form-control" @bind-Value="address.MyNumber" />

                          // Validation is NOT working
                         <InputText class="form-control" @bind-Value="address.MyText" />
            }
</EditForm>
javiercn commented 3 years ago

@M4tthiasStahl thanks for contacting us.

You need to use the ObjectGraphDataAnnotationsValidator , see details here

If you still run into issues after following the provided guidance, please let us know.

ghost commented 3 years ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.