Interactive forms with buttons that respond to clicks, change dynamically, and perform form actions like validation and reset.
addButton()onClickform.resetForm() to reset fieldssetValue()isFormValid() and markAllAsTouched()Buttons are the primary way users interact with your form beyond filling in fields. They trigger actions like submitting data, resetting values, or navigating between sections.
This tutorial also introduces form.state() for managing local state!
Add a button using addButton() with a label and onClick handler. We'll use form.state() to track clicks.
form.state(initialValue) creates a reactive state variable. Call it as a function to get the value, use .set() to replace, or .update() to transform it. Button labels can be dynamic! Pass a function to label to change the text based on state.
Use isDisabled to disable buttons until certain conditions are met. Here, the "Add Shipping Address" button is disabled until the name field is filled.
Use isVisible to show or hide buttons based on form state. This example toggles between Simple and Advanced modes, showing additional fields and a reset button only in advanced mode.
Use form.resetForm() to reset all fields to their default values. This is perfect for "Reset" or "Clear" buttons.
resetForm() restores all fields to their defaultValue. If no default was set, fields become empty. Use form.markAllAsTouched() and form.isFormValid() to validate before revealing new sections. Here, a summary section only appears when all required fields are valid.
markAllAsTouched() triggers validation messages on all fields. isFormValid() returns true only if all required fields are filled. Use this pattern to gate access to additional form sections. Use setValue() to programmatically fill fields with preset values. This example shows template buttons that instantly populate the form with different configurations.
field.setValue(value) sets a field's value programmatically. Use setValue(null) to clear a field. Note: setValue() has no effect on fields with computedValue. setValue() to duplicate address fieldsGreat job! You've learned how to create interactive buttons and manage form actions. Ready to explore layout options?
Continue to Multi-Column LayoutsOther learning paths: