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

add validation for child components #20976

Closed fabianus76 closed 4 years ago

fabianus76 commented 4 years ago

Is your feature request related to a problem? Please describe.

The experimental package https://www.nuget.org/packages/Microsoft.AspNetCore.Blazor.DataAnnotations.Validation adds validation for the model's entire object graph - that's great ! BUT, this isn't fired with child components.

To illustrate:

This will work the street property of address:

Name:

Street:

But this won't work:

Name:

Describe the solution you'd like

Please add validation also for the case where the object graph is nested in child components.

mkArtakMSFT commented 4 years ago

Thanks for contacting us. Can you also show what the AddressForm component looks like?

fabianus76 commented 4 years ago

Hello mkArtakMSFT, on your request I prepared something and while doing this I understood my mistake: I used an EditForm in each component (one in the outer one and an additional one in the AddressForm componant). Once I got rid of the inner (AddressForm) EditForm everything worked out great. I apologize for the trouble.

fabianus76 commented 4 years ago

Finally the issue isn't solved - so here comes the sample. What is missing is the fact, that a change to the object graphe that happens in a child component is not reflected in the context of the parent component. Meaning that if the Street property is modified in the parent component context.IsModified() is true, but not if the same property is modified in the child component:

parent component:

@page "/test"

<EditForm Model="PersonItem"  OnValidSubmit="OnValidSubmit">
    <ObjectGraphDataAnnotationsValidator />  
    <ValidationSummary />  

    <p>
        Name: <TimInput @bind-Value="PersonItem.Name" />
        <ValidationMessage For=@(() => PersonItem.Name) />
    </p>

    <AddrForm AddrItem=@PersonItem.Address ></AddrForm>

    <p>
        Street: <TimInput @bind-Value=@PersonItem.Address.Street />
        <ValidationMessage For="@(() => PersonItem.Address.Street)" />
        <br/>
        <text>In this case any change in the input field leads to context.IsModified() == true</text>
    </p>

    @if(context.IsModified() && context.Validate())
    {
        <div class="mt-n2" >
            <button type="submit" class="btn btn-primary mr-2">Save</button>
        </div>
    }
</EditForm>

@code {
    public Person PersonItem { get; set; }

    protected override void OnInitialized()
    {
        PersonItem = new Person{Name = "Name1"};
        PersonItem.Address = new Addr{Street = "Strasse1"};
    }

    private void OnValidSubmit(){
    }
}

child component (AddrForm.razor):

<p>
    Street: <TimInput @bind-Value="AddrItem.Street" />
    <ValidationMessage For="@(() => AddrItem.Street)" />
    <br/>
    <text>In this case any change in the input field does not fire context.IsModified() == true</text>
</p>

@code {
    [Parameter] public Addr AddrItem { get; set; }
}

Person Class:

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

Addr Class:

public class Addr
    {
        [Required]
        public string Street { get; set; }
    }
}

and to be complete ...

TimInput.razor:

@inherits Microsoft.AspNetCore.Components.Forms.InputText
<input @attributes="@AdditionalAttributes" class="@CssClass" @bind="@CurrentValueAsString" @bind:event="oninput" />
mkArtakMSFT commented 4 years ago

@pranavkm let's make sure whatever concern is raised here is well understood so we can pass this information to whoever will own the Data validation going forward.

fabianus76 commented 4 years ago

@pranavkm let's make sure whatever concern is raised here is well understood so we can pass this information to whoever will own the Data validation going forward.

Hey @mkArtakMSFT, I am very willing to make things clearer. Could you tell me more about what has to be clarified ?

kristof12345 commented 4 years ago

Any update on this?

mkArtakMSFT commented 4 years ago

Hi. Thanks for contacting us. We're closing this issue as there was not much community interest in this ask. You can learn more about our triage process and how we handle issues by reading our Triage Process writeup.

pylvr commented 4 years ago

Any update on this Issue?