Skip to content

Commit 7b70b96

Browse files
author
Jeff Sagal
authored
add required validation doc (sagalbot#868)
1 parent 8293f2b commit 7b70b96

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<template>
2+
<form @submit.stop="onSubmit">
3+
<v-select :options="books" label="title" v-model="selected">
4+
<template #search="{attributes, events}">
5+
<input
6+
:required="!selected"
7+
class="vs__search"
8+
v-bind="attributes"
9+
v-on="events"
10+
/>
11+
</template>
12+
</v-select>
13+
14+
<input type="submit">
15+
</form>
16+
</template>
17+
18+
<script>
19+
import books from '../data/books'
20+
export default {
21+
data: () => ({
22+
books,
23+
selected: null,
24+
}),
25+
methods: {
26+
onSubmit() {
27+
alert('Submitted!');
28+
}
29+
}
30+
};
31+
</script>
32+
33+
<style scoped>
34+
form {
35+
display: flex;
36+
align-items: stretch;
37+
}
38+
39+
.v-select {
40+
width: 75%;
41+
}
42+
43+
input[type="submit"] {
44+
margin-left: 1rem;
45+
background: #44ae7d;
46+
border: none;
47+
border-radius: 3px;
48+
color: #fff;
49+
width: 20%;
50+
}
51+
</style>

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ module.exports = {
117117
title: 'Digging Deeper',
118118
collapsable: false,
119119
children: [
120+
['guide/validation', 'Validation'],
120121
['guide/vuex', 'Vuex'],
121122
['guide/ajax', 'AJAX'],
122123
],

docs/guide/validation.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Required
2+
3+
If you need to ensure that a selection is made before a form is submitted, you can
4+
use the `required` attribute in combination with the `search` scoped slot in order
5+
to do so.
6+
7+
However, the `search` input within the component does not actually store a value, so
8+
simply adding the `required` attribute won't work. Instead, we'll bind the attribute
9+
dynamically, so that it's only present if we don't have a selection.
10+
11+
<ValidationRequired />
12+
13+
```html
14+
<v-select :options="books" label="title" v-model="selected">
15+
<template #search="{attributes, events}">
16+
<input
17+
class="vs__search"
18+
:required="!selected"
19+
v-bind="attributes"
20+
v-on="events"
21+
/>
22+
</template>
23+
</v-select>
24+
```

0 commit comments

Comments
 (0)