I am working with three v-switch
elements, each of them is tied to the switch1
property.
Whenever I click on a v-switch
, it either adds or removes a value from switch1
.
Is there a way to make a v-switch
mandatory? In other words, it should not be possible to uncheck all of them. At least one must always be selected.
I was thinking of implementing an event that checks if switch1
will be an empty array, and then cancels the switch click.
I attempted to achieve this using the change event, but the e
parameter comes as a Boolean instead of the event object.
new Vue({
el: '#app',
vuetify: new Vuetify(),
data () {
return {
switch1: ['val1', 'val2', 'val3'],
change: (e) => { console.log({ e }) }
}
},
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aec8c1c0daee9a80d6">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Material+Icons" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f88e8d9d8c919e81b8cad6c9d6c9cc">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1066657550223e68">[email protected]</a>/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="592f2c3c2d303f20196b776877686d">[email protected]</a>/dist/vuetify.min.js"></script>
<div id="app">
<v-app id="inspire">
<v-container class="px-0" fluid>
{{ switch1 }}
<v-switch
v-model="switch1"
label="val1"
value="val1"
@change="change($event)"
></v-switch>
<v-switch
v-model="switch1"
label="val2"
value="val2"
@change="change($event)"
></v-switch>
<v-switch
v-model="switch1"
label="val3"
value="val3"
@change="change($event)"
></v-switch>
</v-container>
</v-app>
</div>