Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Model validation in single EditForm in Server-Side Blazor

I have a form that binds to three related models in a single EditForm. I am looking to understand how to validate each of them on the same submit. I have been able to successfully validate a single model, but I don't see any details anywhere on how to validate multiples. Ideas?

    <EditForm OnValidSubmit="@Save" EditContext="@EditContext">
    <div class="form-group">

                <input class="form-control" type="text" id="Title" @bind="@TargetUser.Title" />

                <InputText Id="OfficePhone" Class="form-control" @bind-Value="@TargetUser.OfficePhone"></InputText>
                <ValidationMessage For="@(() => TargetUser.OfficePhone)" />

                <input class="form-control" type="text" id="MiddleName" @bind="@TargetUser.MiddleName" />
        <div class="row row-padding">
            <h4>Seller Rates</h4>
        </div>
        <hr />
        <input type="number" step="0.01" id="HourlyRate" @bind="@UserRate.HourlyRate" class="form-control" />
        <input type="number" id="Salary" @bind="@UserRate.Salary" class="form-control" />
        <input type="number" step="0.01" id="OTRate" @bind="@UserRate.OTRate" class="form-control" />
        <input type="date" @bind="@UserRate.ValidFrom" id="ValidFrom" class="form-control"/>
        <input type="date" class="form-control" id="ValidTo" @bind="@UserRate.ValidTo" />

    <DataAnnotationsValidator />
    <ValidationSummary />
</EditForm>

This is a highly edited example of some of the code. Not intended to show what would actually be there. Just to illustrate.

like image 328
Aaron Rumford Avatar asked Jul 01 '26 17:07

Aaron Rumford


1 Answers

I guess what you need here is the ObjectGraphDataAnnotationsValidator component, which enables validation of complex types.

Here's a link to a simple sample

Here's a link to the class definition and samples by the Blazor team

Hope this helps...

like image 151
enet Avatar answered Jul 04 '26 13:07

enet