Form Control
Form Control provides context such as isInvalid
, isDisabled
, and isRequired
to form elements
Import
Chakra UI Vue exports 4 components for Form Control:
CFormControl
: The wrapper that provides context and functionality for all children.CFormLabel
: The label of a form section. The usage is similar to html label.htmlFor
is set by default for children input.CFormHelperText
: The message that tells users more details about the form section.CFormErrorMessage
: The message that shows up when an error occurs.
import { CFormControl, CFormLabel, CFormErrorMessage, CFormHelperText,} from '@chakra-ui/vue-next'
Usage
We'll never share your email.
<CFormControl> <CFormLabel>Email address</CFormLabel> <CInput type='email' /> <CFormHelperText>We'll never share your email.</CFormHelperText></CFormControl>
Error message
CFormErrorMessage
will only show up when the property isInvalid
in
CFormControl
is true.
function errorMessageExample() { const [input, setInput] = useState('') const handleInputChange = (e) => setInput(e.target.value) const isError = input === '' return ( <FormControl isInvalid={isError}> <FormLabel>Email</FormLabel> <Input type='email' value={input} onChange={handleInputChange} /> {!isError ? ( <FormHelperText> Enter the email you'd like to receive the newsletter on.
</FormHelperText> ) : ( <FormErrorMessage>Email is required.</FormErrorMessage> )} </FormControl> )}
Making a field required
By passing the isRequired
props, the Input
field has aria-required
set to
true
, and the FormLabel
will show a red asterisk. This red asterisk can be
overwritten by passing requiredIndicator
to the FormLabel
. If you want to
indicate that a field is optional you can add optionalIndicator
to the
FormLabel
<FormControl isRequired> <FormLabel>First name</FormLabel> <Input placeholder='First name' /></FormControl>
Select Example
<FormControl> <FormLabel>Country</FormLabel> <Select placeholder='Select country'> <option>United Arab Emirates</option> <option>Nigeria</option> </Select></FormControl>
Number Input Example
<FormControl> <FormLabel>Amount</FormLabel> <NumberInput max={50} min={10}> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput></FormControl>
Usage with Form Libraries
Form Libraries like Formik make it so easy to manage form state and validation.
// The below import defines which components come from formik// import { Field, Form, Formik } from 'formik';function FormikExample() { function validateName(value) { let error
if (!value) { error = 'Name is required' } else if (value.toLowerCase() !== 'naruto') { error = "Jeez! You're not a fan 😱" } return error
} return ( <Formik initialValues={{ name: 'Sasuke' }} onSubmit={(values, actions) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)) actions.setSubmitting(false) }, 1000) }} > {(props) => ( <Form> <Field name='name' validate={validateName}> {({ field, form }) => ( <FormControl isInvalid={form.errors.name && form.touched.name}> <FormLabel>First name</FormLabel> <Input {...field} placeholder='name' /> <FormErrorMessage>{form.errors.name}</FormErrorMessage> </FormControl> )} </Field> <Button mt={4} colorScheme='teal' isLoading={props.isSubmitting} type='submit' > Submit
</Button> </Form> )} </Formik> )}
Improvements from v1
- We've improved the accessibility of the
FormControl
component. Here are the changes:id
passed to the form control will be passed to the form input directly.FormLabel
will havehtmlFor
that points to theid
of the form input.FormErrorMessage
addsaria-describedby
andaria-invalid
pointing to the form input.FormHelperText
adds/extendsaria-describedby
pointing to the form input.isDisabled
,isRequired
,isReadOnly
props passed toFormControl
will cascade across all related components.
FormLabel
is now aware of thedisabled
,focused
anderror
state of the form input. This helps you style the label accordingly using the_disabled
,_focus
, and_invalid
style props.- If you render
FormErrorMessage
andisInvalid
isfalse
orundefined
,FormErrorMessage
won't be visible. The only way to make it visible is by passingisInvalid
and setting it totrue
. - You can still supply an
id
prop toFormControl
that will override the randomly generatedid
and attach to thefor
attribute of the label and theid
of the form element. (It also affects theid
attribute of the label) - The combination of
htmlFor
andid
are optional in which adding both will also override the generatedid
sent to the provider.
Edit this page on GitHub