Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/guides/static-edit-form.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Submit and validate a native Blazor form

The gallery's `form-post` page uses the library controls in one named `EditForm`. It is an in-memory draft, not a settings endpoint. Run the gallery from the repository root:

```powershell
dotnet run --project gallery/Gallery --no-launch-profile --urls http://127.0.0.1:0
```

Open the printed address at `/static/form-post`; the same form is available at `/server/form-post` and `/wasm/form-post`. The fixture uses the field, text input, text area, number, select, checkbox, radio group, switch, segmented, date and time controls.

## The form and its names

1. Give `EditForm` a `FormName` and register the same name on the model's `[SupplyParameterFromForm]`. Leave Blazor's antiforgery protection enabled. A named `EditForm` renders its token; a plain host form needs its own `AntiforgeryToken`.
2. Keep the **posted** model through validation. Seed initial sample values only when no model was supplied, then construct the `EditContext`. `ShouldUseFieldIdentifiers = true` opts this form into framework-derived names in both server and WASM renderers.
3. Use `DataAnnotationsValidator` and the host's data rules. The controls bind through `InputBase<TValue>`; they do not supply a validator.

| In the fixture | Native wire name | What the host receives |
| --- | --- | --- |
| Draft title | `Model.Title` | Top-level string. |
| City | `Model.Address.City` | Value on the nested address. |
| Two tags | `Model.Tags[0]`, `Model.Tags[1]` | Two distinct array slots. |
| Support alias | `support_alias` | Host override backed by `[SupplyParameterFromForm(FormName = "integration-draft", Name = "support_alias")]`. |
| Segmented review mode | `Model.ReviewMode` | One selected native radio value. The disabled middle option is skipped by native arrow navigation and is not submitted. |
| Audience radio group | `Model.Audience` | One choice, independent of the segmented review mode. |
| Checked notification checkbox | `Model.NotifyOwners=true` | One Boolean value. |
| Unchecked digest switch and disabled notice checkbox | No entries | Neither `Model.DailyDigest` nor `Model.LockedNotice` is posted. There is no hidden value to substitute for an unchecked or disabled control. |
| Delivery window | `Model.Start`, `Model.End` | Two native date fields, not one range value. |
| Empty quiet time | `Model.QuietTime=` | An empty wire string, mapped to `null` for `TimeOnly?`. |
| Disabled reference | No entry | An absent key, not an empty string. A disabled control is not a security boundary; enforce access in the host. |

An explicit `name` wins, including from an attribute dictionary. Keep the receiving property in sync with it. For normal binding use `@bind-Value`; if you supply `Value` and `ValueChanged` separately, also supply the expression the framework uses for field identity:

```razor
<FlTextInput Value="@Alias" ValueChanged="SetAlias"
ValueExpression="() => Alias" name="support_alias" />

@code {
[SupplyParameterFromForm(FormName = "integration-draft", Name = "support_alias")]
private string? Alias { get; set; }

private void SetAlias(string? value) => Alias = value;
}
```

## Verify the browser and the server separately

- Disable JavaScript in the browser, enter 18 seats, and choose an end date earlier than the start date. Tab from Language into the selected **Personal** review mode and press Right Arrow to skip disabled **Owners only** and choose **Shared**. Tab to **Authors** in the separate audience group and press Right Arrow to reach **Everyone**. Check **Notify owners** and turn **Daily digest** off. **Save draft** sends one value per radio group and one checked Boolean value. `18` is a valid number within the browser's range of 1–100, but the host rejects it with the rule allowing 1–12. The date group owns its error once, before its help and external description. The inputs keep their edits and selections. Correct the two errors and save again; the result lists what the handler received.
- A malformed number is a different case. A native number input can reject or clear the text before any request. The focused bUnit test submits malformed text to the real component binder and checks the framework's parsing message and unchanged bound value. It is not evidence that a malformed number reached the server in a browser POST.
- Clear the title and use **Check on server** to bypass native required validation and see the host's top-level error. Clear only City and save: the default submit validator does not recursively validate the address. **Include the city in submit validation** opens a fresh draft in JavaScript-disabled static rendering. In Server and WASM, the link changes the rule without discarding the draft; **Use default validation** removes the message owned by the optional city check. The opt-in uses the same `RequiredAttribute` rule and an `EditContext` message store for that one field, not a new library validator or a generic object walker.
- In Server and WASM, edit the start, leave it, and check that the draft dates show the new start and the old end. Then edit and leave the end. The date readout comes from the bound model; the change indicator comes from `EditContext.OnFieldChanged`. Save with the reversed dates to see the group error, then correct the end. The save handler reads the existing model, so a disabled field can retain an in-memory value even though a native POST would omit it. The first interactive render preserves the routing and field wiring, but the sample does not promise to retain focus or an uncommitted edit across takeover.

**Windows Playwright WebKit limitation.** This build shows these date and time inputs as text fields. In interactive modes, .NET 10 Blazor sees the declared temporal type but cannot serialize the change for the effective `text` type (`Invalid element type 'text'.`). The edit stays visible without reaching the bound model; the framework's own `InputDate<DateOnly?>` has the same behavior on this build. JavaScript-disabled document POSTs still send the entered strings. The fixture keeps the real binding path with no script workaround. This build is not Safari; see [ADR 0003](../decisions/0003-script-policy.md#what-could-not-be-checked).

```powershell
dotnet test --project tests/FluentCMS.Components.Tests --filter-class '*FormPostBoundaryTests'
dotnet test --project tests/FluentCMS.Components.Browser --filter-class 'FluentCMS.Components.Browser.Forms.FormPost*'
```

The first command checks the parsing boundary; the second drives the actual gallery, including a JavaScript-disabled document POST with antiforgery. See [ADR 0004](../decisions/0004-forms-model.md) for the library's binding contract.
1 change: 1 addition & 0 deletions gallery/Gallery.Client/GalleryPages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class GalleryPages
new("choices", "Checkbox, radio and switch"),
new("segmented", "Segmented"),
new("date-time", "Date and time"),
new("form-post", "Form submission"),
new("tokens", "Tokens"),
new("theme-provider", "Theme provider"),
new("patterns", "Patterns"),
Expand Down
Loading