gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:05:34 26 4
gpt4 key购买 nike

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

这是我的表格:

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

最佳答案

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

此外,您需要使用 vue > 1.0.19 才能使用 vue-validator >= 2.0.01.0.19 中进行了一项更改,为新的 vue-validator 铺平了道路。

关于javascript - 无法使用 vue-validator 添加动态验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36896261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com