Skip to content

Instantly share code, notes, and snippets.

@ncarandini
Last active February 17, 2022 07:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncarandini/b0839175ce8c40c2b38605bb62844cbc to your computer and use it in GitHub Desktop.
Save ncarandini/b0839175ce8c40c2b38605bb62844cbc to your computer and use it in GitHub Desktop.
[Blazor] Using IValidatableObject for form validation
<EditForm Model="@Item" OnValidSubmit="@(e => OnSave.InvokeAsync(Item))">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="Title">Name:</label>
<InputText id="Title" @bind-Value="Item.name" class="form-control" />
</div>
<div class="form-group">
<label for="Description">Description:</label>
<InputText id="Description" @bind-Value="Item.Description" class="form-control" />
</div>
<div class="form-group">
<InputCheckbox id="UseAlphaCode" @bind-Value="Item.UseAlphaCode" />
<label for="UseAlphaCode">
Use Alpha code
</label>
</div>
<div class="form-group">
<label for="AlphaCode">AlphaCode:</label>
<InputText id="AlphaCode" @bind-Value="Item.AlphaCode" class="form-control" />
</div>
<div class="form-group">
<label for="BetaCode">BetaCode:</label>
<InputText id="BetaCode" @bind-Value="Item.BetaCode" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-warning" @onclick="OnCancel">Cancel</button>
</EditForm>
@code {
[Parameter]
public Item Item { get; set; }
[Parameter]
public EventCallback<Item> OnSave { get; set; }
[Parameter]
public EventCallback OnCancel { get; set; }
}
public class Item : IValidatableObject, ICompany, IAddress
{
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Description is required")]
public string Description { get; set; }
public bool UseAlphaCode {get; set; }
public string AlphaCode { get; set; }
public string BetaCode { get; set; }
[Range(0, 999.99)]
public decimal Price { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (UseAlphaCode)
{
if (String.IsNullOrWhiteSpace(AlphaCode))
{
yield return new ValidationResult("AlphaCode is required", new[] { "AlphaCode" });
}
}
else
{
if (String.IsNullOrWhiteSpace(BetaCode))
{
yield return new ValidationResult("BetaCode is required", new[] { "BetaCode" });
}
}
}
}
@ncarandini
Copy link
Author

ncarandini commented Apr 2, 2020

When using Blazor form validation with a model class that implement IValidatableObject, the overloaded Validate method is called only on form submit and only if no other validation decorated properties fails.
As an example, suppose that I try to submit the form with Name, Description and AlphaCode empty, and with UseAlphaCode equal to true. Then the validation summary will contain only errors for Name, Description and not for AlphaCode. Only after clearing the Name and Description errors then the AlphaCode appairs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment