Skip to content

Allow FieldIdentifier to be passed to InputBase and ValidationMessage #16627

@nguyenvanbien95

Description

@nguyenvanbien95

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-fewThis issue impacts only small number of customersarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-builtin-componentsFeatures related to the built in components we ship or could ship in the futureseverity-majorThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions