|
| 1 | +@page "/" |
| 2 | + |
| 3 | +@using Syncfusion.Blazor.Inputs |
| 4 | +@using System.ComponentModel.DataAnnotations |
| 5 | +@using Syncfusion.Blazor.Buttons |
| 6 | + |
| 7 | +<div class="form-container"> |
| 8 | + <EditForm Model="@annotation" OnValidSubmit="OnValidSubmit" OnInvalidSubmit="OnInvalidSubmit"> |
| 9 | + <DataAnnotationsValidator></DataAnnotationsValidator> |
| 10 | + |
| 11 | + <div class="form-element"> |
| 12 | + <SfTextArea @bind-Value="annotation.Comments" ResizeMode="Resize.None" FloatLabelType="FloatLabelType.Always" RowCount="6" ColumnCount="40" |
| 13 | + Placeholder="Type your message.." CssClass="e-static-clear" ShowClearButton="true"></SfTextArea> |
| 14 | + <ValidationMessage For="@(() => annotation.Comments)" /> |
| 15 | + </div> |
| 16 | + |
| 17 | + <div class="form-element"> |
| 18 | + <SfButton type="submit" IsPrimary="true">Submit</SfButton> |
| 19 | + </div> |
| 20 | + </EditForm> |
| 21 | + |
| 22 | + @if (!string.IsNullOrEmpty(Message)) |
| 23 | + { |
| 24 | + <p class="success-message">@Message</p> |
| 25 | + } |
| 26 | +</div> |
| 27 | +<style> |
| 28 | + /* Center the form container */ |
| 29 | + .form-container { |
| 30 | + display: flex; |
| 31 | + flex-direction: column; |
| 32 | + align-items: center; |
| 33 | + margin-top: 5rem; |
| 34 | + } |
| 35 | +
|
| 36 | + /* Style for the form elements to ensure proper spacing */ |
| 37 | + .form-element { |
| 38 | + margin-bottom: 1rem; |
| 39 | + } |
| 40 | +
|
| 41 | + /* Style for the success message */ |
| 42 | + .success-message { |
| 43 | + margin-top: 1rem; |
| 44 | + color: green; /* Ensures the success message is green */ |
| 45 | + } |
| 46 | +</style> |
| 47 | + |
| 48 | +@code { |
| 49 | + private string Message = string.Empty; |
| 50 | + private Annotation annotation = new Annotation(); |
| 51 | + |
| 52 | + private async Task OnValidSubmit() |
| 53 | + { |
| 54 | + Message = "Form Submitted Successfully!"; |
| 55 | + await Task.Delay(2000); |
| 56 | + Message = string.Empty; |
| 57 | + StateHasChanged(); |
| 58 | + } |
| 59 | + |
| 60 | + private void OnInvalidSubmit() |
| 61 | + { |
| 62 | + Message = string.Empty; |
| 63 | + } |
| 64 | + |
| 65 | + public class Annotation |
| 66 | + { |
| 67 | + [Required(ErrorMessage = "The comments field is required.")] |
| 68 | + [MaxLength(250, ErrorMessage = "The comments field should not contain more than 250 characters.")] |
| 69 | + [MinLength(20, ErrorMessage = "The comments field must contain at least 20 characters.")] |
| 70 | + public string? Comments { get; set; } |
| 71 | + } |
| 72 | +} |
0 commit comments