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.37k stars 9.99k forks source link

Allow FieldIdentifier to be passed to InputBase and ValidationMessage #16627

Closed nguyenvanbien95 closed 2 years ago

nguyenvanbien95 commented 4 years ago

Thank you for your effors to build awesome Blazor. I want to create dynamic form using Reflection for TValue: Value as:

<EditForm Model="Value" OnValidSubmit="OnvalidSubmit" OnInvalidSubmit="OnInValidSubmit" class="@formStyle">
                <DataAnnotationsValidator />
                @{
                    var properties = Value.InputFields;
                    @foreach (var p in properties)
                    {
                        var att = p.GetCustomAttributes(false);
                        if (!att.Any(c => c is HiddenAttribute))
                        {
                            var nameAttribute = (DisplayAttribute)att.FirstOrDefault(c => c is DisplayAttribute);
                            var value = p.GetValue(Value);

                                <div class="form-group">
                                    @if (nameAttribute != null)
                                    {
                                        <label for="@p.Name">
                                            @nameAttribute.Name
                                            @if (att.Any(c => c is RequiredAttribute))
                                            {
                                                <font color="red">(*)</font>
                                            }
                                        </label>
                                    }
                                    <InputText class="form-control" Value="@value?.ToString()" ValueExpression="@(() => p.Name)" ValueChanged="(e => p.SetValue(Value, e))" />

                                    <ValidationMessage For="@(() => p)"></ValidationMessage>
                                </div>
                        }
                    }
                }
                <button type="submit" class="btn btn-primary">Click me</button>
            </EditForm>

In MVC, i found a way for that Here.

I think ValidationMessage can support PropertyInfo <ValidationMessage Model="@Value" NameField="p.Name"></ValidationMessage>

javiercn commented 4 years ago

@nguyenvanbien95 Thanks for contacting us.

What is that you are trying to accomplish that doesn't work the way you expect?

pranavkm commented 4 years ago

a) <ValidationSummary Model="value" /> gets you some of the way, but the correct way to fix this would be to add the ability to specify a FieldIdentifier onValidationMessage: <ValidationMessage Field="EditContext.Field(property.Name)" TValue="object" />

The unfortunate part here is that TValue needs to be specified and has to be compile time expression for the generated code to correctly instantiate ValidationMessage<TValue>. However, since it's only ever used to constrain the For expression, and has no practical use in the component outside of it, using object works fairly well.

b) We'd also need for a way for a FieldIdentifier to be assigned to InputBase. The FieldIdentifier calculated using ValueExpression is used to construct the FieldIdentifier for field validation - https://github.com/aspnet/AspNetCore/blob/master/src/Components/Web/src/Forms/InputBase.cs#L203.

nguyenvanbien95 commented 4 years ago

This is just my temporary solution. I hope there is a better way to implement it in code base. Thank you for your detail explaining.

mkArtakMSFT commented 2 years ago

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