0

anyone can point me to an example of a react multiple checkboxes validation? I have some questions, every one of them has a checkbox when it's done and when all are checked, a continue button must be activated.. I know how to do this with only one checkbox, but don't know how to handle more of them. I would rather do this in plain react and not by installing any package. Any help will be appreciated.

2
  • you can use YUP for form validation. github.com/jquense/yup Commented Aug 15, 2020 at 19:20
  • I would rather do this in plain react Commented Aug 15, 2020 at 20:07

3 Answers 3

1

You can control all your inputs using useState. Example for two inputs.

import React, { useState } from "react"

const ControlledCheckboxes = () => {

    const [ firstCheckbox, setFirstCheckbox ] = useState(false);
    const [ secondCheckbox, setSecondCheckbox ] = useState(false);

    return(
        <form>
            <div >

                <div className="form-check">
                    <input type="checkbox" id="first" className="form-check-input"
                           onClick={()=>setFirstCheckbox(!firstCheckbox)}
                           value={firstCheckbox}
                    />
                    <label className="form-check-label" htmlFor="first">FIRST</label>

                </div>
                <div className="form-check">
                    <input type="checkbox" id="second" className="form-check-input"
                           onClick={()=>setSecondCheckbox(!secondCheckbox)}
                           value={secondCheckbox}
                    />
                    <label className="form-check-label" htmlFor="second">SECOND</label>
                </div>
            </div>
            <button type="submit"  className="btn btn-outline-success" disabled={ !(firstCheckbox && secondCheckbox) }  >SUBMIT</button>
        </form>
    )

};
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer, it's really helpful, but if I have 6 or 7 checkboxes how can I handle this in a cleaner way? I'm not sure that if I put there 7 useStates it's an ok solution.
@Christian, can you have an array of checkbox names like const names = ["first", "second"]
0

You can achieve this by using controlled inputs.

Basically you would make the value of the checkboxes correspond to variables in the state of your component. After that it is as simple as if (boolean1 && boolean2) with a conditionally rendered save button.

Controlled inputs

Conditional rendering

2 Comments

Actually this is what I was thinking of doing, but I'm not sure how can I implement multiple checkboxes (aprox 6) within the state. I'm not sure that putting 6 variables in state it's a clean solution, that's why an example would be very helpful.
You could use an array of variables
0

You can achieve most of what you want with just HTML.

<form>
  <input type="checkbox" required />
  <input type="checkbox" required />
  <button>submit</button>
</form>

The form cannot be submitted until all checkboxes are checked.

Let's say that's not enough, you want to access the form validity in your render logic to apply styles or whatever so you need component state.

const [valid, setValid] = useState(false);

All form control change events bubble up to their form (unless explicitly stopped). We can spy on form control changes by adding a change event listener that updates component state to the form element.

event => setValid(event.target.form.checkValidity())

If your form isn't as simple as my example and you need to specifically check certain checkboxes you can find all form controls in event.target.form.elements. You can use the elements property to not even need HTML form validation.

event => setValid(Array.from(event.target.form.elements).every(
  input => input.type !== 'checkbox' || input.checked
))

You can then pass valid as a prop to your submit button.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.