@page "/"
@using Microsoft.Extensions.Logging
@inject ILogger<Index> Logger
@using System.ComponentModel.DataAnnotations
<EditForm Model="@exampleModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label>
From Date:
<InputDate @bind-Value="exampleModel.FromDate" />
</label>
<ValidationMessage For="() => exampleModel.FromDate" />
</p>
<p>
<label>
To Date:
<InputDate @bind-Value="exampleModel.ToDate" />
</label>
<ValidationMessage For="() => exampleModel.ToDate" />
</p>
<button type="submit">Submit</button>
</EditForm>
@code {
private ExampleModel exampleModel = new ExampleModel();
private void HandleValidSubmit()
{
Logger.LogInformation("HandleValidSubmit called");
// Process the valid form
}
public class ExampleModel : IValidatableObject
{
[DataType(DataType.Date)]
public DateTime? FromDate { get; set; }
[DataType(DataType.Date)]
public DateTime? ToDate { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (ToDate < FromDate)
{
yield return new ValidationResult("ToDate must be after FromDate", new[] { nameof(ToDate) });
}
}
}
}