1

to use Material-ui in react-hook-form you should use <Controller and the method render instead of "as = {xy-control}" Also should not mix controller and inputRef = {register}. A single control is also no problem. But there is a compound control in Material-ui. "FormControlLabel + CheckBox" how do you integrate this control in the controller. All my attempts have failed.

This is how it works but I would like the FormControlLaben to be enclosed by the controller. Does somebody has any idea?

                 <Controller
                    name="password"
                    control={control}
                    defaultValue={""}
                    render={(props) => <TextField {...props}
                                                  variant="outlined"
                                                  margin="normal"
                                                  required
                                                  fullWidth
                                                  label="Password"
                                                  type="password"
                                                  id="password"
                                                  autoComplete="current-password"
                    />}
                />

                <FormControlLabel
                    control={
                        <Checkbox
                            inputRef={register}
                            name="remember"
                        />
                    }
                    label="remember"
                />


{/*That works, but it requires an OnChange. Why can't the controller bind it?*/}
                <FormControlLabel
                    control={
                        <Controller
                            name={"remember2"}
                            control={control}
                            render={(props) => (
                                <Checkbox
                                    {...props}
                                    onChange={(e) => props.onChange(e.target.checked)}

                                />
                            )}
                        />
                    }
                    label="remember"
                />

5 Answers 5

5

this was enough for me w/ Mui 5 & RHF 7

const { watch, register } = useForm<{saveEmail: boolean}>({
  defaultValues: { saveEmail: true },
});

...

<FormControlLabel
  control={
    <Checkbox {...register('saveEmail')} checked={watch('saveEmail')} />
  }
  label="save email"
/>
Sign up to request clarification or add additional context in comments.

2 Comments

Using watch worked perfectly ty. I most upvoted answer didn't work for me. I don't need to use Controller with your answer eacher. Much simpler!
Nice this also works for select to get rid of the controller tag
3

Here's how I did it

<FormControlLabel
  control={
    <Controller
      control={control}
      inputRef={register}
      name='controlName'
      render={({onChange, value}) => (
        <Checkbox
          onChange={e => onChange(e.target.checked)}
          checked={value}
        />
      )}
    />
  }
  label="This is a label"
/>

Comments

3

From v7, I used this :

    <Controller
        name='contactAutre'
        control={control}
        defaultValue={false}
        render={({ field }) => (
            <FormControlLabel
                control={<Checkbox {...field} />}
                label='Autre'
            />
        )}
    />

Comments

1

Another way to make things more controlled:

<FormControlLabel
label="label here"
control={
    <Controller
    name="isSeparatedDelivery"
    control={control}
    render={({ field: { onChange, value } }) => (
        <Checkbox
        checked={value}
        onChange={(e) => onChange(e.target.checked)}
        />
    )}
    />
}
/>

or

<Controller
control={control}
name="selected"
render={({ field: { onChange, value } }) => (
  <FormControlLabel
    control={
      <Checkbox
        label="Sélectionner la question"
        checked={value}
        onChange={onChange}
      />
    }
    label="Sélectionner la question"
  />
)}
/>

Comments

0

Credit to Leo Roese for his example for Textfield. However you can apply the same logic for Checkboxes but slightly tweak.

const { control, formState: { errors } } = useForm()

<Controller
    name="privacyAccept"
    control={control}
    render={({ field }) => (
    <>
        <FormControlLabel
        control={<Checkbox {...field} />}
        label="I agree to the privacy policy"
        />
        {errors.privacyAccept && (
        <FormHelperText error>{errors.privacyAccept.message}</FormHelperText>
        )}
    </>
    )}
/>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.