Form Validation
FormValidator class is used to validate user input on forms
Form Validation and Widget Integration Documentation
This document explains how to use the FormValidator
class for validating forms and how to integrate it into widgets for user input in a WebApp application.
Overview
In WebApp, the FormValidator
class is used to validate user input on forms. This validation is performed on the server side before processing the form data. Once validated, the form data can be used in widgets to display appropriate messages and handle user interactions.
Using FormValidator
The FormValidator
class helps in defining validation rules for form fields. Here’s a breakdown of how to set up and use FormValidator
.
Example Code: Form Validation in Controller
Below is an example of how to use FormValidator
to validate an email form in a Dart controller.
Explanation
- Creating a
FormValidator
Instance: Define the form and specify validation rules for each field. UseFieldValidator
methods to set requirements like required fields, email validation, and field length. - Validating the Form: Call
validateAndForm
to check if the form data meets the specified rules. - Processing the Form: If validation is successful, proceed with processing (e.g., sending an email). If not, handle validation errors.
- Rendering the Template: Add the form results and validation messages to the response and render the appropriate template.
Integrating Form Validation into Widgets
Forms validated with FormValidator
can be integrated into HTML templates (widgets) to provide user feedback and interact with the validated data.
Example Widget Integration
Here’s how to use the validated form data in an HTML widget:
Explanation
- HTML Structure: The HTML template contains form fields for email and password, including validation feedback. It uses server-side variables to display error messages and form values.
- Conditionally Displaying Content: The widget conditionally shows different content based on whether the user is logged in and the result of the form submission.
- Integration with Controller: The controller handles form submission, validates data using
FormValidator
, and passes the results to the template for rendering.
Summary
This documentation outlines how to use FormValidator
for form validation in WebApp. By setting up the validation rules in your Dart controller and integrating them into HTML widgets,