Skip to content

Add and document the pattern for forms and validation in components with collections of items #8189

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
rynowak opened this issue Mar 4, 2019 · 4 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Needs: Design This issue requires design work before implementating.

Comments

@rynowak
Copy link
Member

rynowak commented Mar 4, 2019

Consider a model like:

public class Person
{
    public string Name { get; set; }

    public List<Contact> EmergencyContacts { get; set;  }
}

public class EmergencyContact
{
    public string Name { get; set; }

    public string PhoneNumber { get; set; }
}

How we would write a form that lets you add a dynamic number of emergency contacts? I tried this during some app building time, and there are some problems with it that we could make better.

Issue 1

Doing the most obvious thing with a collection throws an IndexOutOfRangeException.

image

This works fine if you do it like this:

image

Issue 2

Validation doesn't automatically traverse the members of something so you have to manually add validation support for any sattelite objects you want validated.

Example: https://github.com/rynowak/ComponentFunTime/blob/master/Components/Shared/EmployeeEditor.razor#L85

@rynowak rynowak added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 4, 2019
@Andrzej-W
Copy link

@rynowak issue 1 is related to this original post:
aspnet/Blazor.Docs#371 (comment)
It was moved here:
dotnet/AspNetCore.Docs#10768
Unfortunately it is now closed and change in the doc is in my opinion to small - it only mentions lambda expressions. I can bet that people will have problems with binding also and your example is the best proof of it.

@SteveSandersonMS
Copy link
Member

Doing the most obvious thing with a collection throws an IndexOutOfRangeException.

One mitigating factor is that the really most obvious thing (foreach) actually does work.

Of course I do agree we should look for a good fix for the for case too though!

@mkArtakMSFT mkArtakMSFT added bug This issue describes a behavior which is not expected - a bug. 1 - Ready Needs: Design This issue requires design work before implementating. area-blazor Includes: Blazor, Razor Components labels Mar 6, 2019
@mkArtakMSFT mkArtakMSFT added this to the 3.0.0-preview5 milestone Mar 6, 2019
@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@danroth27
Copy link
Member

For the issue with closing over a loop variable we're considering adding an analyzer to detect that situation: #10476

@danroth27
Copy link
Member

I've filed #10526 to separately track the feedback that validation doesn't automatically traverse members. Closing this issue.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 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 bug This issue describes a behavior which is not expected - a bug. Needs: Design This issue requires design work before implementating.
Projects
None yet
Development

No branches or pull requests

6 participants