无法使用vue-validator添加动态验证

时间:2016-04-27 16:59:40

标签: javascript validation vue.js

我想知道你是否可以帮助我,我正在尝试验证一个在另一个输入中由值生成的动态输入的表单。

这是我的表格:

<validator name="validationQ">
   <form class="form-horizontal">
      <h3>Crear pregunta</h3>
      <div class="form-group">
         <label class="col-lg-2 control-label">Pregunta</label>
         <div class="col-lg-10">
            <input type="text" v-model="questionFormData.name" v-validate:name="{ required: true}" 
               placeholder="Nombre" class="form-control" id="name"> 
            <span v-if="$validationQ.name.required" class="help-block m-b-none">
            <span class="label label-danger">Pregunta obligatoria</span>
            </span>
         </div>
      </div>
      <div class="form-group">
         <label class="col-lg-2 control-label">Tipo de pregunta</label>
         <div class="col-lg-10">
            <select class="form-control" 
               v-model="questionFormData.questionType_id"
               id="questiontype" v-validate:questiontype="{ required: true}">
               <option selected value>-Selecciona el tipo de respuesta-</option>
               <option v-for="question_type in question_types" 
                  value="{{question_type.id}}">{{question_type.name}}</option>
            </select>
            <span v-if="$validationQ.questiontype.required" class="help-block m-b-none">
            <span class="label label-danger">Tipo de pregunta obligario</span>
            </span>
         </div>
      </div>
      <div class="form-group" v-if="questionFormData.questionType_id == 4">
         <!--<validator name="range">-->
         <label class="col-lg-2 control-label">Valor mínimo</label> 
         <div class="col-lg-4">
            <input type="number" class="form-control" id="minvalue"
               placeholder="Valor mínimo" min="0"
               v-model="questionFormData.min_value"
               v-validate:minvalue="{ required: true }">       
            <span v-if="$validationQ.minvalue.required"
               class="help-block m-b-none">
            <span class="label label-danger">Campo oblicagorio</span>    
            </span>
         </div>
         <label class="col-lg-2 control-label">Valor máximo</label>
         <div class="col-lg-4">
            <input type="number" class="form-control" id="maxvalue"
               placeholder="Valor máximo" min="5"
               v-model="questionFormData.max_value"
               v-validate:maxvalue="{ required:true }">
            <span v-if="$validationQ.maxvalue.required" class="help-block m-b-none">
            <span class="label label-danger">Campo oblicagorio</span>    
            </span>     
         </div>
         <!--</validator>-->
      </div>
      <div class="form-group" v-if="questionFormData.questionType_id == 1" >
         <label class="col-lg-2 control-label">Tipo de respuesta</label>
         <div class="col-lg-10">
            <select class="form-control" v-model="questionFormData.open_type"
               id="opentype" v-validate:opentype="{ required: true}">
               <option selected value>-Selecciona el tipo de respuesta-</option>
               <option value="text">Texto</option>
               <option value="email">Email</option>
               <option value="numeric">Numérico</option>
            </select>
            <span v-if="$validationQ.opentype.required" class="help-block m-b-none">
            <span class="label label-danger">Tipo de respuesta obligario</span>
            </span>
         </div>
      </div>
      <div class="form-group" 
         v-if="questionFormData.questionType_id == 5 || questionFormData.questionType_id == 6" >
         <label class="col-lg-2 control-label">Tipo de respuesta</label>
         <div class="col-lg-10">
            <select class="form-control" disabled v-model="questionFormData.open_type"
               id="opentype" v-validate:opentype="{ required: true}">
               <option  value>-Selecciona el tipo de respuesta-</option>
               <option selected value="text">Texto</option>
               <option value="numeric">Numérico</option>
            </select>
            <span v-if="$validationQ.opentype.required" class="help-block m-b-none">
            <span class="label label-danger">Tipo de respuesta obligario</span>
            </span>
         </div>
      </div>
      <div class="form-group">
         <div class="col-lg-offset-2 col-lg-10">
            <div class="i-checks">
               <label> <input v-model="questionFormData.is_required" 
                  type="checkbox" class="checkbox-primary"> Pregunta obligatoria </label>
            </div>
         </div>
      </div>
      <div class="form-group">
         <div class="col-lg-offset-2 col-lg-10">
            <button class="btn btn-sm btn-success btn-block" v-on:click="addQuestion(current_section_id)" 
               data-dismiss="modal" type="button" 
               v-if="$validationQ.valid ">Agregar pregunta</button>
         </div>
      </div>
      <div class="form-group">
         <div class="col-lg-offset-2 col-lg-10">
            <button class="btn btn-sm btn-white btn-block" data-dismiss="modal"> Cancelar </button>
         </div>
      </div>
   </form>
</validator>

这是验证器对象:

{
  "valid": false,
  "invalid": true,
  "touched": false,
  "untouched": true,
  "modified": false,
  "dirty": false,
  "pristine": true,
  "questiontype": {
    "required": true,
    "modified": false,
    "pristine": true,
    "dirty": false,
    "untouched": true,
    "touched": false,
    "invalid": true,
    "valid": false
  },
  "name": {
    "required": true,
    "modified": false,
    "pristine": true,
    "dirty": false,
    "untouched": true,
    "touched": false,
    "invalid": true,
    "valid": false
  }
}

从输出中,我了解动态生成的输入不会动态添加到验证对象。所以我想知道是否有人能给我提示解决我的问题。

vue和vue-validator的当前版本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-validator/2.0.2/vue-validator.min.js"></script>

1 个答案:

答案 0 :(得分:0)

您可以使用v-if而不是v-show。这仍然会隐藏不必要的问题,但会确保它们被vue-validator选中。

此外,您需要使用vue > 1.0.19来使用vue-validator >= 2.0.01.0.19发生了变化,为新vue-validator铺平了道路。