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.21k stars 9.95k forks source link

Add validation for object in list #25609

Closed MarvinKlein1508 closed 4 years ago

MarvinKlein1508 commented 4 years ago

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

I have a model e.g. Order. This Model constis of a property and a List of Customer. I have installed the latest preview of Microsoft.AspNetCore.Blazor.DataAnnotations.Validation

Validating the models works fine. However, I do not show all items of the list by once. To Load a customer, the user needs to click on an item in the table. Now the details of the item are shown to the user and he/she can edit it. However, if the user edits an item and has a validation error, and switches to another item, it is unclear for the user where the validation error occurs.

I have created a good example which can be found here: https://github.com/MarvinKlein1508/BlazorObjectValidation

Describe the solution you'd like

A simple solution would be to add functionaltity to <ValidationMessage />

Lets say I have the following code: ` table class="table"

Firstname Lastname
    <tbody>
        @foreach (var customer in order.Customers)
        {
            <tr style="cursor: pointer;" @onclick="() => order.SelectedCustomer = customer">
                <td>
                    @customer.Firstname

                    <ValidationMessage For="customer">
                        Invalid data found.
                    </ValidationMessage>
                </td>
                <td>@customer.Lastname</td>
            </tr>
        }
    </tbody>
</table>`

As you can see, I bind the object of the list to my message. If I do this, this section should be shown when any validation error in this type occurs. In addition you should be able to a custom validation message. Perhaps people only want to display an error icon. If you don't specify an optional message it could show all validation errors of the object just as ValidationSummary does.

javiercn commented 4 years ago

@MarvinKlein1508 thanks for contacting us.

We believe you want something like what ValidationSummary offers by passing in the object you want to show validation messages for as the Model.