【发布时间】:2018-10-01 16:10:45
【问题描述】:
这是一个 vueJS 表单。我有一个名为“medications”的嵌套值,我正在尝试提交表单……我的模板和数据区域中有与药物相关的代码。在我从选择框中选择药物并输入其余字段并提交后,我收到一条错误消息,告诉我我没有提交所有值...这是我代码中的片段...
注意:我没有显示整个表格...仅显示与药物表格字段相关的部分。
<template>
...
<div class="col-sm-2">
<b-form-select v-model="medication">
<option selected :value="null">Medication</option>
<option value="name" v-for="catMed in catMedications">{{catMed.medication.name}}</option>
</b-form-select>
</div>
...
</template>
data(){
...
duration: '',
frequency: '',
name: '',
medication: {name: '', duration: '', frequency: '', dosage: '', notes: ''},
...
(also, here is my POST function..if it helps)
postFeedings(catID, catName) {
const vm = this;
axios.post(`/api/v1/carelogs/`,{
cat: {id: catID, name: catName},
weight_unit_measure: 'G',
weight_before_food: this.weight_before_food,
food_unit_measure: 'G',
amount_of_food_taken: this.amount_of_food_taken,
food_type: this.food_type,
weight_after_food: this.weight_after_food,
stimulated: this.stimulated,
stimulation_type: this.stimulation_type,
medication: {name: vm.name, duration: vm.duration, frequency: vm.frequency, dosage: vm.dosage, notes: vm.notes},
medication_dosage_unit: 'ML',
medication_dosage_given: this.medication_dosage_given,
notes: this.notes
})
.then(response => {
console.log(response);
response.status === 201 ? this.showSwal('success-message','Carelog added') : null;
this.getFeedings(catName);
})
.catch(error => {
console.log(catID, catName);
console.log(error);
this.showSwal('auto-close', error);
})
}
错误:这是我返回的错误......
{"medication":{"frequency":["This field may not be blank."],"name":["This field may not be blank."]}}
所有其他参数都已发送...但药物参数未发送...
我做错了什么?
编辑:按照 Husam Ibrahim 的建议更新了 axios 帖子
【问题讨论】:
-
只有
frequency和name是空白的字段吗?如果是这样,this.frequency和this.name是如何填充的? -
是的 @tony19 频率和名称是唯一空白的字段。这就是我的问题,我不知道如何填充它们。
-
您在模板或脚本的哪个位置设置
this.frequency和this.name? -
@tony19 这是关键......你的线索让我专注于我的模板部分以及我如何设置“名称”的值