3
votes

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.

1
You can only use a single model... Do you mean a complex type ? I'll post the answer as if you mean a complex type... - enet
@enet What I mean is that the form is complex and I need to validate multiple types. See TargetUser and UserRate in the example above. Two separate types. What I am considering is to create a 'combined' type that I use as the model for the EditForm, then, on submit, create the separate EFCore processes for the individual types. Thoughts? - Aaron Rumford
@enet please add an answer of that so I can mark you for it. That is exactly what I am looking for. - Aaron Rumford

1 Answers

2
votes

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...