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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With