Ismail
Ismail

Reputation: 23

How to use "DataAnnotations" Validation without OnValidSubmit

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/

Upvotes: 0

Views: 419

Answers (1)

Leandro Toloza
Leandro Toloza

Reputation: 2020

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!

Upvotes: 1

Related Questions