ZBLOG

在 Vue 3 的 <script setup> 中处理表单验证和必填项时,可以使用 v-model 来双向绑定数据,并确保在用户手动输入后进行校验。根据你提供的代码,若要解决“手动输入之后必填校验仍然展示”的问题,可以考虑以下几个方面:

1. 确保 v-model 的正确使用

确保你的 v-model 是针对每个输入框的数据模型(例如 item.productNameitem.loanInfoCode[index])。如果有多个产品行,确保它们都能正确更新。

2. 使用 watch 监听数据变化

可以通过使用 Vue 的 watch API 来监听产品名称和借据号的变化,以便及时更新其状态,从而避免必填校验错误。

示例代码修改

以下是调整后的代码示例,包括如何使用 watch 来监听字段的变化并更新验证状态:

  1. <template>
  2. <div
  3. class="loanInfo"
  4. :style="isCustomerNotFound && productRows.length > 0 ? { height: '100px' } : {}"
  5. v-if="!isEditing"
  6. >
  7. <a-row v-if="isCustomerNotFound" v-for="(item, index) in productRows" :key="index">
  8. <a-col :span="12">
  9. <a-form-item
  10. label="产品名称"
  11. name={`productName-${index}`}
  12. :rules="[{
  13. required: true,
  14. message: '请手动输入产品名称!',
  15. validator: (rule, value) => {
  16. return !!value || Promise.reject(new Error('请手动输入产品名称!'));
  17. }
  18. }]"
  19. style="width: 101%"
  20. >
  21. <a-input v-model:value="item.productName" />
  22. </a-form-item>
  23. </a-col>
  24. <a-col :span="12">
  25. <a-form-item
  26. label="借据号"
  27. name={`loanInfoCode-${index}`}
  28. :rules="[{
  29. required: true,
  30. message: '请手动输入借据号!',
  31. validator: (rule, value) => {
  32. return !!value || Promise.reject(new Error('请手动输入借据号!'));
  33. }
  34. }]"
  35. style="width: 102%; margin-left: -5px"
  36. >
  37. <a-input v-model:value="item.loanInfoCode" />
  38. </a-form-item>
  39. </a-col>
  40. </a-row>
  41. <span
  42. v-if="productRows.length > 0 && isCustomerNotFound"
  43. class="jiahao"
  44. @click="addProductRow"
  45. >
  46. + 添加产品
  47. </span>
  48. </div>
  49. </template>
  50. <script setup>
  51. import { ref, watch } from 'vue';
  52. const isCustomerNotFound = ref(true);
  53. const productRows = ref([{ productName: '', loanInfoCode: '' }]);
  54. const formModal = ref({});
  55. const modalType = ref('');
  56. const isModalVisible = ref(false);
  57. const handleSubmit = async () => {
  58. try {
  59. await formRef.value.validate();
  60. const values = { ...formModal.value };
  61. if (modalType.value === 'add') {
  62. addCustomer(values);
  63. } else if (modalType.value === 'edit') {
  64. editCustomer(values);
  65. }
  66. setTimeout(() => {
  67. QueryTab();
  68. isModalVisible.value = false;
  69. }, 200);
  70. } catch (error) {
  71. console.error('Validation failed:', error);
  72. }
  73. };
  74. // Watcher to check for manual input and revalidate
  75. watch(productRows, (newValue) => {
  76. // This logic can be customized based on the validation need.
  77. newValue.forEach((item, index) => {
  78. if (!item.productName || !item.loanInfoCode) {
  79. // Trigger any necessary UI updates or state changes here.
  80. }
  81. });
  82. });
  83. // Example method to add a new product row
  84. const addProductRow = () => {
  85. productRows.value.push({ productName: '', loanInfoCode: '' });
  86. };
  87. </script>

核心点总结:

  • 确保每个表单项都有唯一的 name 属性,防止冲突。
  • 在规则中定义自定义验证函数以动态检查值。
  • 使用 watch 函数来监测数据变化并根据需要触发验证或其他逻辑。


    内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=22860

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?