The forms component provides a range of form elements and inputs that allow users to enter information. It also includes error messages. It is designed to make it as simple as possible to create an HTML form.
When to use this component
Use the forms component when users need to submit information or data. This could be in order to complete a task or as part of a service we provide. For example, when an author needs to enter the title of their manuscript when submitting to a journal.
How it works
The forms component does not include any JavaScript. States (such as an invalid/error state) are defined at a data level. If you're using client-side processing, you might benefit from compiling the form’s handlebars template in the browser.
Installation
To use the forms component, enter the following command in your Terminal:
`` npm install @springernature/global-forms `
Then include the necessary Sass files in your project:
`scss // Include this with your other components @import '@springernature/global-forms/scss/50-components/forms';
// Include these with your utilities (if not already there) @import '@springernature/brand-context/default/scss/60-utilities/buttons.scss'; @import '@springernature/brand-context/default/scss/60-utilities/spacing.scss'; `
Then you will need to register the handlebars partials in the /view folder. There are 2 options for rendering form fields:
1. Compile fields independently, using their respective templates from the view/fields folder. 2. Organise your data into arrays of fieldsets and fields and loop over these arrays, assigning the globalFormFieldset and globalFormField partials respectively.
Here is an example base template implementation:
`html
`
For this example, you might have 2 simple inputs for a name and password:
Form fields are made up of a control (the input) and an associated label. They can also include hint text to provide extra guidance when filling out a field.
The template property sets the type of field - for example, "template": "globalFormText" renders a text input field if that is what you have registered the view/fields/globalFormText.hbs template as.
This component supports several standard form field attributes. For example, to include a readonly attribute on your text input, you can include a property of the same name on the data:
There are 2 ways to add fieldsets to your form: 1. Create a field with the template value of globalFormFieldset and an array of fields using its fields property. 2. Create an array of fieldsets and loop over them, assigning the globalFormFieldset partial.
Use HTML to style the legend and add semantic meaning. In most cases, legends should be headings, with the heading level determined by the page structure.
Do not use a fieldset without an accompanying legend. If a legend is not appropriate, neither is a fieldset; just use fields without a fieldset parent.
A fieldset can include one or more nested fieldsets. If you are including headings in your fieldsets, be careful to use a heading level that reflects the nesting level. For example, if a fieldset’s legend uses
Avoid asking for information unless you really need it. To support this, form fields are set to
required by default.
You do not need to mark fields as required. If a user tries to submit the form without filling in a required field, browser validation will show them an error message.
If you need to ask for optional information, set
optional: true. This will remove required and aria-required="true" and append the text “(optional)” to the field’s label.
$3
Error messages inform the user when there’s a problem with the information they’ve provided and how they can fix it.
Error messages are displayed inline, underneath the label and above the input of the relevant field. Error messages are prefixed with a cross icon. This helps to make the error more obvious to users who cannot perceive the red colour.
error property. The inclusion of the property means the field is in an error state. The property value (a string) defines the error message the user sees.`json { "template": "globalFormText", "label": "Full name", "id": "full-name", "name": "full-name", "error": "Enter your name" } `
errorSummary property. Each error in the errors array must point to the id of the input it relates to and repeat its error message:`json "errorSummary": { "id": "summary", "title": "There is a problem", "errors": [ { "id": "full-name-error", "error": "Enter your full name" }, { "id": "checkbox-terms-error", "error": "Agree to the terms to continue" } ] } `
Use the text input field when you need to let users enter text that’s no longer than a single line, such as their name or email address.
$3
All text inputs must have visible labels. Labels are positioned above the input and are written in sentence case.
#### Avoid placeholder text
The text input component does not support placeholder text.
Placeholder text:
- disappears when a user enters information - which makes it hard for some people to remember what it said - can be mistaken by some users for information they’ve entered
Instead, add a hint above the input, when you need one, using a