MuiSelectElement
Use MUI Select with react-hook-form without writing a Controller. MuiSelectElement binds the selected value, validation and error state to the form.
MuiSelectElement is MUI's Select connected to React Hook Form. It stores the selected
MenuItem's value, or an array of values with multiple, and sets the Select's error state when
validation fails. New to the adapters? Start with
Using MUI with React Hook Form.
Import
import { MuiSelectElement } from '@piplup/rhf-adapters/mui-material';Usage
'use client';import MenuItem from '@mui/material/MenuItem';import Stack from '@mui/material/Stack';import { MuiSelectElement, MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { useForm } from 'react-hook-form';export default function Page() { const { control, handleSubmit } = useForm({ defaultValues: { role: 'user' }, }); return ( <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}> <Stack spacing={2}> <MuiSelectElement control={control} name="role"> <MenuItem value="user">User</MenuItem> <MenuItem value="admin">Admin</MenuItem> </MuiSelectElement> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> );}Labels and error messages
MUI's Select has no label or helper text of its own; both come from the surrounding
FormControl. The element sets the error state, and
MuiFormHelperTextElement
renders the message for the same field:
<FormControl fullWidth>
<InputLabel id="role-label">Role</InputLabel>
<MuiSelectElement
control={control}
name="role"
labelId="role-label"
label="Role"
required
messages={{ required: 'Pick a role' }}
>
<MenuItem value="viewer">Viewer</MenuItem>
<MenuItem value="editor">Editor</MenuItem>
</MuiSelectElement>
<MuiFormHelperTextElement control={control} name="role" renderOnError />
</FormControl>With renderOnError, the helper text renders nothing until the field has an error. Without it,
its children show as a hint while the field is valid and the error replaces them, which is how
helperText behaves on a TextField.
Give a single Select a default value of "". It counts as empty, so required reports an
untouched Select, and MUI renders it as "nothing selected" instead of warning about an
out-of-range value.
Multiple selection
With multiple, the field stores an array of the selected values:
<FormControl fullWidth>
<InputLabel id="languages-label">Languages</InputLabel>
<MuiSelectElement
control={control}
name="languages"
labelId="languages-label"
label="Languages"
multiple
required
messages={{ required: 'Pick at least one language' }}
>
<MenuItem value="en">English</MenuItem>
<MenuItem value="fr">French</MenuItem>
</MuiSelectElement>
<MuiFormHelperTextElement control={control} name="languages">
Pick every language you speak
</MuiFormHelperTextElement>
</FormControl>A value that isn't an array, such as a missing default, is shown as no selection. required
fails on an empty array. maxLength doesn't apply to arrays, so cap the number of choices with a
validate function in rules; the
useMuiSelectAdapter demo does
exactly that.
Props
Prop
Type
MuiSelectElement supports props of the MUI Select component, along with additional adapter-specific props. For a complete list of available Select properties, refer to the MUI API documentation.
The
refis forwarded to the underlying<Select />component.
Notes
- For custom select UIs, build on
useMuiSelectAdapter. Unlike the element, the hook returnshelperText, which holds the error message when you passcomposeHelperText. - To store something other than the selected
value, usetransform.
FAQ
How do I use MUI Select with React Hook Form?
Render MuiSelectElement with control, a field name and your MenuItems as children. It stores the selected item's value in the form and marks the Select as invalid when validation fails. Put it in a FormControl with an InputLabel for the usual MUI label.
Why doesn't MuiSelectElement show an error message?
MUI's Select has no helperText prop, so the element sets the error state but can't render the message. Add MuiFormHelperTextElement with the same name inside the FormControl; it shows the field's error, and with renderOnError it renders nothing until there is one.
What default value should a Select field have?
Use an empty string for a single Select and an empty array for multiple. An empty string also fails required, so an untouched Select is reported as missing.
How do I validate a multiple Select?
required fails on an empty array. maxLength doesn't apply to arrays, so limit the number of choices with a validate function in rules, as the useMuiSelectAdapter demo does.
See also
MuiRatingElement
Use MUI Rating with react-hook-form without writing a Controller. MuiRatingElement binds the star value and validation to a form field.
MuiSliderElement
Use MUI Slider with react-hook-form without writing a Controller. MuiSliderElement binds the slider value and validation to a form field.