I have used "DataAnnotations" Validation in Blazor application with the help of below link.It works fine when I use "OnValidSubmit" in EditForm.I would like to know how to use DataAnnotations Validation When I click on button (without OnValidSubmit in EditForm)
https://www.c-sharpcorner.com/article/validation-in-blazor-app/
CodePudding user response:
You Need to Do the Following, based on that Page:
<EditForm Model="@employee" EditContext="@editContextForButton">
And in the end of the form
<button onclick="@validSubmit">submitButton</button>
Finally you do the following in @code
private EditContext editContextForButton {get;set;}
protected override void OnTinitializedAsync(){
editContextForButton = new EditContext(YourModelHere);
}
private void validSubmit(){
editContextForButton.Validate();
}
I Test it and it works!
CodePudding user response:
You need to create validation attribute action filter for that and use it over your model/viewmodel. as shown in this example below:
stackoverflow.com/questions/23780943/how-to-create-custom-validation-attribute