Forms and Validation
This lesson will guide you through creating a form with field-level error handling and dynamic field captions using metadata. You’ll learn how to capture validation errors, display custom field captions, and dynamically reflect error messages for specific fields.
Code Example: TodoItem Component
Here’s the initial code for the TodoItem
component:
Code Explanation
-
ErrorInfo Type:
ErrorInfo<Task>
captures errors specific to each field in theTask
entity. If validation errors occur, they populatemodelState
, which contains error messages for each field.- Example Validation Error Payload:
- Each error message in
modelState
corresponds to a specific field, allowing targeted error display beside the relevant form inputs. - The validations themselves are defined within the entity as part of our single source of truth, ensuring consistent rules and messages across the entire application.
- Check out the validation options in the validation article to see how you can define and extend these validations directly in your entity.
-
The
save
Function:save
is triggered when the “Save” button is clicked:- It starts by clearing previous errors with
setError(undefined)
. - Then, it tries to save the
state
usingtaskRepo.save(state)
. - If an error occurs,
setError(error)
captures it, with field-specific messages provided byErrorInfo<Task>
.
- It starts by clearing previous errors with
-
Displaying Field-Level Errors:
- Error messages are shown directly below each field using
error?.modelState?.title
anderror?.modelState?.priority
. - Optional chaining (
?.
) ensures the UI is protected from undefined values, making error handling efficient and safe.
- Error messages are shown directly below each field using
Try it Out
Clear the title
field or set an invalid value for priority
(anything other than “low,” “medium,” or “high”) and see how validation messages appear in real-time, guiding users to correct their inputs.
Expanding with Field Options
To enhance the user experience, let’s switch the priority
input to a dropdown using the priority options defined in the entity.
-
First, add options to
priority
: -
Use the
options
list to render a dropdown:
This approach allows you to keep the priority
options in the entity as a single source of truth, ensuring consistency across the application.
Dynamic and Scalable Forms
To create a more dynamic form, you can loop through fields directly from the entity, easily building long or short forms without hardcoding field values:
Try the Interactive Example
Click the solve
button at the top right of the code editor to see this in action! This setup ensures consistent validation and display across forms and fields, making your UI scalable and reliable.
Summary
By utilizing field metadata, error handling, and dynamic rendering techniques, you can create reusable, rich forms and UI elements that enhance the consistency and maintainability of your application. These techniques allow you to:
- Centralize Display Logic: Captions, input types, and validation can all be maintained within the entity definitions, providing a single source of truth that is easily accessible across the application.
- Efficiently Handle Validation: By capturing and displaying field-level errors dynamically, you can offer immediate, user-friendly feedback, ensuring a smoother user experience.
- Build Scalable, Dynamic Forms: With access to field metadata and validation options, you can dynamically generate forms that adapt to each field’s specific requirements, reducing code duplication and making it easy to create various form layouts.
Together, these strategies make it straightforward to construct forms and other UI components that are consistently styled, validated, and ready for reuse throughout the application.
- Installing dependencies
- Starting http server