You can define the form attribute validation in a Razor component inside the function to achieve this. Please do not forget to include the DataAnnotations in the Razor component file.
@using System.ComponentModel.DataAnnotations;
<EditForm Model="@model" OnValidSubmit="@SubmitHandler">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group d-flex justify-content-between">
<label class="col-form-label col-3" for="name">Name</label>
<InputText @bind-Value="@model.Name" id="name" Class="form-control" />
</div>
<button type="submit" @onclick="@SubmitHandler" class="btn btn-primary">Submit</button>
</EditForm>
@code {
public class ModelClass
{
[Required]
public string Name { get; set; }
}
private ModelClass model = new ModelClass();
private void SubmitHandler()
{
Console.WriteLine("Submit");
}
}
Please refer to this documentation for more form validations.
Share with