Form
BootstrapVueNext form component and helper components that optionally support inline form styles and validation states. Pair them up with other BootstrapVueNext form control components for an easy customized, and responsive, layout with a consistent look and feel.
Introduction to forms and controls
Be sure to use an appropriate type
on all inputs (e.g., email
for email address or number
for numerical information) to take advantage of newer input controls like email verification, number selection, and more.
Here is a quick example to demonstrate BootstrapVueNext's form styles. Keep reading for documentation on supported components, form layout, and more.
{ "email": "", "name": "", "food": null, "checked": [] }
<template>
<BForm @submit="onSubmit" @reset="onReset" v-if="show">
<BFormGroup
id="input-group-1"
label="Email address:"
label-for="input-1"
description="We'll never share your email with anyone else."
>
<BFormInput
id="input-1"
v-model="form.email"
type="email"
placeholder="Enter email"
required
/>
</BFormGroup>
<BFormGroup id="input-group-2" label="Your Name:" label-for="input-2">
<BFormInput id="input-2" v-model="form.name" placeholder="Enter name" required />
</BFormGroup>
<BFormGroup id="input-group-3" label="Food:" label-for="input-3">
<BFormSelect id="input-3" v-model="form.food" :options="foods" required />
</BFormGroup>
<BFormGroup id="input-group-4">
<BFormCheckboxGroup v-model="form.checked" id="checkboxes-4">
<BFormCheckbox value="me">Check me out</BFormCheckbox>
<BFormCheckbox value="that">Check that out</BFormCheckbox>
</BFormCheckboxGroup>
</BFormGroup>
<BButton type="submit" variant="primary">Submit</BButton>
<BButton type="reset" variant="danger">Reset</BButton>
</BForm>
<BCard class="mt-3" header="Form Data Result">
<pre class="m-0">{{ form }}</pre>
</BCard>
</template>
<script setup lang="ts">
const foods = [{text: 'Select One', value: null}, 'Carrots', 'Beans', 'Tomatoes', 'Corn']
const form = reactive({
email: '',
name: '',
food: null,
checked: [],
})
const show = ref(true)
const onSubmit = (event) => {
event.preventDefault()
alert(JSON.stringify(form))
}
const onReset = (event) => {
event.preventDefault()
// Reset our form values
form.email = ''
form.name = ''
form.food = null
form.checked = []
// Trick to reset/clear native browser form validation state
show.value = false
nextTick(() => {
show.value = true
})
}
</script>
Inline form
Bootstrap 5 has dropped form-specific layout classes for the grid system. See the Bootstrap 5 Changelog.
To create horizontal forms with the grid add the .row
class to form groups and use the .col-_-_
classes to specify the width of your labels and controls. Be sure to add .col-form-label
to your <label>
s as well, so they’re vertically centered with their associated form controls.
You may need to manually address the width and alignment of individual form controls with spacing utilities (as shown below). Lastly, be sure to always include a <label>
with each form control, even if you need to hide it from non-screenreader visitors with class .visually-hidden
.
<BForm class="d-flex flex-row align-items-center flex-wrap">
<label class="col-form-label visually-hidden" for="inline-form-input-name">Name</label>
<div class="col-lg-3 me-2 my-2">
<BFormInput id="inline-form-input-name" placeholder="Jane Doe" />
</div>
<label class="col-form-label visually-hidden" for="inline-form-input-username"
>Username</label
>
<div class="col-lg-3 me-2 my-2">
<BInputGroup prepend="@" class="col-lg-4">
<BFormInput id="inline-form-input-username" placeholder="Username" />
</BInputGroup>
</div>
<div class="col-lg-3 me-2 my-2">
<BFormCheckbox>Remember me</BFormCheckbox>
</div>
<div class="col-lg-1 my-2">
<BButton variant="primary">Save</BButton>
</div>
</BForm>
Custom form controls and selects are also supported.
<template>
<BForm>
<div class="row">
<label class="col-form-label col-lg-2 me-sm-2" for="inline-form-custom-select-pref"
>Preference</label
>
<div class="col-lg-2">
<BFormSelect
id="inline-form-custom-select-pref"
v-model="customSelect"
class="mb-2 me-sm-2 mb-sm-0"
:options="[{text: 'Choose...', value: null}, 'One', 'Two', 'Three']"
/>
</div>
<BFormCheckbox class="col-form-label col-lg-3 mb-2 me-sm-2 mb-sm-0"
>Remember my preference</BFormCheckbox
>
<div class="col-lg-2 col-form-label">
<BButton variant="primary">Save</BButton>
</div>
</div>
</BForm>
</template>
<script setup lang="ts">
import {ref} from 'vue'
const customSelect = ref(null)
</script>
Floating Labels
Wrap a BFormInput
, BFormTextarea
, or BFormSelect
in a BFormFloatingLable
to enable floating labesl. A placeholder
is required on each input in order to make the Bootstrap 5 css
work correctly.
<BForm>
<BFormFloatingLabel label="Email address" label-for="floatingEmail" class="my-2">
<BFormInput id="floatingEmail" type="email" placeholder="Email address" />
</BFormFloatingLabel>
<BFormFloatingLabel label="Password" label-for="floatingPassword" class="my-2">
<BFormInput id="floatingPassword" type="password" placeholder="Password" />
</BFormFloatingLabel>
</BForm>
Floating labels work correclty for disable state and readonly states. In addition to styled textual inputs, floating labels also work for plaintext inputs, textareas, input groups and selects. See the Bootstrap 5 documentation for more details.
The floating
attribute on the BForm
component only applies to single form controls like this:
<BForm floating>
<BFormInput
id="floatingFormInputValue"
type="email"
placeholder="name@example.com"
/>
<label for="floatingFormInputValue">Input with value</label>
</BForm>
Accessibility
Ensure that all form controls have an appropriate accessible name so that their purpose can be conveyed to users of assistive technologies. The simplest way to achieve this is to use a <label>
element, or—in the case of buttons—to include sufficiently descriptive text as part of the <button>...</button>
content.
For situations where it’s not possible to include a visible <label>
or appropriate text content, there are alternative ways of still providing an accessible name, such as:
<label>
elements hidden using the.visually-hidden
class- Pointing to an existing element that can act as a label using
aria-labelledby
- Providing a title attribute
- Explicitly setting the accessible name on an element using aria-label
If none of these are present, assistive technologies may resort to using the placeholder attribute as a fallback for the accessible name on <input>
and <textarea>
elements. The examples in this section provide a few suggested, case-specific approaches.
While using visually hidden content (.visually-hidden
, aria-label
, and even placeholder content, which disappears once a form field has content) will benefit assistive technology users, a lack of visible label text may still be problematic for certain users. Some form of visible label is generally the best approach, both for accessibility and usability.
Related form control and layout components
See also:
BFormInput
Textual and text-like inputsBFormTextarea
Text area inputsBFormSelect
Select inputBFormRadio
Radio InputsBFormCheckbox
Checkbox InputsBFormFile
File InputBFormSpinbutton
Numerical range spinbutton inputBFormTags
Customizable tag inputBFormRating
Star rating custom form input and display (Not yet implemented)BButton
ButtonsBFormGroup
Form Input wrapper to generate form-groups that support labels, help text and feedbackBInputGroup
Form Inputs with add-onsBFormRow
Create grid rows and columns with tighter margins (available via the Layout and grid components)
Form helper components
The following helper components are available with the Form
plugin:
BFormText
Help text blocks for inputsBFormInvalidFeedback
Invalid feedback text blocks for inputinvalid
statesBFormValidFeedback
Valid feedback text blocks for inputvalid
statesBFormDatalist
Easily create a<datalist>
for use withBFormInput
or plain<input>
Form text helper
Display a block of help text below an input with the BFormText
helper component. text is displayed with a muted color and slightly smaller font-size.
Tip: Help text should be explicitly associated with the form control it relates to using the aria-describedby
attribute. This will ensure that assistive technologies, such as screen readers, will announce this help text when the user focuses or enters the control.
<BForm @submit.stop.prevent>
<label for="text-password">Password</label>
<BFormInput
type="password"
id="text-password"
aria-describedby="password-help-block"
/>
<BFormText id="password-help-block">
Your password must be 8-20 characters long, contain letters and numbers, and must not
contain spaces, special characters, or emoji.
</BFormText>
</BForm>
Feedback helpers
The BFormValidFeedback
and BFormInvalidFeedback
helper components will display feedback (based on input state) as a block of colored text. They rely on being placed after an input (sibling) and will show based on the browser native validation state of the input. To force them to show, set the prop force-show
to true
, or bind the controls state
to the state
prop of the feedback helper, or set the was-validated
class on a parent element (such as a form). See the Validation section below for additional details.
Use the optional Boolean prop tooltip
to change the display from a block to a static tooltip style. The feedback will typically appear below the form control. When this mode is enabled, it is important that the parent container have a position: relative:
css style (or position-relative
class). Note that tooltip style feedback may, since its positioning is static, obscure other inputs, labels, etc.
Note: Some form controls, such as BFormRadio
and BFormCheckbox
have wrapper elements which will prevent the feedback text from automatically showing (as the feedback component is not a direct sibling of the form control's input). Use the feedback component's state
prop (bound to the state of the form control) or the force-show
prop to display the feedback.
<template>
<BForm @submit.stop.prevent>
<label for="feedback-user">User Id</label>
<BFormInput v-model="userId" :state="validation" id="feedback-user" />
<BFormInvalidFeedback :state="validation">
Your user Id must be 5-12 characters long.
</BFormInvalidFeedback>
<BFormValidFeedback :state="validation"> Looks Good. </BFormValidFeedback>
</BForm>
</template>
<script setup lang="ts">
const userId = ref('')
const validation = computed(() => userId.value.length > 4 && userId.value.length < 13)
</script>
Datalist helper
For browsers that support <datalist>
elements, the <BFormDatalist>
helper component will allow you to quickly create a <datalist>
and child <option>
elements via an array passed to the options
prop.
You may also manually provide <option>
elements inside <BFormDatalist>
. They will appear below any <option>
elements generated from the options
prop. Or use the first
slot to place options above the generated options.
<template>
<div>
<label for="input-with-list">Input with datalist</label>
<BFormInput id="input-with-list" list="input-list" />
<BFormDatalist id="input-list" :options="datalistOptions" />
</div>
</template>
<script setup lang="ts">
const datalistOptions = ['Apple', 'Banana', 'Grape', 'Kiwi', 'Orange']
</script>
See also:
<BFormInput> datalist
for datalist usage.<BFormSelect>
options
prop docs for details on the formats and helper props associated withoptions
. Note that<BFormDatalist>
only support a flat list ofBFormSelectOptions
, unlike<BFormSelect>
which support a heirarchy ofBFormSelectOption
andBFormSelectOptionGroup
.
Validation
Disable browser native HTML5 validation by setting the novalidate
prop to true on BForm
.
Set the validated
prop, on BForm
, to true
to add the Bootstrap v5 .was-validated
class to the form to trigger validation states.
All form controls support a state
prop, which can be used to set the form control into one of three contextual states:
false
(denotes invalid state) is great for when there is a blocking or required field. A user must fill in this field properly to submit the formtrue
(denotes valid state) is ideal for situations when you have per-field validation throughout a form and want to encourage a user through the rest of the fieldsnull
Displays no validation state (neither valid nor invalid)
Refer to the Bootstrap v5 Form Validation Documentation for details on the Bootstrap v5 validation states.
Component Reference
<BForm>
Prop | Type | Default | Description |
---|---|---|---|
floating | boolean | false | When set, renders a single control form with a floating label. This only works for forms where the immediate children are a label and one of the supported controls. See above for details. |
id | string | undefined | Used to set the `id` attribute on the rendered content, and used as the base to generate any additional element IDs as needed |
novalidate | boolean | false | When set, disables browser native HTML5 validation on controls in the form |
validated | boolean | false | When set, adds the Bootstrap class 'was-validated' on the form, triggering the native browser validation states |
Event | Args | Description |
---|---|---|
submit | submit : Event - Native submit event | Emitted when the form is submitted |
Name | Scope | Description |
---|---|---|
default | Contet to place in the form |
<BFormDatalist>
Prop | Type | Default | Description |
---|---|---|---|
disabled-field | string | 'disabled' | Field name in the `options` array that should be used for the disabled state |
html-field | string | 'html' | Field name in the `options` array that should be used for the html label instead of text field |
id | string | undefined | Used to set the `id` attribute on the rendered content, and used as the base to generate any additional element IDs as needed |
options | readonly (unknown | Record<string, unknown>)[] | '() => []' | Array of items to render in the component. Note that BFormDatalist only supports Options, not OptionsGroups |
text-field | string | 'text' | Field name in the `options` array that should be used for the text label |
value-field | string | 'value' | Field name in the `options` array that should be used for the value |
Name | Scope | Description |
---|---|---|
default | Content to place in the from datalist | |
first | Slot to place options above options provided via the 'options' prop |
<BFormFloatingLabel>
Prop | Type | Default | Description |
---|---|---|---|
label | string | undefined | The text of the floating label |
label-for | string | undefined | The id of the input control that the floating label is for |
Name | Scope | Description |
---|---|---|
default | The input control that contains the floating label | |
label | The content to display in the floating label |
<BFormInvalidFeedback>
Prop | Type | Default | Description |
---|---|---|---|
aria-live | string | undefined | When the rendered element is an `aria-live` region (for screen reader users), set to either 'polite' or 'assertive' |
force-show | boolean | false | Shows the feedback text, regardless of the value of the 'state' prop |
id | string | undefined | Used to set the `id` attribute on the rendered content, and used as the base to generate any additional element IDs as needed |
role | string | undefined | Sets the ARIA attribute `role` to a specific value |
state | boolean | null | undefined | Controls the validation state appearance of the component. `true` for valid, `false` for invalid, or `null` for no validation state |
tag | string | 'div' | Specify the HTML tag to render instead of the default tag |
text | string | undefined | The feedback text to display |
tooltip | boolean | false | Renders the feedback text in a rudimentary tooltip style |
Name | Scope | Description |
---|---|---|
default | Content to place in the form invalid feedback |
<BFormRow>
Prop | Type | Default | Description |
---|---|---|---|
tag | string | 'div' | Specify the HTML tag to render instead of the default tag |
Name | Scope | Description |
---|---|---|
default | Content to place in the form row |
<BFormText>
Prop | Type | Default | Description |
---|---|---|---|
id | string | undefined | Used to set the `id` attribute on the rendered content, and used as the base to generate any additional element IDs as needed |
inline | boolean | false | When set, renders the help text as an inline element, rather than a block element |
tag | string | 'div' | Specify the HTML tag to render instead of the default tag |
text | string | undefined | The text to display |
text-variant | TextColorVariant | null | null | Applies one of the Bootstrap theme color variants to the text |
Name | Scope | Description |
---|---|---|
Content to place in the form text |
<BFormValidFeedback>
Prop | Type | Default | Description |
---|---|---|---|
aria-live | string | undefined | When the rendered element is an `aria-live` region (for screen reader users), set to either 'polite' or 'assertive' |
force-show | boolean | false | Shows the feedback text, regardless of the value of the 'state' prop |
id | string | undefined | Used to set the `id` attribute on the rendered content, and used as the base to generate any additional element IDs as needed |
role | string | undefined | Sets the ARIA attribute `role` to a specific value |
state | boolean | null | undefined | Controls the validation state appearance of the component. `true` for valid, `false` for invalid, or `null` for no validation state |
tag | string | 'div' | Specify the HTML tag to render instead of the default tag |
text | string | undefined | The feedback text to display |
tooltip | boolean | false | Renders the feedback text in a rudimentary tooltip style |
Name | Scope | Description |
---|---|---|
default | Content to place in the form invalid feedback |