Skip to content

Commit f496e41

Browse files
authored
Merge pull request #2 from bennlongg/main
👋 Submitting state and required fields
2 parents 806f0b4 + 61bfac6 commit f496e41

File tree

8 files changed

+198
-466
lines changed

8 files changed

+198
-466
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ To use it you have to import it in your Form component. Then call it with the fo
2424

2525
And at the end, attach the handleSubmit function to the onSubmit event.
2626

27-
```bash
27+
```typescript
2828
import { useForm } from "formbold-react";
2929

3030
function Form() {
@@ -41,7 +41,7 @@ function Form() {
4141
<label htmlFor="email">Email Address</label>
4242
<input id="email" type="email" name="email" required />
4343
<textarea id="message" name="message" required />
44-
<button type="submit">Submit</button>
44+
<button type="submit">{state.submitting ? "Submitting..." : "Submit"}</button>
4545

4646
<div>
4747
{state.error && state.error.message}
@@ -54,6 +54,34 @@ function Form() {
5454
export default Form;
5555
```
5656

57+
## Required fields
58+
59+
To make certain fields mandatory in your form, you can use the `requiredFields` option when using the `useForm` hook. In the example below, the `email` field is set as a required field:
60+
61+
```typescript
62+
const [state, handleSubmit] = useForm("form_id", { requiredFields: ["email"] });
63+
```
64+
This ensures that the form cannot be submitted unless the `email` field is filled out by the user.
65+
66+
67+
## Custom error messages
68+
You can customize the error messages displayed when certain fields are not filled out in your form. By using the `errorMessages` option in the `useForm` hook, you can provide custom error messages for different scenarios.
69+
70+
Here's an example of how you can set custom error messages for the `name` and `email` fields:
71+
72+
```typescript
73+
const [state, handleSubmit] = useForm("form_id", {
74+
requiredFields: ["name", "email"],
75+
errorMessages: {
76+
empty: "Please fill the form!",
77+
required: fields => `Please fill the required fields: ${fields.join(", ")}`,
78+
}
79+
});
80+
```
81+
82+
Feel free to customize the error messages according to your specific requirements.
83+
84+
5785
## ****Useful Links and Information****
5886

5987
For more information visit the [documentation](https://formbold.com/docs).

0 commit comments

Comments
 (0)