Skip to content

Commit c0036b4

Browse files
committed
Add submit validation
1 parent bb5841e commit c0036b4

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

docs/src/app/(private)/experiments/forms/tanstack.tsx

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,50 @@ export default function App() {
4444
onSubmit: async ({ value }) => {
4545
console.log('submit', value);
4646
},
47+
validators: {
48+
onSubmit: ({ value: formValues }) => {
49+
console.log('validate on submit', formValues);
50+
const fields: Record<string, string | undefined> = {};
51+
52+
if (!formValues.serverName) {
53+
fields.serverName = 'Required';
54+
}
55+
56+
if (!formValues.staticIpAddress) {
57+
fields.staticIpAddress = 'Required';
58+
} else if (!IPV4_PATTERN.test(formValues.staticIpAddress)) {
59+
fields.staticIpAddress = 'Invalid ipv4 address';
60+
}
61+
62+
if (!formValues.region) {
63+
fields.region = 'Required';
64+
}
65+
66+
if (!formValues.image) {
67+
fields.image = 'Required';
68+
} else if (!CONTAINER_URL_PATTERN.test(formValues.image)) {
69+
fields.image = 'Invalid ipv4 address';
70+
}
71+
72+
if (!formValues.instanceType) {
73+
fields.instanceType = 'Required';
74+
}
75+
76+
if (formValues.numOfInstances == null) {
77+
fields.numOfInstances = 'Required';
78+
}
79+
80+
if (!formValues.storageType) {
81+
fields.storageType = 'Required';
82+
}
83+
84+
if (formValues.backupSchedule.length === 0) {
85+
fields.backupSchedule = 'Required';
86+
}
87+
88+
return { fields };
89+
},
90+
},
4791
});
4892
return (
4993
<div className="font-sans w-80">
@@ -53,6 +97,9 @@ export default function App() {
5397
handleSubmit();
5498
}}
5599
>
100+
<h2 className="border-b border-gray-200 pb-3 text-lg font-medium text-gray-900">
101+
Configure VPS
102+
</h2>
56103
<FormField
57104
name="serverName"
58105
validators={{
@@ -107,6 +154,7 @@ export default function App() {
107154
onValueChange={field.handleChange}
108155
onBlur={field.handleBlur}
109156
placeholder="e.g. 10.2.3.45"
157+
className="!w-40"
110158
/>
111159

112160
<FieldError match={!field.state.meta.isValid}>
@@ -263,7 +311,7 @@ export default function App() {
263311
value={field.state.value || null}
264312
onValueChange={field.handleChange}
265313
>
266-
<SelectTrigger onBlur={field.handleBlur}>
314+
<SelectTrigger className="w-48" onBlur={field.handleBlur}>
267315
<SelectValue />
268316
<SelectIcon>
269317
<ChevronUpDownIcon />
@@ -426,14 +474,9 @@ export default function App() {
426474

427475
<FormField
428476
name="storageType"
429-
validators={{
430-
onSubmit: ({ value }) => {
431-
return value ? undefined : 'Required';
432-
},
433-
}}
434477
children={(field) => {
435478
return (
436-
<FieldRoot name={field.name} invalid={!field.state.meta.isValid}>
479+
<FieldRoot name={field.name} invalid={!field.state.meta.isValid} className="mt-3">
437480
<FieldsetRoot
438481
render={
439482
<RadioGroup
@@ -474,13 +517,10 @@ export default function App() {
474517
}
475518
return undefined;
476519
},
477-
onSubmit: ({ value }) => {
478-
return value.length > 0 ? undefined : 'Required';
479-
},
480520
}}
481521
children={(field) => {
482522
return (
483-
<FieldRoot name={field.name} invalid={!field.state.meta.isValid}>
523+
<FieldRoot name={field.name} invalid={!field.state.meta.isValid} className="mt-2">
484524
<FieldsetRoot
485525
render={
486526
<CheckboxGroup value={field.state.value} onValueChange={field.handleChange} />
@@ -510,7 +550,9 @@ export default function App() {
510550
}}
511551
/>
512552

513-
<Button type="submit">Submit</Button>
553+
<Button type="submit" className="mt-4">
554+
Save
555+
</Button>
514556
</Form>
515557
</div>
516558
);
@@ -1029,7 +1071,7 @@ const REGIONS = cartesian(['us', 'eu'], ['central', 'east', 'west'], ['1', '2',
10291071
);
10301072

10311073
const INSTANCE_TYPES = [
1032-
{ label: 'Select instance type', value: null },
1074+
{ label: 'Pick instance type', value: null },
10331075
...cartesian(['t', 'm'], ['1', '2'], ['small', 'medium', 'large'])
10341076
.map((val1) => val1.join('.').replace('.', ''))
10351077
.map((val2) => ({

0 commit comments

Comments
 (0)