FORM & VALIDATION
Overview
- As known, form are one of the most ways and traditional user inputs approaches, hence, in Still.js they are part of the core features.
What you'll learn
Two-way input Binding considering
text,
radio-button,
check-box,
and Combobox (select)
Validators
The HTML Form tag in form Declaration
- In Still.js, when creating a form it's required to wrap all components within the
<form>
HTML tags, otherwise it won't work or it'll misbehave. Follow an example bellow:
Binding input text
Because the HTML file as the same name it's automatically bound to the .js as template. | |
---|---|
The form has two fields, First Name and Last Name, whose values are bound to the component's firstName
and lastName
state variables using the (value) directive. Also, they're bound to the tempate in the Hello @firstName @lastName
statement.
Still.js supports 2-way binding, so changes to bound input fields automatically update the linked state variables and keep the UI reactive.
Binding Radio Button and/or Checkbox
Two scenarios are being addressed in the template, we have the gender field with hard coded values, whereas Stills and Role fields are coming from lists and we're looping throug them.
Whereas input text and comboboxes bind is done via (value)
, for Radio and Checkbox we use (field)
while (value)
is still used for the value itself instead for binding. (value)
in the combo-box holds the actual value instead of the component binding variable.
Checkboxes and radio buttons in Still.js use a label
directive with position suffixes like Before
or After
to control label placement relative to the input.
Binding Combobox (select)
For comboboxes, binding is done on the <select>
element using (value)
, and when options come from a list, a placeholder can be used.
Form validation overview
Still.js provides with it's own validation capabilities which provides with a bunch of pre-defined ones. But also it allows custom validator to be added. Check Form Validation Directive.
In Summary
Forms are core part of Still.js, supporting 2-way binding using the (value)
directive in most cases. However, for comboboxes and checkboxes, binding is done with the (field)
directive.
Fields bound to any form element are naturally available to be bound to the template, as well as can be listen to the changes reactively when changed.