-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
docs(input): form validation examples #4368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import {Button, Form, Input} from "@nextui-org/react"; | ||
|
|
||
| export default function App() { | ||
| const [submitted, setSubmitted] = React.useState(null); | ||
|
|
||
| const onSubmit = (e) => { | ||
| e.preventDefault(); | ||
| const data = Object.fromEntries(new FormData(e.currentTarget)); | ||
|
|
||
| setSubmitted(data); | ||
| }; | ||
|
|
||
| return ( | ||
| <Form className="w-full max-w-xs" validationBehavior="native" onSubmit={onSubmit}> | ||
| <Input | ||
| isRequired | ||
| errorMessage={({validationDetails, validationErrors}) => { | ||
| if (validationDetails.typeMismatch) { | ||
| return "Please enter a valid email address"; | ||
| } | ||
|
|
||
| return validationErrors; | ||
| }} | ||
| label="Email" | ||
| labelPlacement="outside" | ||
| name="email" | ||
| placeholder="Enter your email" | ||
| type="email" | ||
| /> | ||
| <Button color="primary" type="submit"> | ||
| Submit | ||
| </Button> | ||
| {submitted && ( | ||
| <div className="text-small text-default-500"> | ||
| You submitted: <code>{JSON.stringify(submitted)}</code> | ||
| </div> | ||
| )} | ||
| </Form> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import App from "./built-in-validation.raw.jsx?raw"; | ||
|
|
||
| const react = { | ||
| "/App.jsx": App, | ||
| }; | ||
|
|
||
| export default { | ||
| ...react, | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,40 @@ | ||||||||||||||||||||
| import {Button, Form, Input} from "@nextui-org/react"; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export default function App() { | ||||||||||||||||||||
| const [submitted, setSubmitted] = React.useState(null); | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+1
to
+5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add missing React import. The component uses React.useState but React is not imported. +import React from "react";
import {Button, Form, Input} from "@nextui-org/react";📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
| const onSubmit = (e) => { | ||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||
| const data = Object.fromEntries(new FormData(e.currentTarget)); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| setSubmitted(data); | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ( | ||||||||||||||||||||
| <Form className="w-full max-w-xs" validationBehavior="native" onSubmit={onSubmit}> | ||||||||||||||||||||
| <Input | ||||||||||||||||||||
| isRequired | ||||||||||||||||||||
| label="Username" | ||||||||||||||||||||
| labelPlacement="outside" | ||||||||||||||||||||
| name="username" | ||||||||||||||||||||
| placeholder="Enter your username" | ||||||||||||||||||||
| type="text" | ||||||||||||||||||||
| validate={(value) => { | ||||||||||||||||||||
| if (value.length < 3) { | ||||||||||||||||||||
| return "Username must be at least 3 characters long"; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return value === "admin" ? "Nice try!" : null; | ||||||||||||||||||||
| }} | ||||||||||||||||||||
| /> | ||||||||||||||||||||
| <Button color="primary" type="submit"> | ||||||||||||||||||||
| Submit | ||||||||||||||||||||
| </Button> | ||||||||||||||||||||
| {submitted && ( | ||||||||||||||||||||
| <div className="text-small text-default-500"> | ||||||||||||||||||||
| You submitted: <code>{JSON.stringify(submitted)}</code> | ||||||||||||||||||||
| </div> | ||||||||||||||||||||
| )} | ||||||||||||||||||||
| </Form> | ||||||||||||||||||||
| ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import App from "./custom-validation.raw.jsx?raw"; | ||
|
|
||
| const react = { | ||
| "/App.jsx": App, | ||
| }; | ||
|
|
||
| export default { | ||
| ...react, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import {Button, Form, Input} from "@nextui-org/react"; | ||
|
|
||
| export default function App() { | ||
| const [submitted, setSubmitted] = React.useState(null); | ||
| const [password, setPassword] = React.useState(""); | ||
| const errors = []; | ||
|
|
||
| const onSubmit = (e) => { | ||
| e.preventDefault(); | ||
| const data = Object.fromEntries(new FormData(e.currentTarget)); | ||
|
|
||
| setSubmitted(data); | ||
| }; | ||
|
|
||
| if (password.length < 4) { | ||
| errors.push("Password must be 4 characters or more."); | ||
| } | ||
| if ((password.match(/[A-Z]/g) || []).length < 1) { | ||
| errors.push("Password must include at least 1 upper case letter"); | ||
| } | ||
| if ((password.match(/[^a-z0-9]/gi) || []).length < 1) { | ||
| errors.push("Password must include at least 1 symbol."); | ||
| } | ||
|
|
||
| return ( | ||
| <Form className="w-full max-w-xs" validationBehavior="native" onSubmit={onSubmit}> | ||
| <Input | ||
| errorMessage={() => ( | ||
| <ul> | ||
| {errors.map((error, i) => ( | ||
| <li key={i}>{error}</li> | ||
| ))} | ||
| </ul> | ||
| )} | ||
| isInvalid={errors.length > 0} | ||
| label="Password" | ||
| labelPlacement="outside" | ||
| name="password" | ||
| placeholder="Enter your password" | ||
| value={password} | ||
| onValueChange={setPassword} | ||
| /> | ||
| <Button color="primary" type="submit"> | ||
| Submit | ||
| </Button> | ||
| {submitted && ( | ||
| <div className="text-small text-default-500"> | ||
| You submitted: <code>{JSON.stringify(submitted)}</code> | ||
| </div> | ||
| )} | ||
| </Form> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import App from "./real-time-validation.raw.jsx?raw"; | ||
|
|
||
| const react = { | ||
| "/App.jsx": App, | ||
| }; | ||
|
|
||
| export default { | ||
| ...react, | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {Button, Form, Input} from "@nextui-org/react"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add missing React import The component uses +import * as React from "react";
import {Button, Form, Input} from "@nextui-org/react";📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default function App() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [isLoading, setIsLoading] = React.useState(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [errors, setErrors] = React.useState({}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const onSubmit = async (e) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setIsLoading(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const data = Object.fromEntries(new FormData(e.currentTarget)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await callServer(data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setErrors(result.errors); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setIsLoading(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling and type safety As a documentation example, it should demonstrate proper error handling and type safety. - const onSubmit = async (e) => {
+ type FormData = {
+ username: string;
+ };
+
+ const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsLoading(true);
+ try {
+ const formData = new FormData(e.currentTarget);
+ const data: FormData = {
+ username: formData.get('username') as string
+ };
+ const result = await callServer(data);
- const data = Object.fromEntries(new FormData(e.currentTarget));
- const result = await callServer(data);
-
- setErrors(result.errors);
+ setErrors(result.errors);
+ } catch (error) {
+ setErrors({ username: "An error occurred. Please try again." });
+ }
setIsLoading(false);
};📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className="w-full max-w-xs" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| validationBehavior="native" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| validationErrors={errors} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onSubmit={onSubmit} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isRequired | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isDisabled={isLoading} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label="Username" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| labelPlacement="outside" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name="username" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| placeholder="Enter your username" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Button color="primary" isLoading={isLoading} type="submit"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Submit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Form> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fake server used in this example. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async function callServer(_) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await new Promise((resolve) => setTimeout(resolve, 500)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errors: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| username: "Sorry, this username is taken.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import App from "./server-validation.raw.jsx?raw"; | ||
|
|
||
| const react = { | ||
| "/App.jsx": App, | ||
| }; | ||
|
|
||
| export default { | ||
| ...react, | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing React import
The component uses
React.useStatebut doesn't import React.+import * as React from "react"; import {Button, Form, Input} from "@nextui-org/react";📝 Committable suggestion