2017-11-25 40 views
0

選択リストを交換するためにフォーム使用ラジオがありますが、検証メッセージが正しく動作していないようです。 これは私の形態であり、タイプAの検証メッセージが仕事である: enter image description hereラジオボタン交換で選択リストの検証にvee-validateを使用する

が、私は検証メッセージが動作していないタイプBラジオボタンを変更する場合: enter image description here

とも私はボタンとIFタイプAを提出する]をクリックした場合検証正しくないと私はそれを提出するTYPEBする変更のみタイプAを検証するように見えるので、検証が合格しないだろう...ここ

とは、私のコードです:

<form id="form" @submit.prevent="validateBeforeSubmit"> 
 
    <label>Type A</label> 
 
    <input type="radio" v-model="Type" value="TypeA" /> 
 
    <label>Type B</label> 
 
    <input type="radio" v-model="Type" value="TypeB" /> 
 

 
    <table> 
 
     <tr v-if="Type==='TypeA'"> 
 
      <td> 
 
       <select v-model="TypeA" v-validate="'required|not_in:Choose'" name="TypeA"> 
 
        <option v-for="option in TypeAOptions" v-bind:value="option.value"> 
 
         {{ option.value }} 
 
        </option> 
 
       </select> 
 
       <span v-if="errors.has('TypeA')"> 
 
        {{ errors.first('TypeA')}} 
 
       </span> 
 
      </td> 
 
     </tr> 
 
     <tr v-if="Type==='TypeB'"> 
 
      <td> 
 
       <select v-model="TypeB" v-validate="'required|not_in:Choose'" name="TypeB"> 
 
        <option v-for="option in TypeBOptions" v-bind:value="option.value"> 
 
         {{ option.value }} 
 
        </option> 
 
       </select> 
 
       <span v-if="errors.has('TypeB')"> 
 
        {{ errors.first('TypeB')}} 
 
       </span> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
    <button type="submit">Submit</button> 
 
</form> 
 

 
<script src="https://cdn.jsdelivr.net/npm/vue"></script> 
 
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vee-validate.js"></script> 
 

 
<script> 
 
    Vue.use(VeeValidate); 
 
    var form = new Vue({ 
 
     el: '#form', 
 
     data: { 
 
      Type: 'TypeA', 
 
      TypeA: 'Choose', 
 
      TypeAOptions: [{ 
 
        value: 'Choose' 
 
       }, 
 
       { 
 
        value: 'A', 
 
       }, 
 
       { 
 
        value: 'B' 
 
       }, 
 
       { 
 
        value: 'C' 
 
       }, 
 
       { 
 
        value: 'D' 
 
       } 
 
      ], 
 

 
      TypeB: 'Choose', 
 
      TypeBOptions: [{ 
 
        value: 'Choose' 
 
       }, 
 
       { 
 
        value: '1' 
 
       }, 
 
       { 
 
        value: '2' 
 
       }, 
 
       { 
 
        value: '3' 
 
       }, 
 
       { 
 
        value: '4' 
 
       } 
 
      ], 
 
     }, 
 
     methods: { 
 
      validateBeforeSubmit() { 
 
       this.$validator.validateAll().then((result) => { 
 
        if (result) { 
 
         alert("Submit Success"); 
 
         return; 
 
        } 
 
        alert("Correct them errors!"); 
 
       }); 
 
      } 
 
     } 
 
    }) 
 
</script>

私はこの問題を解決する方法がわからない、誰かが私を助けることができますか?

答えて

1

v-if DOM要素を追加/削除し、DOMのチェックを検証するため、v-showの代わりにv-ifを使用してください。

<form id="form" @submit.prevent="validateBeforeSubmit"> 
 
    <label>Type A</label> 
 
    <input v-on:change="changeType" type="radio" v-model="Type" value="TypeA" /> 
 
    <label>Type B</label> 
 
    <input v-on:change="changeType" type="radio" v-model="Type" value="TypeB" /> 
 

 
    <table> 
 
     <tr v-show="Type==='TypeA'"> 
 
      <td> 
 
       <select v-model="TypeA" v-validate="'required|not_in:Choose'" name="TypeA"> 
 
        <option v-for="option in TypeAOptions" v-bind:value="option.value"> 
 
         {{ option.value }} 
 
        </option> 
 
       </select> 
 
       <span v-if="errors.has('TypeA')"> 
 
        {{ errors.first('TypeA')}} 
 
       </span> 
 
      </td> 
 
     </tr> 
 
     <tr v-show="Type==='TypeB'"> 
 
      <td> 
 
       <select v-model="TypeB" v-validate="'required|not_in:Choose'" name="TypeB"> 
 
        <option v-for="option in TypeBOptions" v-bind:value="option.value"> 
 
         {{ option.value }} 
 
        </option> 
 
       </select> 
 
       <span v-if="errors.has('TypeB')"> 
 
        {{ errors.first('TypeB')}} 
 
       </span> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
    <button type="submit">Submit</button> 
 
</form> 
 

 
<script src="https://cdn.jsdelivr.net/npm/vue"></script> 
 
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vee-validate.js"></script> 
 

 
<script> 
 
    Vue.use(VeeValidate); 
 
    var form = new Vue({ 
 
     el: '#form', 
 
     data: { 
 
      Type: 'TypeA', 
 
      TypeA: 'Choose', 
 
      TypeAOptions: [{ 
 
        value: 'Choose' 
 
       }, 
 
       { 
 
        value: 'A', 
 
       }, 
 
       { 
 
        value: 'B' 
 
       }, 
 
       { 
 
        value: 'C' 
 
       }, 
 
       { 
 
        value: 'D' 
 
       } 
 
      ], 
 

 
      TypeB: 'Choose', 
 
      TypeBOptions: [{ 
 
        value: 'Choose' 
 
       }, 
 
       { 
 
        value: '1' 
 
       }, 
 
       { 
 
        value: '2' 
 
       }, 
 
       { 
 
        value: '3' 
 
       }, 
 
       { 
 
        value: '4' 
 
       } 
 
      ], 
 
     }, 
 
     computed:{ 
 
     }, 
 
     methods: { 
 
      changeType:function(){ 
 
       this.errors.clear(); 
 
      }, 
 
      validateBeforeSubmit() { 
 
       this.$validator.validate(this.Type).then((result) => { 
 
        if (result) { 
 
         alert("Submit Success"); 
 
         return; 
 
        } 
 
        alert("Correct them errors!"); 
 
       }); 
 
      } 
 
     } 
 
    }) 
 
</script>

また、あなたはすべてのフィールドを検証する両方のドロップダウンが確認された時点で意味。

これを機能させるには私はこの行を変更したときに一度に1つのドロップダウンだけを検証することを意味します。

this.$validator.validateAll() 

から

this.$validator.validate(this.Type) 
+0

ありがとうございました。 –

+0

ありがとうございます:実際に私はこのバリデータを使用したことはありませんでした。あなたのためにチェックしました:)これは非常に便利です – C2486

0

へ 私は、TDタグでV-場合と同じ条件を追加

<form id="form" @submit.prevent="validateBeforeSubmit"> 
 
    <label>Type A</label> 
 
    <input type="radio" v-model="Type" value="TypeA" /> 
 
    <label>Type B</label> 
 
    <input type="radio" v-model="Type" value="TypeB" /> 
 

 
    <table> 
 
     <tr v-show="Type==='TypeA'"> 
 
      <td v-if="Type==='TypeA'"> 
 
       <select v-model="TypeA" v-validate="'required|not_in:Choose'" name="TypeA"> 
 
        <option v-for="option in TypeAOptions" v-bind:value="option.value"> 
 
         {{ option.value }} 
 
        </option> 
 
       </select> 
 
       <span v-if="errors.has('TypeA')"> 
 
        {{ errors.first('TypeA')}} 
 
       </span> 
 
      </td> 
 
     </tr> 
 
     <tr v-show="Type==='TypeB'"> 
 
      <td v-if="Type==='TypeB'"> 
 
       <select v-model="TypeB" v-validate="'required|not_in:Choose'" name="TypeB"> 
 
        <option v-for="option in TypeBOptions" v-bind:value="option.value"> 
 
         {{ option.value }} 
 
        </option> 
 
       </select> 
 
       <span v-if="errors.has('TypeB')"> 
 
        {{ errors.first('TypeB')}} 
 
       </span> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
    <button type="submit">Submit</button> 
 
</form> 
 

 
<script src="https://cdn.jsdelivr.net/npm/vue"></script> 
 
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vee-validate.js"></script> 
 

 
<script> 
 
    Vue.use(VeeValidate); 
 
    var form = new Vue({ 
 
     el: '#form', 
 
     data: { 
 
      Type: 'TypeA', 
 
      TypeA: 'Choose', 
 
      TypeAOptions: [{ 
 
        value: 'Choose' 
 
       }, 
 
       { 
 
        value: 'A', 
 
       }, 
 
       { 
 
        value: 'B' 
 
       }, 
 
       { 
 
        value: 'C' 
 
       }, 
 
       { 
 
        value: 'D' 
 
       } 
 
      ], 
 

 
      TypeB: 'Choose', 
 
      TypeBOptions: [{ 
 
        value: 'Choose' 
 
       }, 
 
       { 
 
        value: '1' 
 
       }, 
 
       { 
 
        value: '2' 
 
       }, 
 
       { 
 
        value: '3' 
 
       }, 
 
       { 
 
        value: '4' 
 
       } 
 
      ], 
 
     }, 
 
     methods: { 
 
      validateBeforeSubmit() { 
 
       this.$validator.validateAll().then((result) => { 
 
        if (result) { 
 
         alert("Submit Success"); 
 
         return; 
 
        } 
 
        alert("Correct them errors!"); 
 
       }); 
 
      } 
 
     } 
 
    }) 
 
</script>

、この問題を解決するための別の方法を見つけました

validateは同じコードを保持できます:

this.$validator.validateAll() 
関連する問題