gpt4 book ai didi

表单提交后验证重置

转载 作者:行者123 更新时间:2023-12-03 06:48:26 25 4
gpt4 key购买 nike

我有一个带有复选框的表单,我希望用户至少选择其中一个。一切正常,但在重置表单后我无法隐藏验证消息。docs 中描述了这种情况。 , 但提供的解决方案似乎无效,因为在提交表单验证错误后出现。

    <template>
<v-app>
<v-content>
<playground></playground>
<v-card class="mx-auto" outlined>
<ValidationObserver ref="obs" v-slot="{ invalid, valid, validated, passes, reset }">
<v-card-title class="pb-0">Meal types</v-card-title>
<v-row justify="center">
<v-col cols="11">
<v-form ref="form">
<ValidationProvider rules="required" v-slot="{ errors, failedRules }">
<v-container row pa-0>
<v-row justify="space-around">
<v-checkbox
v-model="mealType"
value="BREAKFAST"
label="Breakfast"
hide-details
></v-checkbox>
<v-checkbox v-model="mealType" value="DINNER" label="Dinner" hide-details></v-checkbox>
<v-checkbox v-model="mealType" value="SUPPER" label="Supper" hide-details></v-checkbox>
<v-checkbox v-model="mealType" value="SNACK" label="Snack" hide-details></v-checkbox>
</v-row>
</v-container>
<v-row justify="center">
<v-alert
v-if="failedRules.required"
type="error"
dense
outlined
class="mt-4 mb-0"
>Select at least one meal type</v-alert>
</v-row>
</ValidationProvider>
</v-form>
</v-col>
</v-row>
<v-card-actions>
<v-row justify="center">
<v-btn text color="deep-purple accent-4" @click="passes(addRecipe)">Save</v-btn>
<v-btn @click="reset">Reset</v-btn>
</v-row>
</v-card-actions>
</ValidationObserver>
</v-card>
</v-content>
</v-app>
</template>

<script>
import Playground from "./components/Playground";

export default {
name: "App",
components: {
Playground
},
data() {
return {
recipeName: "",
mealType: []
};
},
methods: {
addRecipe() {
console.log("add recipe");
// after save or reset alerts should disappear..
this.$refs.form.reset();
requestAnimationFrame(() => {
this.$refs.obs.reset();
});
}
}
};
</script>

带有重现用例的代码沙箱:https://codesandbox.io/s/vee-validate-3-reset-checkbox-validation-qr8uw请选择一些餐食类型并点击保存。单击保存按钮后,表单将重置并显示验证消息,但事实并非如此。如何解决?

最佳答案

找到了一个解决方法,这将帮助您解决问题(原始的 this.$refs.form.reset()),必须是一个错误,应该报告给 VeeValidate解决。

我发现使方法async,并手动重置变量使一切顺利。

methods: {
async addRecipe() {
console.log("add recipe");
console.log(this.mealType);

this.mealType = [];
this.$refs.obs.reset();
}
}

关于表单提交后验证重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59380609/

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