【问题标题】:How to validate radio buttons in a b-form-radio-group in vuejs如何在 vuejs 中验证 b-form-radio-group 中的单选按钮
【发布时间】:2019-11-15 18:57:13
【问题描述】:

我有一组 b-form-radio-group 单选按钮,我如何验证它们,因为其中一个必须检查?

这是 b-modal 中 b-form-radio-group 的一个 div

<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>

当我点击“确定”按钮时,如果我没有选择任何单选按钮,b-modal 应该会警告我。

【问题讨论】:

    标签: vue.js bootstrap-vue


    【解决方案1】:

    您需要添加state 属性。您还可以使用 b-form-invalid-feedback b-form-valid-feedback 插槽来接收消息:

    <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)    
        }
    }
    ...    
    

    您可以在文档中找到更多信息:https://bootstrap-vue.js.org/docs/components/form-radio/#contextual-state-with-feedback-example

    【讨论】:

      【解决方案2】:

      在您的updateQuantity 方法中,您可以检查您的quantityOption 是否已设置。

      您还可以实现 Andriy 的答案以获得更直观的表示,但您仍然需要检查事件以确保它是否已设置。

      <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>
      

      【讨论】:

        猜你喜欢
        • 2020-02-10
        • 2021-07-08
        • 1970-01-01
        • 2013-01-22
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        • 2020-02-10
        • 2017-11-23
        相关资源
        最近更新 更多