Error summary
Summarize validation errors and link users to the affected fields.
Linked field errors
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let nativeValidationAttributes key initiallyShown (message: string) =
let checkedState = "$" + key + "_checked"
let error = "$" + key + "_error"
let refresh = error + " = el.validity.valid ? '' : " + System.Text.Json.JsonSerializer.Serialize(message)
let validate = checkedState + " = true; " + refresh
[ _dataSignals ($"{{{key}_checked: false, {key}_error: ''}}")
_dataOn ("blur", validate)
_dataOn ("input", $"if ({checkedState}) {{ {refresh} }}")
_dataOn ("invalid", validate)
_dataOn ("reset", ["window"], $"if (evt.target === el.form) queueMicrotask(() => {{ if (!evt.defaultPrevented) {{ {checkedState} = false; {error} = ''; }} }})")
_dataAttr ("aria-invalid", error + " ? 'true' : null")
if initiallyShown then _dataInit validate ]
let errorSummaryExample =
div {
_class "grid min-w-0 max-w-xl gap-4 p-4"
fveErrorSummary {
_id "example-errors"
_label "Check your details"
_style "display:none"
_dataShow "$_summary_email_error"
fveErrorSummaryItem {
_href "#summary-email"
_dataText "'Email address: ' + $_summary_email_error"
}
}
fveField {
fveLabel { _for "summary-email"; "Email address" }
fveInput {
_id "summary-email"
_name "email"
_type InputType.Email
_value "not-an-email"
yield! nativeValidationAttributes "_summary_email" true "Enter a valid email address."
_dataAttr ("aria-describedby", "$_summary_email_error ? 'summary-email-validation' : null")
}
fveFieldError {
_id "summary-email-validation"
_style "display:none"
_dataShow "$_summary_email_error"
_dataText "$_summary_email_error"
}
}
}This example also uses Field, Input. See their Installation sections for any additional setup.
On this page
Installation
Copy this component and its required dependencies into the consumer-owned Components project.
dotnet fve add error-summary --config src/Acme.Components/fve.jsonAdd to src/Acme.Web/js/app.js once, alongside your other component imports:
import '../../Acme.Components/Components/Assets/error-summary.js';The CLI resolves dependencies and preserves consumer-authored files. See Installation for project initialization, Tailwind, the shared runtime, and building/loading your app entry.
Usage
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let nativeValidationAttributes key initiallyShown (message: string) =
let checkedState = "$" + key + "_checked"
let error = "$" + key + "_error"
let refresh = error + " = el.validity.valid ? '' : " + System.Text.Json.JsonSerializer.Serialize(message)
let validate = checkedState + " = true; " + refresh
[ _dataSignals ($"{{{key}_checked: false, {key}_error: ''}}")
_dataOn ("blur", validate)
_dataOn ("input", $"if ({checkedState}) {{ {refresh} }}")
_dataOn ("invalid", validate)
_dataOn ("reset", ["window"], $"if (evt.target === el.form) queueMicrotask(() => {{ if (!evt.defaultPrevented) {{ {checkedState} = false; {error} = ''; }} }})")
_dataAttr ("aria-invalid", error + " ? 'true' : null")
if initiallyShown then _dataInit validate ]
let errorSummaryExample =
div {
_class "grid min-w-0 max-w-xl gap-4 p-4"
fveErrorSummary {
_id "example-errors"
_label "Check your details"
_style "display:none"
_dataShow "$_summary_email_error"
fveErrorSummaryItem {
_href "#summary-email"
_dataText "'Email address: ' + $_summary_email_error"
}
}
fveField {
fveLabel { _for "summary-email"; "Email address" }
fveInput {
_id "summary-email"
_name "email"
_type InputType.Email
_value "not-an-email"
yield! nativeValidationAttributes "_summary_email" true "Enter a valid email address."
_dataAttr ("aria-describedby", "$_summary_email_error ? 'summary-email-validation' : null")
}
fveFieldError {
_id "summary-email-validation"
_style "display:none"
_dataShow "$_summary_email_error"
_dataText "$_summary_email_error"
}
}
}API reference
module ErrorSummaryDsl =
val fveErrorSummary : ComponentElementBuilder
val fveErrorSummaryItem : ComponentElementBuilder