Skip to content

Blazor EditForm does not validate all items in the form, and does not trigger the correct validation events. #13345

Closed
@mrpmorris

Description

@mrpmorris

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThis issue tracks updating documentationarea-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions