Reputation: 73
I have a b-form-radio-group of radio buttons, how can I validate them as one of them must be checked?
Here is a div of b-form-radio-group inside the b-modal
<b-modal id="manageQuantity" title="Manage Quantity" @ok="updateQuantity">
<div class="radio-button">
<b-form-group
id="quantityOption"
label-cols-sm="3"
label="Option :"
label-for="input-horizontal"
>
<b-form-radio-group
id="quantityOption"
class="individual-button"
buttons
button-variant="outline-secondary"
v-model="form.quantityOption"
:options="quantityOptions"
></b-form-radio-group>
</b-form-group>
</div>
</b-modal>
When I click "OK" button the b-modal should warn me if I did not select any of the radio button.
Upvotes: 0
Views: 5231
Reputation: 10324
In your updateQuantity
method you can check if your quantityOption
is set or not.
You could also implement Andriy's answer for a more visual representation, but you still need the check in the event to make sure it's set or not.
<div>
<b-btn v-b-modal.modal>Open Modal</b-btn>
<b-modal id="modal" @ok="updateQuantity">
<b-form-radio-group
buttons
button-variant="outline-secondary"
v-model="form.quantityOption"
:options="quantityOptions"
></b-form-radio-group>
</b-modal>
</div>
<script>
data() {
return {
form: { quantityOption: null },
quantityOptions: [
{value: 1, text: '1' },
{value: 2, text: '2' },
{value: 3, text: '3' }
]
}
},
methods: {
updateQuantity(event) {
if(!this.form.quantityOption){
alert('Please select one of the options first')
event.preventDefault()
}
}
}
</script>
Upvotes: 0
Reputation: 8263
You need to add state
property. You also can use the b-form-invalid-feedback
b-form-valid-feedback
slots for the messages:
<b-form-radio-group
id="quantityOption"
class="individual-button"
buttons
button-variant="outline-secondary"
v-model="form.quantityOption"
:options="quantityOptions"
:state="state"
>
<b-form-invalid-feedback :state="state">Please select one</b-form-invalid-feedback>
<b-form-valid-feedback :state="state">Thank you</b-form-valid-feedback>
</b-form-radio-group>
....
data(){
return{
form:{
quantityOption: null
}
}
}
...
computed: {
state() {
return Boolean(this.form.quantityOption)
}
}
...
You could find more in the documentation: https://bootstrap-vue.js.org/docs/components/form-radio/#contextual-state-with-feedback-example
Upvotes: 1