-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample2.tsx
More file actions
128 lines (117 loc) · 3.33 KB
/
example2.tsx
File metadata and controls
128 lines (117 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
80
81
82
83
84
85
86
87
88
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
116
117
118
119
120
121
122
123
124
125
126
127
128
"use client";
import React from "react";
import { useForm } from "react-hook-form";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Profanity } from "profanity-validator";
import { toast } from "sonner";
const profanity = new Profanity({
customWords: ["badword", "inappropriate"],
heat: 0.5,
});
interface FormValues {
title: string;
description: string;
tags: string;
}
export default function Example2() {
const form = useForm<FormValues>({
defaultValues: {
title: "This is a clean title",
description: "This description is free of profanity",
tags: "clean, safe, f*ck friendly, boobs less",
},
});
const onSubmit = async (data: FormValues) => {
try {
// Validate all fields
const results = await Promise.all([
profanity.validateField(data.title),
profanity.validateField(data.description),
profanity.validateField(data.tags),
]);
const [titleResult, descriptionResult, tagsResult] = results;
if (!titleResult.isValid) {
form.setError("title", { message: titleResult.message });
}
if (!descriptionResult.isValid) {
form.setError("description", { message: descriptionResult.message });
}
if (!tagsResult.isValid) {
form.setError("tags", { message: tagsResult.message });
}
if (results.every((r) => r.isValid)) {
// All validations passed
console.log("Form submitted:", data);
toast("Form submitted check console log!");
}
} catch (error) {
console.error("Validation error:", error);
toast.error("Validation failed. Check form errors.");
}
};
return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-6 w-full max-w-md mx-auto p-6"
>
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>Title</FormLabel>
<FormControl>
<Input type="text" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Textarea {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="tags"
render={({ field }) => (
<FormItem>
<FormLabel>Tags</FormLabel>
<FormControl>
<Input
type="text"
{...field}
placeholder="Enter comma-separated tags"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" className="w-full">
{form.formState.isSubmitting ? "Checking..." : "Submit"}
</Button>
</form>
</Form>
);
}