|
| 1 | +@page "/complex-form" |
| 2 | +@rendermode InteractiveServer |
| 3 | + |
| 4 | +@using System.ComponentModel.DataAnnotations |
| 5 | +@using BlazorUnitedApp.Validation |
| 6 | + |
| 7 | +<PageTitle>Validated Order Form</PageTitle> |
| 8 | + |
| 9 | +<h3>Validated Order Form</h3> |
| 10 | + |
| 11 | +<EditForm Model="@order" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit"> |
| 12 | + <DataAnnotationsValidator /> |
| 13 | + |
| 14 | + <div class="container mt-4"> |
| 15 | + <h4>Order Details</h4> |
| 16 | + <div class="mb-3"> |
| 17 | + <label for="orderName" class="form-label">Order Name</label> |
| 18 | + <InputText id="orderName" @bind-Value="order.OrderName" class="form-control" /> |
| 19 | + <ValidationMessage For="@(() => order.OrderName)" /> |
| 20 | + </div> |
| 21 | + |
| 22 | + <hr /> |
| 23 | + |
| 24 | + <h4>Customer Details</h4> |
| 25 | + <div class="card mb-3"> |
| 26 | + <div class="card-body"> |
| 27 | + <div class="mb-3"> |
| 28 | + <label for="customerFullName" class="form-label">Full Name</label> |
| 29 | + <InputText id="customerFullName" @bind-Value="order.CustomerDetails.FullName" class="form-control" /> |
| 30 | + <ValidationMessage For="@(() => order.CustomerDetails.FullName)" /> |
| 31 | + </div> |
| 32 | + <div class="mb-3"> |
| 33 | + <label for="customerEmail" class="form-label">Email</label> |
| 34 | + <InputText id="customerEmail" @bind-Value="order.CustomerDetails.Email" class="form-control" /> |
| 35 | + <ValidationMessage For="@(() => order.CustomerDetails.Email)" /> |
| 36 | + </div> |
| 37 | + <div class="mb-3"> |
| 38 | + <label for="customerAge" class="form-label">Age</label> |
| 39 | + <InputNumber id="customerAge" @bind-Value="order.CustomerDetails.Age" class="form-control" /> |
| 40 | + <ValidationMessage For="@(() => order.CustomerDetails.Age)" /> |
| 41 | + </div> |
| 42 | + |
| 43 | + <h5>Shipping Address</h5> |
| 44 | + <div class="card mb-3"> |
| 45 | + <div class="card-body"> |
| 46 | + <div class="mb-3"> |
| 47 | + <label for="shippingStreet" class="form-label">Street</label> |
| 48 | + <InputText id="shippingStreet" @bind-Value="order.CustomerDetails.ShippingAddress.Street" class="form-control" /> |
| 49 | + <ValidationMessage For="@(() => order.CustomerDetails.ShippingAddress.Street)" /> |
| 50 | + </div> |
| 51 | + <div class="mb-3"> |
| 52 | + <label for="shippingCity" class="form-label">City</label> |
| 53 | + <InputText id="shippingCity" @bind-Value="order.CustomerDetails.ShippingAddress.City" class="form-control" /> |
| 54 | + <ValidationMessage For="@(() => order.CustomerDetails.ShippingAddress.City)" /> |
| 55 | + </div> |
| 56 | + <div class="mb-3"> |
| 57 | + <label for="shippingZipCode" class="form-label">Zip Code</label> |
| 58 | + <InputText id="shippingZipCode" @bind-Value="order.CustomerDetails.ShippingAddress.ZipCode" class="form-control" /> |
| 59 | + <ValidationMessage For="@(() => order.CustomerDetails.ShippingAddress.ZipCode)" /> |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + |
| 66 | + <hr /> |
| 67 | + |
| 68 | + <h4>Order Items</h4> |
| 69 | + @if (order.OrderItems.Any()) |
| 70 | + { |
| 71 | + for (int i = 0; i < order.OrderItems.Count; i++) |
| 72 | + { |
| 73 | + var itemIndex = i; |
| 74 | + <div class="card mb-3"> |
| 75 | + <div class="card-header d-flex justify-content-between align-items-center"> |
| 76 | + <span>Item @(itemIndex + 1)</span> |
| 77 | + <button type="button" class="btn btn-sm btn-danger" @onclick="() => RemoveOrderItem(itemIndex)">Remove</button> |
| 78 | + </div> |
| 79 | + <div class="card-body"> |
| 80 | + <div class="mb-3"> |
| 81 | + <label for="@($"productName_{itemIndex}")" class="form-label">Product Name</label> |
| 82 | + <InputText id="@($"productName_{itemIndex}")" @bind-Value="order.OrderItems[itemIndex].ProductName" class="form-control" /> |
| 83 | + <ValidationMessage For="@(() => order.OrderItems[itemIndex].ProductName)" /> |
| 84 | + </div> |
| 85 | + <div class="row"> |
| 86 | + <div class="col-md-6 mb-3"> |
| 87 | + <label for="@($"quantity_{itemIndex}")" class="form-label">Quantity</label> |
| 88 | + <InputNumber id="@($"quantity_{itemIndex}")" @bind-Value="order.OrderItems[itemIndex].Quantity" class="form-control" /> |
| 89 | + <ValidationMessage For="@(() => order.OrderItems[itemIndex].Quantity)" /> |
| 90 | + </div> |
| 91 | + <div class="col-md-6 mb-3"> |
| 92 | + <label for="@($"price_{itemIndex}")" class="form-label">Price</label> |
| 93 | + <InputNumber id="@($"price_{itemIndex}")" @bind-Value="order.OrderItems[itemIndex].Price" class="form-control" TValue="decimal" ParseErrorMessage="Please enter a valid price." /> |
| 94 | + <ValidationMessage For="@(() => order.OrderItems[itemIndex].Price)" /> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + } |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + <p>No order items. Add one below.</p> |
| 104 | + } |
| 105 | + |
| 106 | + <button type="button" class="btn btn-success mb-3" @onclick="AddOrderItem">Add Order Item</button> |
| 107 | + |
| 108 | + <hr /> |
| 109 | + |
| 110 | + <div class="mb-3"> |
| 111 | + <button type="submit" class="btn btn-primary">Submit Order</button> |
| 112 | + </div> |
| 113 | + |
| 114 | + <ValidationSummary /> |
| 115 | + </div> |
| 116 | +</EditForm> |
| 117 | + |
| 118 | +@if (submitted) |
| 119 | +{ |
| 120 | + <div class="mt-4 alert alert-success" role="alert"> |
| 121 | + <h4>Form Submitted Successfully!</h4> |
| 122 | + <p>Order Name: @order.OrderName</p> |
| 123 | + <p>Customer: @order.CustomerDetails.FullName (@order.CustomerDetails.Email)</p> |
| 124 | + <h5>Order Items:</h5> |
| 125 | + <ul> |
| 126 | + @foreach (var item in order.OrderItems) |
| 127 | + { |
| 128 | + <li>@item.Quantity x @item.ProductName at @item.Price.ToString("C")</li> |
| 129 | + } |
| 130 | + </ul> |
| 131 | + </div> |
| 132 | +} |
| 133 | + |
| 134 | +@if (submitFailed) |
| 135 | +{ |
| 136 | + <div class="mt-4 alert alert-danger" role="alert"> |
| 137 | + <h4>Form Submission Failed!</h4> |
| 138 | + <p>Please correct the validation errors and try again.</p> |
| 139 | + </div> |
| 140 | +} |
| 141 | + |
| 142 | + |
| 143 | +@code { |
| 144 | + private OrderModel order = new OrderModel(); |
| 145 | + private bool submitted = false; |
| 146 | + private bool submitFailed = false; |
| 147 | + |
| 148 | + private void HandleValidSubmit() |
| 149 | + { |
| 150 | + Console.WriteLine("Form submitted successfully!"); |
| 151 | + // Here you would typically process the order |
| 152 | + // For demo purposes, we'll just display a success message |
| 153 | + submitted = true; |
| 154 | + submitFailed = false; |
| 155 | + // To see the data in console: |
| 156 | + // System.Text.Json.JsonSerializer.Serialize(order); |
| 157 | + } |
| 158 | + |
| 159 | + private void HandleInvalidSubmit() |
| 160 | + { |
| 161 | + Console.WriteLine("Form submission failed due to validation errors."); |
| 162 | + submitted = false; |
| 163 | + submitFailed = true; |
| 164 | + } |
| 165 | + |
| 166 | + private void AddOrderItem() |
| 167 | + { |
| 168 | + order.OrderItems.Add(new OrderItemModel()); |
| 169 | + submitted = false; // Reset submission status |
| 170 | + submitFailed = false; |
| 171 | + } |
| 172 | + |
| 173 | + private void RemoveOrderItem(int index) |
| 174 | + { |
| 175 | + if (index >= 0 && index < order.OrderItems.Count) |
| 176 | + { |
| 177 | + order.OrderItems.RemoveAt(index); |
| 178 | + } |
| 179 | + submitted = false; // Reset submission status |
| 180 | + submitFailed = false; |
| 181 | + } |
| 182 | +} |
0 commit comments