You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/development-guide/15-composition.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,3 +52,74 @@ The Linode Create Page is a good example of a complex form that is built using r
52
52
### Uncontrolled Forms
53
53
Uncontrolled forms are a type of form that does not have a state for its values. It is often used for simple forms that do not need to be controlled, such as forms with a single input field or call to action.
54
54
55
+
## Form Validation (React Hook Form)
56
+
### Best Practices
57
+
1. Keep API validation in `@linode/validation` package
58
+
2. Create extended schemas in `@linode/manager` package when you need validation beyond the API contract
59
+
3. Use yup.concat() to extend existing schemas
60
+
4. Add custom validation logic within the resolver function
61
+
5. Include type definitions for form values and context
62
+
63
+
### Simple Schema Extension
64
+
For basic form validation, extend the API schema directly:
You may create a `resolver` function that handles the validation (see: [ManageImageRegionsForm.tsx](https://github.com/linode/manager/blob/develop/packages/manager/src/features/Images/ImagesLanding/ImageRegions/ManageImageRegionsForm.tsx#L189-L213)):
84
+
85
+
```typescript
86
+
// Step 1: Create a Resolver Function
87
+
// This function validates your form data against specific requirements
message: 'Please select at least one valid option',
106
+
type: 'validate'
107
+
}
108
+
},
109
+
values
110
+
};
111
+
}
112
+
113
+
return { errors: {}, values };
114
+
};
115
+
116
+
// Step 2: Use the Resolver in Your Form
117
+
const form =useForm({
118
+
resolver,
119
+
defaultValues: { selectedItems: [] },
120
+
context: { availableItems: ['item1', 'item2'] }
121
+
});
122
+
```
123
+
124
+
### Additional Complexity
125
+
When working with multiple sequential schemas that require validation, you can create a resolver map and function (see: [LinodeCreate/resolvers.ts](https://github.com/linode/manager/blob/develop/packages/manager/src/features/Linodes/LinodeCreate/resolvers.ts])).
0 commit comments