Bind fields, preserve browser validation and handle authoritative corrections.
A form has two directions of information flow. User edits update server state, and server corrections update the control's property. Writing a value attribute alone is not enough once a user has made a field dirty. Bindings connect the control to a writable readable and coordinate the round trip.
A label needs a matching field id. Required fields should keep native constraint validation: an invalid submit is rejected before the application accepts it. ARIA descriptions add context but do not replace a real label.
Understand the moving parts
bindValue handles text-like values and bindChecked handles boolean checked state. Select and radio controls have their own native property semantics. Programmatic correction must preserve useful caret state rather than replacing the whole input.
The exercise separates changing a draft from submitting it. A native form submission gives the app a deliberate commit point. Reset should visibly restore the lesson's initial values, while a server correction demonstrates that the server remains authoritative over the bound value.
Walk through the code
from pysx import Fragment, pysx, signal
def app() -> Fragment:
name = signal("")
accepted = signal(False)
result = signal("Not submitted")
def submit(_: object) -> None:
if accepted():
result.set(f"Accepted {name()}")
else:
result.set("Accept the terms before submitting")
def correct(_: object) -> None:
name.set("Server correction")
return pysx(t"""
form(id="lesson-form"onSubmit={submit})
label(for="form-name"): "Name (required)"input(id="form-name"bindValue={name} required={True})
label(for="form-terms"class="check-field")
input(id="form-terms"type="checkbox"bindChecked={accepted})
"Accept the demonstration terms"button(id="form-submit"type="submit"): "Submit"div:
button(id="form-correct"onClick={correct}): "Apply server correction"output(id="form-echo"): {name}
output(id="form-result"role="status"): {result}
""")
The name and checkbox bind to separate writable signals. Native required blocks an empty name; submit checks acceptance and reports the name. Apply server correction writes the same name signal that controls the field and preview.
Try it live
Try submitting an empty name. Enter a name, accept the terms, submit, then apply the server correction and reset.
What to observe
Empty input is rejected. A valid submit shows the accepted name; correction updates the actual field as well as its text preview.
Take this with you
Keep semantic forms and labels, and use bindings for the complete property round trip.