|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: Form Events |
| 4 | +description: Form for Blazor - Events. |
| 5 | +slug: form-events |
| 6 | +tags: telerik,blazor,form,edit,events |
| 7 | +published: True |
| 8 | +position: 30 |
| 9 | +--- |
| 10 | + |
| 11 | +# Form Events |
| 12 | + |
| 13 | +The Form component for Blazor exposes events that allow you to respond to user actions and provide custom logic. |
| 14 | + |
| 15 | +* [OnSubmit](#onsubmit) |
| 16 | +* [OnValidSubmit](#onvalidsubmit) |
| 17 | +* [OnInvalidSubmit](#oninvalidsubmit) |
| 18 | + |
| 19 | +>note The examples in this article use the [`EditContext`]({%slug form-overview%}#use-the-telerik-form-for-blazor-with-an-editcontext), but you can use a [model]({%slug form-overview%}#use-the-telerik-form-for-blazor-with-a-model) instead. |
| 20 | +
|
| 21 | +## OnSubmit |
| 22 | + |
| 23 | +The `OnSubmit` event fires when the user clicks on the Submit button in the Form. Its handler takes as an argument the `EditContext` object and is used to trigger some custom logic based on the validity of the form. |
| 24 | + |
| 25 | +When there is a handler for the `OnSubmit` event, the [`OnValidSubmit`](#onvalidsubmit) and [`OnInvalidSubmit`](#oninvalidsubmit) events will not be fired. |
| 26 | + |
| 27 | +The `OnSubmit` event is mapped to the `OnSubmit` event of the <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0">Microsoft EditForm</a> |
| 28 | + |
| 29 | + |
| 30 | +>caption Handle the OnSubmit event |
| 31 | +
|
| 32 | +````CSHTML |
| 33 | +@* Use the OnSubmit event to trigger some custom logic depending on the validity of the form *@ |
| 34 | +
|
| 35 | +@using System.ComponentModel.DataAnnotations |
| 36 | +
|
| 37 | +<TelerikForm EditContext="@myEditContext" |
| 38 | + OnSubmit="@OnSubmitHandler"> |
| 39 | + <FormValidation> |
| 40 | + <DataAnnotationsValidator></DataAnnotationsValidator> |
| 41 | + </FormValidation> |
| 42 | +</TelerikForm> |
| 43 | +
|
| 44 | +
|
| 45 | +@code { |
| 46 | + public Person person = new Person(); |
| 47 | +
|
| 48 | + EditContext myEditContext { get; set; } |
| 49 | +
|
| 50 | +
|
| 51 | + private void OnSubmitHandler(EditContext editContext) |
| 52 | + { |
| 53 | + bool isFormValid = editContext.Validate(); |
| 54 | +
|
| 55 | + if (isFormValid) |
| 56 | + { |
| 57 | + //apply some custom logic when the form is valud |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + //apply some custom logic when the form is not valid |
| 62 | + } |
| 63 | + } |
| 64 | +
|
| 65 | + protected override void OnInitialized() |
| 66 | + { |
| 67 | + myEditContext = new EditContext(person); |
| 68 | + base.OnInitialized(); |
| 69 | + } |
| 70 | +
|
| 71 | + public class Person |
| 72 | + { |
| 73 | + public int Id { get; set; } |
| 74 | + [Required(ErrorMessage ="Add your first name")] |
| 75 | + public string FirstName { get; set; } |
| 76 | + [Required(ErrorMessage = "Add your last name")] |
| 77 | + public string LastName { get; set; } |
| 78 | + [Range(typeof(DateTime), "1/1/1900", "1/15/2020", ErrorMessage = "The Date of Birth must be between 1/1/1900 and 1/15/2021")] |
| 79 | + public DateTime DOB { get; set; } = DateTime.Today.AddYears(-20); |
| 80 | + public string CompanyName { get; set; } |
| 81 | + public DateTime HireDate { get; set; } |
| 82 | + public bool IsOnVacation { get; set; } = true; |
| 83 | + } |
| 84 | +} |
| 85 | +```` |
| 86 | + |
| 87 | +## OnValidSubmit |
| 88 | + |
| 89 | +The `OnValidSubmit` event fires when the form is submitted and there are no validation erros. It is mapped to `OnValidSubmit ` event of the <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0">Microsoft EditForm</a>. Its handler takes the `EditContext` as an argument. |
| 90 | + |
| 91 | +>caption Use the OnValidSubmit event |
| 92 | +
|
| 93 | +````CSHTML |
| 94 | +@* You can use the OnValidSubmit event to provide custom logic when the form is valid *@ |
| 95 | +
|
| 96 | +@using System.ComponentModel.DataAnnotations |
| 97 | +
|
| 98 | +<TelerikForm EditContext="@myEditContext" |
| 99 | + OnValidSubmit="@OnValidSubmitHandler"> |
| 100 | + <FormValidation> |
| 101 | + <DataAnnotationsValidator></DataAnnotationsValidator> |
| 102 | + </FormValidation> |
| 103 | +</TelerikForm> |
| 104 | +
|
| 105 | +
|
| 106 | +@code { |
| 107 | + public Person person = new Person(); |
| 108 | +
|
| 109 | + EditContext myEditContext { get; set; } |
| 110 | +
|
| 111 | +
|
| 112 | + public void OnValidSubmitHandler(EditContext editContext) |
| 113 | + { |
| 114 | + //some logic when the form is valid. |
| 115 | + Console.WriteLine("valid submission, you can save the model"); |
| 116 | + } |
| 117 | +
|
| 118 | + protected override void OnInitialized() |
| 119 | + { |
| 120 | + myEditContext = new EditContext(person); |
| 121 | + base.OnInitialized(); |
| 122 | + } |
| 123 | +
|
| 124 | + public class Person |
| 125 | + { |
| 126 | + public int Id { get; set; } |
| 127 | + [Required(ErrorMessage = "Add your first name")] |
| 128 | + public string FirstName { get; set; } |
| 129 | + [Required(ErrorMessage = "Add your last name")] |
| 130 | + public string LastName { get; set; } |
| 131 | + [Range(typeof(DateTime), "1/1/1900", "1/15/2020", ErrorMessage = "The Date of Birth must be between 1/1/1900 and 1/15/2021")] |
| 132 | + public DateTime DOB { get; set; } = DateTime.Today.AddYears(-20); |
| 133 | + public string CompanyName { get; set; } |
| 134 | + public DateTime HireDate { get; set; } |
| 135 | + public bool IsOnVacation { get; set; } = true; |
| 136 | + } |
| 137 | +} |
| 138 | +```` |
| 139 | + |
| 140 | +## OnInvalidSubmit |
| 141 | + |
| 142 | +The `OnInvalidSubmit` event fires when there are validation erros in the Form upon its submission. It is mapped to `OnInvalidSubmit` event of the <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0">Microsoft EditForm</a>. Its handler takes the `EditContext` as an argument. |
| 143 | + |
| 144 | +>caption Use the OnInvalidSubmit event |
| 145 | +
|
| 146 | +````CSHTML |
| 147 | +@* You can use the OnInvalidSubmit event to provide custom logic when the form is not valid *@ |
| 148 | +
|
| 149 | +@using System.ComponentModel.DataAnnotations |
| 150 | +
|
| 151 | +<TelerikForm EditContext="@myEditContext" |
| 152 | + OnInvalidSubmit="@OnInvalidSubmitHandler"> |
| 153 | + <FormValidation> |
| 154 | + <DataAnnotationsValidator></DataAnnotationsValidator> |
| 155 | + </FormValidation> |
| 156 | +</TelerikForm> |
| 157 | +
|
| 158 | +
|
| 159 | +@code { |
| 160 | + public Person person = new Person(); |
| 161 | +
|
| 162 | + EditContext myEditContext { get; set; } |
| 163 | +
|
| 164 | + public void OnInvalidSubmitHandler(EditContext editContext) |
| 165 | + { |
| 166 | + //some logic when the form is not valid. |
| 167 | + Console.WriteLine("INVALID submission attempt"); |
| 168 | + } |
| 169 | +
|
| 170 | + protected override void OnInitialized() |
| 171 | + { |
| 172 | + myEditContext = new EditContext(person); |
| 173 | + base.OnInitialized(); |
| 174 | + } |
| 175 | +
|
| 176 | + public class Person |
| 177 | + { |
| 178 | + public int Id { get; set; } |
| 179 | + [Required(ErrorMessage = "Add your first name")] |
| 180 | + public string FirstName { get; set; } |
| 181 | + [Required(ErrorMessage = "Add your last name")] |
| 182 | + public string LastName { get; set; } |
| 183 | + [Range(typeof(DateTime), "1/1/1900", "1/15/2020", ErrorMessage = "The Date of Birth must be between 1/1/1900 and 1/15/2021")] |
| 184 | + public DateTime DOB { get; set; } = DateTime.Today.AddYears(-20); |
| 185 | + public string CompanyName { get; set; } |
| 186 | + public DateTime HireDate { get; set; } |
| 187 | + public bool IsOnVacation { get; set; } = true; |
| 188 | + } |
| 189 | +} |
| 190 | +```` |
| 191 | + |
| 192 | +## See Also |
| 193 | + |
| 194 | + * [Overview]({%slug form-overview%}) |
| 195 | + * [FormItems]({%slug form-formitems%}) |
| 196 | + * [Template]({%slug form-formitems-template%}) |
| 197 | + * [Orientation]({%slug form-orientation%}) |
| 198 | + * [Events]({%slug form-events%}) |
| 199 | + |
0 commit comments