formaze 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -108
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,55 +28,55 @@ import "formaze/dist/style.css";
|
|
|
28
28
|
|
|
29
29
|
// create the validation schema
|
|
30
30
|
const formSchema = makeFormSchema({
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
email: { type: "email" },
|
|
32
|
+
password: { type: "password", minLength: { value: 8 } },
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
// create the form component
|
|
36
36
|
const Form = createFormValidator(formSchema);
|
|
37
37
|
|
|
38
38
|
export function MyForm() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
39
|
+
// for TypeScript
|
|
40
|
+
function handleSubmit(data: z.infer<typeof formSchema>) {
|
|
41
|
+
const result = formSchema.safeParse(data);
|
|
42
|
+
|
|
43
|
+
if (!result.success) throw new Error("Invalid inputs");
|
|
44
|
+
|
|
45
|
+
console.log(data);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// for JavaScript
|
|
49
|
+
// /**@param {import("zod").infer<typeof formSchema>} data */
|
|
50
|
+
// function handleSubmit(data) {
|
|
51
|
+
// const result = formSchema.safeParse(data);
|
|
52
|
+
|
|
53
|
+
// if (!result.success) throw new Error("Invalid inputs");
|
|
54
|
+
|
|
55
|
+
// console.log(data);
|
|
56
|
+
// }
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<Form onSubmit={handleSubmit}>
|
|
60
|
+
<Form.Input
|
|
61
|
+
label="Email"
|
|
62
|
+
type="email"
|
|
63
|
+
name="email"
|
|
64
|
+
placeholder="Enter your email"
|
|
65
|
+
/>
|
|
66
|
+
<Form.Input
|
|
67
|
+
label="Password"
|
|
68
|
+
type="password"
|
|
69
|
+
name="password"
|
|
70
|
+
placeholder="Enter your password"
|
|
71
|
+
/>
|
|
72
|
+
<button
|
|
73
|
+
className="rounded-md bg-blue-500 py-1 px-3 text-white hover:bg-blue-600"
|
|
74
|
+
type="submit"
|
|
75
|
+
>
|
|
76
|
+
Submit
|
|
77
|
+
</button>
|
|
78
|
+
</Form>
|
|
79
|
+
);
|
|
80
80
|
}
|
|
81
81
|
```
|
|
82
82
|
|
|
@@ -86,33 +86,33 @@ The makeFormSchema method allows you to define validation rules for each field i
|
|
|
86
86
|
|
|
87
87
|
```js
|
|
88
88
|
const formSchema = makeFormSchema({
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
89
|
+
email: {
|
|
90
|
+
type: "email",
|
|
91
|
+
customMessage: "A valid email is required",
|
|
92
|
+
},
|
|
93
|
+
password: {
|
|
94
|
+
type: "password",
|
|
95
|
+
minLength: {
|
|
96
|
+
value: 8,
|
|
97
|
+
message: "Password must be at least 8 characters",
|
|
98
|
+
},
|
|
99
|
+
maxLength: {
|
|
100
|
+
value: 16,
|
|
101
|
+
message: "Password must be less than 16 characters",
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
username: {
|
|
105
|
+
type: "string",
|
|
106
|
+
regex: {
|
|
107
|
+
value: /^[a-z0-9]{3,}$/ /* only lowercase letters and numbers */,
|
|
108
|
+
message: "Username must contains lowercase letters and numbers",
|
|
109
|
+
},
|
|
110
|
+
customMessage: "Username must not be empty",
|
|
111
|
+
},
|
|
112
|
+
terms: {
|
|
113
|
+
type: "boolean",
|
|
114
|
+
customMessage: "You must accept the terms to continue",
|
|
115
|
+
},
|
|
116
116
|
});
|
|
117
117
|
```
|
|
118
118
|
|
|
@@ -123,8 +123,8 @@ import { z } from "zod";
|
|
|
123
123
|
import { createFormValidator } from "formaze";
|
|
124
124
|
|
|
125
125
|
const formSchema = z.object({
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
email: z.string().email(),
|
|
127
|
+
name: z.string().min(3, { message: "Required" }),
|
|
128
128
|
});
|
|
129
129
|
|
|
130
130
|
const Form = createFormValidator(formSchema);
|
|
@@ -157,39 +157,39 @@ import "formaze/dist/style.css";
|
|
|
157
157
|
|
|
158
158
|
```css
|
|
159
159
|
.form-control {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
160
|
+
display: flex;
|
|
161
|
+
flex-direction: column;
|
|
162
|
+
justify-content: center;
|
|
163
|
+
gap: 1rem;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
.form-control .form-field {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
167
|
+
display: flex;
|
|
168
|
+
flex-direction: column;
|
|
169
|
+
justify-content: center;
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
.form-control .form-field .form-input {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
173
|
+
padding: 10px 1rem;
|
|
174
|
+
border-radius: 10px;
|
|
175
|
+
border-width: 1px;
|
|
176
|
+
border-color: rgb(209 213 219);
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
.form-control .form-field .form-input:focus {
|
|
180
|
-
|
|
181
|
-
|
|
180
|
+
border-color: rgb(59 130 246);
|
|
181
|
+
outline: none;
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
.form-control .form-field-error-text {
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
color: rgb(220, 38, 38);
|
|
186
|
+
font-size: 0.875rem;
|
|
187
187
|
}
|
|
188
188
|
|
|
189
189
|
/* or */
|
|
190
190
|
|
|
191
191
|
.form-control .form-field-error {
|
|
192
|
-
|
|
192
|
+
color: rgb(220, 38, 38);
|
|
193
193
|
}
|
|
194
194
|
```
|
|
195
195
|
|
|
@@ -227,8 +227,8 @@ The createFormValidator function accepts an argument which is a type of zod sche
|
|
|
227
227
|
|
|
228
228
|
```tsx
|
|
229
229
|
const schema = makeFormSchema({
|
|
230
|
-
|
|
231
|
-
|
|
230
|
+
email: { type: "email" },
|
|
231
|
+
password: { type: "string", minLength: { value: 8 } },
|
|
232
232
|
});
|
|
233
233
|
|
|
234
234
|
const Form = createFormValidator(schema);
|
|
@@ -247,14 +247,14 @@ const Form = createFormValidator(schema);
|
|
|
247
247
|
|
|
248
248
|
```tsx
|
|
249
249
|
const schema = makeFormSchema({
|
|
250
|
-
|
|
251
|
-
|
|
250
|
+
email: { type: "email" },
|
|
251
|
+
password: { type: "string" },
|
|
252
252
|
});
|
|
253
253
|
|
|
254
254
|
const Form = createFormValidator(schema);
|
|
255
255
|
|
|
256
256
|
const handleSubmit = (data: z.infer<typeof schema>) => {
|
|
257
|
-
|
|
257
|
+
console.log(data); // { email: "test@example.com", password: "securePassword" }
|
|
258
258
|
};
|
|
259
259
|
|
|
260
260
|
<Form onSubmit={handleSubmit} />;
|
|
@@ -271,15 +271,15 @@ const handleSubmit = (data: z.infer<typeof schema>) => {
|
|
|
271
271
|
|
|
272
272
|
```tsx
|
|
273
273
|
const schema = makeFormSchema({
|
|
274
|
-
|
|
275
|
-
|
|
274
|
+
email: { type: "email" },
|
|
275
|
+
password: { type: "string" },
|
|
276
276
|
});
|
|
277
277
|
|
|
278
278
|
const Form = createFormValidator(schema);
|
|
279
279
|
|
|
280
280
|
const defaultValues = {
|
|
281
|
-
|
|
282
|
-
|
|
281
|
+
email: "example@example.com",
|
|
282
|
+
password: "",
|
|
283
283
|
};
|
|
284
284
|
|
|
285
285
|
<Form defaultValues={defaultValues} />;
|
|
@@ -298,9 +298,9 @@ You can include `Form.Input` for each form field, along with any other elements
|
|
|
298
298
|
|
|
299
299
|
```tsx
|
|
300
300
|
<Form onSubmit={onSubmit} defaultValues={defaultValues}>
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
301
|
+
<Form.Input type="email" name="email" placeholder="Email" />
|
|
302
|
+
<Form.Input type="password" name="password" placeholder="Password" />
|
|
303
|
+
<button type="submit">Submit</button>
|
|
304
304
|
</Form>
|
|
305
305
|
```
|
|
306
306
|
|
|
@@ -357,14 +357,14 @@ The makeFormSchema is a function that generates a Zod schema based on the provid
|
|
|
357
357
|
|
|
358
358
|
```tsx
|
|
359
359
|
const schema = makeFormSchema({
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
360
|
+
email: { type: "email" },
|
|
361
|
+
password: {
|
|
362
|
+
type: "string",
|
|
363
|
+
minLength: {
|
|
364
|
+
value: 8,
|
|
365
|
+
message: "Password must be at least 8 characters long",
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
368
|
});
|
|
369
369
|
```
|
|
370
370
|
|