Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mrpmorris opened this issue Aug 22, 2019 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation

Comments

@mrpmorris
Copy link

mrpmorris commented Aug 22, 2019

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
Copy link
Author

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

@pranavkm pranavkm added area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation labels Aug 22, 2019
@pranavkm
Copy link
Contributor

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

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation
Projects
None yet
Development

No branches or pull requests

2 participants