Creating effective Zurb Foundation forms is all about simplicity and clarity. Zurb Foundation forms are designed to be easy to use, even for users who may not be tech-savvy.
A well-structured form should have a clear and concise label for each field, as seen in the example of the Zurb Foundation form with a label for the "Email" field. This helps users understand what information is required.
One of the key principles of Zurb Foundation forms is to keep it short and sweet. A form with too many fields can be overwhelming for users, which is why it's essential to only ask for the information that's necessary.
Form Basics
Creating a form in Foundation is designed to be easy but extremely flexible.
Your password must have at least 10 characters, a number, and an Emoji. This is just one example of how Foundation forms can be customized to meet specific requirements.
Add the .middle class to vertically align the label with its input. This simple addition can greatly improve the user experience.
Variables
Variables are a crucial part of customizing your form styles. By using Sass variables, you can change the default styles of your form components.
You can customize the default styles of your form components using these Sass variables in your project's settings file. For example, you can change the background color of invalid text inputs by modifying the $input-background-invalid variable.
The default border around custom fieldsets is 1px solid $medium-gray, and you can change this by modifying the $fieldset-border variable. Similarly, the default padding inside custom fieldsets is rem-calc(20), which you can change by modifying the $fieldset-padding variable.
Here's a table summarizing some of the key variables you can customize:
You can also customize the default font size and color of form labels by modifying the $form-label-font-size and $form-label-color variables, respectively. The default font size for form labels is rem-calc(14), and the default font color is $black.
Form Input
Form Input is an essential part of any form, and Zurb Foundation's approach makes it easy to work with. You can validate input using the validateInput function, which checks for specific attributes like data-abide-ignore, type="hidden", or disabled.
This function takes a jQuery object as an argument, specifically an HTML input, and ignores any inputs that match these attributes. It's a great way to streamline your form validation process.
Some common input types in Zurb Foundation include text inputs, which come in various flavors like text, date, datetime, and email. These input types create a text field that users can fill in.
Text Inputs
Text inputs are an essential part of form design, and there are many types to choose from.
These input types create a text field, including text, date, datetime, datetime-local, email, month, number, password, search, tel, time, url, and week.
Some of these types, like email and url, have specific formats that users expect to see, so it's a good idea to use them when the input requires a specific type of text.
Using the right type of text input can make a big difference in how users interact with your form, and can even help prevent errors.
Form Validation
Form validation is a crucial aspect of any form, and Abide makes it easy to implement. It automatically detects form errors by their class, which can be set to a custom value using the formErrorSelector option.
Abide also ignores inputs with specific attributes, such as data-abide-ignore, type="hidden", or disabled. This means you can exclude certain fields from validation if needed.
To validate a form, you can use the validateForm method, which checks for invalid inputs and displays the corresponding error elements. This method fires two events: Abide#event:formvalid and Abide#event:forminvalid.
Here are some scenarios where you might want to override or add custom patterns and validators:
- Override builtin patterns and validators before foundation is initialized
- Add new patterns and validators before or after foundation is initialized
Abide also provides a method to remove error classes from elements, which can be useful when you want to reset a form or remove error messages. The removeErrorClasses method takes a jQuery object as an argument and removes the specified CSS error class from the label, input, and form elements.
Validate Form
Abide's validateForm function is a game-changer for form validation. It goes through a form and displays the form error element if there are any invalid inputs. This function fires two events: Abide#event:formvalid and Abide#event:forminvalid.
You can call this function on a form by passing it as an argument to the validateForm method. For example, if you have a form with an ID of "myForm", you can call validateForm like this: $('#myForm').validateForm();
If you want to remove CSS error classes from a label, input, or form, you can use the removeErrorClasses method. This method takes a jQuery object as an argument and removes the specified error class.
Here are some key things to keep in mind when using the validateForm function:
- It ignores inputs with the data-abide-ignore attribute, type="hidden", or disabled attributes set.
- It goes through a form to find inputs and proceeds to validate them in ways specific to their type.
Form Errors and Handling
Form errors are automatically detected by Abide when they have a class of .form-error by default, or when specified in the formErrorSelector option. This means you don't have to write extra code to identify errors.
Abide can also detect form errors when they are siblings of the input or inside the same parent. If the form errors can't be placed next to the input, like in an Input Group, you can declare the relation with a [data-form-error-for] attribute.
The form-input-error class styles the background and border of an input field to have an error state. You can customize this by using the $background variable, which defaults to $alert-color, and the $background-lighten variable, which defaults to 10%.
Here's a quick rundown of the parameters for form-input-error:
If you want to remove error classes from a label, input, or form, you can use the removeErrorClasses function. This function takes a jQuery object as an argument, which is the element to remove the class from.
Sources
Featured Images: pexels.com