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 EditForm does not validate all items in the form, and does not trigger the correct validation events. #13345

Closed mrpmorris closed 5 years ago

mrpmorris commented 5 years ago

Describe the bug

EditForm only validates the object in its Model parameter. If the object is a complex object (such as a Person with an Address) its complex properties are not also validated.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core '3.0.0-preview8.19405.7
  2. Create a new Blazor app
  3. Replace the contents of index.razor with the following mark-up
@page "/"
@using System.ComponentModel.DataAnnotations

@if (LastSubmissionStatus != null)
{
    <h1>Last submission at @LastSubmissionTime.ToString("hh:mm:ss") was @LastSubmissionStatus</h1>
}

<EditForm Model=Person1 OnValidSubmit=ValidFormSubmitted OnInvalidSubmit=InvalidFormSubmitted>
    <DataAnnotationsValidator/>
    <ValidationSummary/>
    Person name <InputText @bind-Value=Person1.Name/><br/>
    Country <InputText @bind-Value=Person1.HomeAddress.Country/><br/>
    <input type="submit" value="Validate"/>
</EditForm>

@code {
    Person Person1;
    string LastSubmissionStatus;
    DateTime LastSubmissionTime;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Person1 = new Person
        {
            Name = null,
            HomeAddress = new Address
            {
                Country = null
            }
        };
    }

    void ValidFormSubmitted(EditContext context)
    {
        LastSubmissionTime = DateTime.Now;
        LastSubmissionStatus = "valid";
    }

    void InvalidFormSubmitted(EditContext context)
    {
        LastSubmissionTime = DateTime.Now;
        LastSubmissionStatus = "invalid";
    }

    class Person
    {
        [Required]
        public string Name { get; set; }
        public Address HomeAddress { get; set; }
    }

    class Address
    {
        [Required]
        public string Country { get; set; }

    }
}

5: Run the app 6: Enter a value for Name 7: Click the Validate button

Expected: An invalid form, with Country highlighted Actual: OnValidSubmit is executed

8: Enter a value for Country 9: Click Validate 10: Delete the value for Country 11: Click Validate

Expected: OnInvalidSubmit should be executed Actual: It is not, nor is OnValidSubmit

mrpmorris commented 5 years ago

Note: Code to reproduce is here -> https://pastebin.com/0TafVse6

pranavkm commented 5 years ago

Thanks to for your issue report @mrpmorris. This is a duplicate of #10526. We'll use the other issue to track fixing this.