Skip to content
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

👋 Submitting state and required fields #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To use it you have to import it in your Form component. Then call it with the fo

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

```bash
```typescript
import { useForm } from "formbold-react";

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

<div>
{state.error && state.error.message}
Expand All @@ -54,6 +54,34 @@ function Form() {
export default Form;
```

## Required fields

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:

```typescript
const [state, handleSubmit] = useForm("form_id", { requiredFields: ["email"] });
```
This ensures that the form cannot be submitted unless the `email` field is filled out by the user.


## Custom error messages
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.

Here's an example of how you can set custom error messages for the `name` and `email` fields:

```typescript
const [state, handleSubmit] = useForm("form_id", {
requiredFields: ["name", "email"],
errorMessages: {
empty: "Please fill the form!",
required: fields => `Please fill the required fields: ${fields.join(", ")}`,
}
});
```

Feel free to customize the error messages according to your specific requirements.


## ****Useful Links and Information****

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