|
| 1 | +# Next.js with react-bootstrap and form validation |
| 2 | + |
| 3 | +[Starting from this example](https://git.1-hub.cnvercel/next.js/tree/canary/examples/with-react-bootstrap), this project was my experiment to see how form validation could be done easily and effectively with [React Bootstrap](https://react-bootstrap.netlify.app/) as the UI framework. |
| 4 | + |
| 5 | +I tried two scenarios where my goal was to minimize complexity, tediousness, and stay as true to React/HTML5 as possible. |
| 6 | + |
| 7 | +[React Hook Form](https://react-hook-form.com/) is minimally intrusive, easy to use, and is fairly flexible. With React Bootstrap it only worked with the "ref" approach using [register](https://react-hook-form.com/api#register). The [Controller](https://react-hook-form.com/api#Controller) approach didn't work for me. The downside to the "ref" approach is that I'm guessing it is using an uncontrolled strategy, which is [not recommended by React](https://reactjs.org/docs/uncontrolled-components.html). |
| 8 | + |
| 9 | +The other approach, which I ended up preferring, was to use [React Bootstrap's support for native HTML5 validation](https://react-bootstrap.netlify.app/components/forms/#forms-validation-native). It was easy to activate and not surprisingly integrates more easily with Bootstrap's `Form.Control.Feedback` component. What really sold me on this approach is that HTML5 already provides a `validationMessage`, [described here](https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation#Validating_forms_using_JavaScript), that conveys a helpful description of why the field is invalid. |
| 10 | + |
| 11 | +Here is the view of the required field after the initial form submission: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +As typing characters, but still below the `minLength={3}` declaration: |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +Finally, when typing at least another character the rendering flips to a positive indication and feedback removal: |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +With that knowledge I wrapped up the potential repetition in a little helper component that paired a `Form.Control` with a `Form.Control.Feedback`. That helper component made use of the `validationMessage`, the `name` on the input, and the [`invalid` event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/invalid_event) fired by inputs during validation. |
| 24 | + |
| 25 | +Using that helper component each input is about 10 lines, which is quite acceptable, for example: |
| 26 | + |
| 27 | +```jsx |
| 28 | +<ValidatedFormControl onChange={handleChange} |
| 29 | + value={values.yourName} |
| 30 | + name="yourName" |
| 31 | + type="text" placeholder="Enter your name" |
| 32 | + required |
| 33 | + minLength={3} |
| 34 | +/> |
| 35 | +``` |
| 36 | + |
| 37 | +That `ValidatedFormControl` doesn't require much code itself: |
| 38 | +```jsx |
| 39 | +function ValidatedFormControl({ |
| 40 | + value, |
| 41 | + onChange, |
| 42 | + ...props |
| 43 | +}) { |
| 44 | + const [message, setMessage] = useState(); |
| 45 | + const { controlId } = useContext(FormContext); |
| 46 | + |
| 47 | + return ( |
| 48 | + <> |
| 49 | + <Form.Control {...props} |
| 50 | + value={value} |
| 51 | + onChange={event => { |
| 52 | + onChange(event.target.value, event.target.name || controlId); |
| 53 | + event.target.checkValidity(); |
| 54 | + }} |
| 55 | + onInvalid={event => setMessage( |
| 56 | + event.target.validationMessage)} |
| 57 | + /> |
| 58 | + <Form.Control.Feedback type="invalid"> |
| 59 | + {message} |
| 60 | + </Form.Control.Feedback> |
| 61 | + </> |
| 62 | + ) |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Deploy your own |
| 67 | + |
| 68 | +Deploy this project using [Vercel](https://vercel.com): |
| 69 | + |
| 70 | +[](https://vercel.com/import/project?template=https://git.1-hub.cnitzg/nextjs-bootstrap-validation) |
0 commit comments