我有一个表格,用户可以选择输入生日。我正在使用 vue.js 并使用 new FormData() 发送请求。如果我不填写生日字段[HTTP/1.0 400 BAD REQUEST 272ms] *errors Object {birthday: […] } * birthday [“Not a valid date.”, 它会给我错误。] 0“不是有效日期。” 如果我填写该字段,它就可以工作,但我希望它是可选的。谢谢。超文本标记语言<input id="birthday" v-model="newUser.birthday" class="form-control" placeholder="birthday (Opt)" type="date" /> 视图.js new Vue({ el: "#app", delimiters: ["[[", "]]"], data() { return { newUser: { name: "", surname: "", email: "", title: "", birthday: "", employee_number: "", monthly_meal_limit: "", max_meal_amount: "" }, }; },发送请求的函数 sendInvite(newUser) { let inputCheck = this.validateBeforeSubmit(newUser); if(inputCheck) { let fd = new FormData(); fd.append("name", newUser.name); fd.append("surname", newUser.surname); fd.append("email", newUser.email); fd.append("title", newUser.title) fd.append("employee_number", newUser.employee_number) fd.append("birthday", newUser.birthday) let token = this.getCsrfToken() const headers = new Headers({ "X-CSRFToken": token }) //send the request with the formdata let req = new Request(apiPath, { body: fd, headers, method: "POST" }); fetch( req) .then((response) => response.json()) .then((data) => { this.addNewForm = false, this.newUser = { name: "", surname: "", email: "", title: "", employee_number: 0, birthday: "" }, this.getInvites() }) .catch((error) => console.log("error", error)); } }
1 回答
白衣染霜花
TA贡献1796条经验 获得超10个赞
如果后台可以的话也许你可以这样做。并且不要将生日字段添加到表单中
let inputCheck = this.validateBeforeSubmit(newUser);
if(inputCheck) {
let fd = new FormData();
fd.append("name", newUser.name);
fd.append("surname", newUser.surname);
fd.append("email", newUser.email);
fd.append("title", newUser.title)
fd.append("employee_number", newUser.employee_number)
if(newUser.birthday && newUser.birthday.length > 0){
fd.append("birthday", newUser.birthday)
}
......
添加回答
举报
0/150
提交
取消