8

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.

4
  • 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... Commented Jan 28, 2020 at 21:55
  • @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? Commented Jan 28, 2020 at 22:08
  • See this: gunnarpeipman.com/… Commented Jan 28, 2020 at 22:38
  • @enet please add an answer of that so I can mark you for it. That is exactly what I am looking for. Commented Jan 28, 2020 at 22:47

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have a similar problem: my form is bound to 2 different models (Request and Systems) and I have to validate against both models.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.