summaryrefslogtreecommitdiff
path: root/docs/guide/quickstart/forms.md
blob: 5356eac0435b39fae53d331e083ff663668eb198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Forms

Forms are created using the [bootstrap-vue form
component](https://bootstrap-vue.org/docs/components/form)
and validated with the [Vuelidate](https://vuelidate.js.org/#sub-installation)
plugin.

## Form component

When creating a new form, use the `<b-form>` [form
component](https://bootstrap-vue.org/docs/components/form). Use the `.prevent`
event modifier on submit to prevent the submit event from reloading the page.

## Form group component

The `<b-form-group>` [form group
component](https://bootstrap-vue.org/docs/components/form-group)
pairs form controls with a legend or label, helper text, invalid/valid
feedback text, and visual validation state feedback. Learn more about
commonly used form components:

- [Form checkbox](https://bootstrap-vue.org/docs/components/form-checkbox)
- [Form input](https://bootstrap-vue.org/docs/components/form-input)
- [Form radio](https://bootstrap-vue.org/docs/components/form-radio)
- [Form select](https://bootstrap-vue.org/docs/components/form-select)
- [Form file custom component](/guide/components/file-upload)

When helper text is provided, use the `<b-form-text>` component and `aria-describedby` on the
form group component the helper text describes.

## Validation

For custom form validation messages, disable browser native HTML5
validation by setting the `novalidate` prop on `<b-form>`. Use Vuelidate to
check the form validation state and add
custom validation messages in the `<b-form-invalid-feedback>` component.

When creating a new form add the `VuelidateMixin`
to the `scripts` block and import any
[validators](https://vuelidate.js.org/#validators) needed
such as: `required`, `requiredIf`, etc. The use of built-in validators is
preferred.

## Complete form

A complete form will look like this.

```vue
<template>
  <b-form novalidate @submit.prevent="handleSubmit">
    <b-form-group
      :label="$t('pageName.form.inputLabel')"
      label-for="form-input-id"
    >
      <b-form-text id="form-input-helper-text">
        {{ $t('pageName.form.helperText') }}
      </b-form-text>
      <b-form-input
        id="form-input-id"
        v-model="form.input"
        type="text"
        aria-describedby="form-input-helper-text"
        :state="getValidationState($v.form.input)"
        @change="$v.form.input.$touch()"
      />
      <b-form-invalid-feedback role="alert">
        <div v-if="!$v.form.input.required">
          {{ $t('global.form.fieldRequired') }}
        </div>
      </b-form-invalid-feedback>
    </b-form-group>
    <b-button variant="primary" type="submit" class="mb-3">
      {{ $t('global.action.save') }}
    </b-button>
  </b-form>
</template>

<script>
import VuelidateMixin from '@/components/Mixins/VuelidateMixin';
import { required } from 'vuelidate/lib/validators';

export default {
  name: 'PageName',
  components: {
  ...
  },
  mixins: [VuelidateMixin],
  data(){
    form: {
      input: '',
    }
  },
  validations() {
    return {
      form: {
        input: { required },
      }
    }
  },
  computed: {
    ...
  },
  created() {
    ...
  },
  methods:{
    handleSubmit() {
      this.$v.$touch();
      if (this.$v.$invalid) return;
      this.$store
        .dispatch(
          'pageName/updateFormData',
          formData
        )
        .then((success) => {
          this.successToast(success);
        })
        .catch(({ message }) => this.errorToast(message))
        .finally(() => {
          this.$v.form.$reset();
          this.endLoader();
        });
    },
};
</script>
```