Skip to content

Commit

Permalink
Merge pull request #27 from hamsurang/docs/setError
Browse files Browse the repository at this point in the history
Docs: set-error.mdx
  • Loading branch information
chaaerim authored Feb 1, 2025
2 parents b96f251 + 8bc705a commit ccb5f31
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/content/docs/useform/seterror.mdx
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
---
title: setError
description: Manually set an input error
description: μž…λ ₯ 였λ₯˜λ₯Ό μˆ˜λ™μœΌλ‘œ μ„€μ •ν•˜κΈ°
sidebar: apiLinks
---

## \</> `setError:` <TypeText>`(name: string, error: FieldError, { shouldFocus?: boolean }) => void`</TypeText>

The function allows you to manually set one or more errors.
이 ν•¨μˆ˜λŠ” ν•˜λ‚˜ μ΄μƒμ˜ 였λ₯˜λ₯Ό μˆ˜λ™μœΌλ‘œ μ„€μ •ν•  수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

### Props

---

| Name | Type | Description |
| ------- | ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` | <TypeText>string</TypeText> | input's name. |
| `error` | <TypeText>`{ type: string, message?: string, types: MultipleFieldErrors }`</TypeText> | Set an error with its type and message. |
| config | <TypeText>`{ shouldFocus?: boolean }`</TypeText> | Should focus the input during setting an error. This only works when the input's reference is registered, it will not work for custom register as well. |
| Name | Type | Description |
| ------- | ------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` | <TypeText>string</TypeText> | μž…λ ₯ ν•„λ“œμ˜ 이름. |
| `error` | <TypeText>`{ type: string, message?: string, types: MultipleFieldErrors }`</TypeText> | 였λ₯˜μ˜ νƒ€μž…κ³Ό λ©”μ‹œμ§€λ₯Ό μ„€μ •ν•©λ‹ˆλ‹€. |
| config | <TypeText>`{ shouldFocus?: boolean }`</TypeText> | 였λ₯˜ μ„€μ • μ‹œ μž…λ ₯ ν•„λ“œμ— 포컀슀λ₯Ό 쀄지 μ—¬λΆ€λ₯Ό μ„€μ •ν•©λ‹ˆλ‹€. 이 κΈ°λŠ₯은 μž…λ ₯ ν•„λ“œμ˜ μ°Έμ‘°κ°€ λ“±λ‘λ˜μ–΄ μžˆμ„ λ•Œλ§Œ λ™μž‘ν•˜λ©°, custom registerμ—λŠ” μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. |

<Admonition type="important" title="Rules">

- This method will not persist the associated input error if the input passes `register`'s associated rules.
- 이 λ©”μ„œλ“œλŠ” μž…λ ₯이 `register`의 κ΄€λ ¨ κ·œμΉ™μ„ ν†΅κ³Όν•œ κ²½μš°μ—λŠ” κ΄€λ ¨λœ μž…λ ₯ 였λ₯˜λ₯Ό μ§€μ†ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
```javascript
register('registerInput', { minLength: 4 });
setError('registerInput', { type: 'custom', message: 'custom message' });
// validation will pass as long as minLength requirement pass
register("registerInput", { minLength: 4 })
setError("registerInput", { type: "custom", message: "custom message" })
// minLength μš”κ΅¬ 사항을 μΆ©μ‘±ν•˜λŠ” ν•œ μœ νš¨μ„± κ²€μ‚¬λŠ” ν†΅κ³Όν•©λ‹ˆλ‹€.
```
- An error that is not associated with an input field will be persisted until cleared with `clearErrors`. This behaviour is only applicable for built-in validation at field level.
- μž…λ ₯ ν•„λ“œμ™€ μ—°κ΄€λ˜μ§€ μ•Šμ€ 였λ₯˜λŠ” `clearErrors`λ₯Ό 톡해 μ§€μ›Œμ§ˆ λ•ŒκΉŒμ§€ μ§€μ†λ©λ‹ˆλ‹€. 이 λ™μž‘μ€ ν•„λ“œ μˆ˜μ€€μ—μ„œμ˜ λ‚΄μž₯ μœ νš¨μ„± κ²€μ‚¬μ—λ§Œ ν•΄λ‹Ήλ©λ‹ˆλ‹€.
```javascript
setError("notRegisteredInput", { type: "custom", message: "custom message" })
// clearErrors() need to invoked manually to remove that custom error
// ν•΄λ‹Ή 였λ₯˜λ₯Ό μ œκ±°ν•˜λ €λ©΄ clearErrors()λ₯Ό μˆ˜λ™μœΌλ‘œ ν˜ΈμΆœν•΄μ•Ό ν•©λ‹ˆλ‹€.
```
- You can set a server or global error with `root` as the key. This type of error will not persist with each submission.
- `root`λ₯Ό ν‚€λ‘œ μ‚¬μš©ν•˜μ—¬ μ„œλ²„ λ˜λŠ” μ „μ—­ 였λ₯˜λ₯Ό μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이 μœ ν˜•μ˜ 였λ₯˜λŠ” 각 제좜 μ‹œ μ§€μ†λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
```javascript
setError("root.serverError", {
type: "400",
Expand All @@ -40,18 +40,18 @@ The function allows you to manually set one or more errors.
type: "random",
})
```
- Can be useful in the `handleSubmit` method when you want to give error feedback to a user after async validation. (ex: API returns validation errors)
- `shouldFocus` doesn't work when an input has been disabled.
- This method will force set `isValid` formState to `false`. However, it's important to be aware that `isValid` will always be derived from the validation result of your input registration rules or schema result.
- There are certain keywords that need to be avoided to prevent conflicts with type checking. They are `type` and `types`.
- 이 λ©”μ„œλ“œλŠ” 비동기 μœ νš¨μ„± 검사 ν›„, μ‚¬μš©μžμ—κ²Œ 였λ₯˜ ν”Όλ“œλ°±μ„ μ œκ³΅ν•˜λ €λŠ” 경우, `handleSubmit` λ©”μ„œλ“œμ—μ„œ μœ μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (예: APIμ—μ„œ μœ νš¨μ„± 검사 였λ₯˜ λ°˜ν™˜ μ‹œ)
- `shouldFocus`λŠ” μž…λ ₯ ν•„λ“œκ°€ λΉ„ν™œμ„±ν™”λœ 경우 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
- 이 λ©”μ„œλ“œλŠ” `isValid` 폼 μƒνƒœλ₯Ό `false`둜 κ°•μ œ μ„€μ •ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ `isValid`λŠ” 항상 μž…λ ₯ 등둝 κ·œμΉ™ λ˜λŠ” μŠ€ν‚€λ§ˆ 결과의 μœ νš¨μ„± 검사 κ²°κ³Όμ—μ„œ νŒŒμƒλœλ‹€λŠ” 점에 μœ μ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.
- νƒ€μž… 검사λ₯Ό λ°©ν•΄ν•˜μ§€ μ•ŠκΈ° μœ„ν•΄ `type`κ³Ό `types`λΌλŠ” ν‚€μ›Œλ“œλŠ” ν”Όν•΄μ•Ό ν•©λ‹ˆλ‹€.

</Admonition>

**Examples:**

---

**Single Error**
**단일 였λ₯˜ μ„€μ •**

<TabGroup buttonLabels={["TS", "JS"]}>

Expand Down Expand Up @@ -123,7 +123,7 @@ const App = () => {

</TabGroup>

**Multiple Errors**
**닀쀑 였λ₯˜ μ„€μ •**

<TabGroup buttonLabels={["TS", "JS"]}>

Expand Down Expand Up @@ -163,12 +163,12 @@ const App = () => {
{
type: "manual",
name: "username",
message: "Double Check This",
message: "ν•œ 번 더 ν™•μΈν•˜μ„Έμš”.",
},
{
type: "manual",
name: "firstName",
message: "Triple Check This",
message: "μ„Έ 번 더 ν™•μΈν•˜μ„Έμš”.",
},
]

Expand Down Expand Up @@ -216,12 +216,12 @@ const App = () => {
{
type: "manual",
name: "username",
message: "Double Check This",
message: "ν•œ 번 더 ν™•μΈν•˜μ„Έμš”.",
},
{
type: "manual",
name: "firstName",
message: "Triple Check This",
message: "μ„Έ 번 더 ν™•μΈν•˜μ„Έμš”.",
},
]

Expand All @@ -240,7 +240,7 @@ const App = () => {

</TabGroup>

**Single Field Errors**
**단일 ν•„λ“œ 였λ₯˜**

<TabGroup buttonLabels={["TS", "JS"]}>

Expand All @@ -267,8 +267,8 @@ const App = () => {
React.useEffect(() => {
setError("lastName", {
types: {
required: "This is required",
minLength: "This is minLength",
required: "이 ν•„λ“œλŠ” ν•„μˆ˜μž…λ‹ˆλ‹€.",
minLength: "μ΅œμ†Œ 길이가 λΆ€μ‘±ν•©λ‹ˆλ‹€.",
},
})
}, [setValue])
Expand Down Expand Up @@ -309,8 +309,8 @@ const App = () => {
React.useEffect(() => {
setError("lastName", {
types: {
required: "This is required",
minLength: "This is minLength",
required: "이 ν•„λ“œλŠ” ν•„μˆ˜μž…λ‹ˆλ‹€.",
minLength: "μ΅œμ†Œ 길이가 λΆ€μ‘±ν•©λ‹ˆλ‹€.",
},
})
}, [setError])
Expand All @@ -333,7 +333,7 @@ const App = () => {

</TabGroup>

**Server Error**
**μ„œλ²„ 였λ₯˜ μ„€μ •**

```javascript copy
import * as React from "react";
Expand Down Expand Up @@ -369,6 +369,6 @@ const App = () => {

---

The following video explain `setError` API in detail.
λ‹€μŒ λΉ„λ””μ˜€λŠ” `setError` API에 λŒ€ν•΄ μžμ„Ένžˆ μ„€λͺ…ν•©λ‹ˆλ‹€.

<YouTube youTubeId="raMqvE0YyIY" />

0 comments on commit ccb5f31

Please sign in to comment.