Element 动态表单嵌套踩坑日记 please transfer a valid prop path to form item

相信很多人使用动态表单,没错就在昨天我也使用了动态表单这不是第一次,在上一篇文章中我还专门出了一篇关于动态表单的文章只不过没有使用表单嵌套,一切都很顺利,但是这次使用动态表单并嵌套之后出现了我意想不到的事情,如果你还没有使用过动态表单,请点击去了解动态表单

都知道动态表单的验证是比较特殊的,都是写在el-form-item上面的如下。

  <el-form-item
          label="作业标题"
          :prop="'domains.' + index + '.jobtitle'"
          :rules="[
            {
              required: true,
              message: '标题不能为空',
              trigger: 'blur',
            },
            { min: 5, max: 200, message: '标题最少 5 个字符,最多 200 个字符', trigger: 'blur' },
          ]">
          <el-input :rows="5" v-model.trim="item.jobtitle" maxlength="200" show-word-limit placeholder="字数限制5-200,标题不可重复" class="long1" type="textarea"></el-input>
        </el-form-item>

在嵌套之还却报了一个错误 please transfer a valid prop path to form item! 这个错误是什么意思呢。译:(请传输一个有效的道具路径来形成物品!)意思是说你的 prop 的路径不对 ,表单嵌套代码如下

<template>
  <div class="box">
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <div v-for="(item, index) in dynamicValidateForm.domains" :key="item.key" class="ietm_content">
        <el-form-item
          label="测试"
          :prop="'domains.' + index + '.jobtitle'"
          :rules="[
            {
              required: true,
              message: '标题不能为空',
              trigger: 'blur',
            },
            { min: 5, max: 200, message: '标题最少 5 个字符,最多 200 个字符', trigger: 'blur' },
          ]">
          <el-input :rows="5" v-model.trim="item.jobtitle" maxlength="200" show-word-limit placeholder="字数限制5-200,标题不可重复" class="long1" type="textarea"></el-input>
        </el-form-item>
        <!-- 正确选项 -->
        <el-form-item label="正确选项">
          <el-button style="margin-left: 10px" @click="addcorrect(item)" type="primary">添加选项</el-button>
          <span style="color: #606266; margin-left: 10px">最多共可设置 5 个</span>
        </el-form-item>
        <div v-for="(itemr, indexr) in item.correct" :key="indexr.key">
          <el-form-item
            :label="`选项${indexr + 1}`"
            :prop="itemr.+ indexr + '.options'"
            :rules="[
              {
                required: true,
                message: '选项不能为空',
                trigger: 'blur',
              }]">
            <el-input v-model.trim="itemr.options" class="long1" placeholder="请输入"clearable> 
           </el-input>
         
          </el-form-item>
        </div>
      </div>
    </el-form>
  
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      dynamicValidateForm: {
        domains: [
          {
            jobtitle: '',
            correct: [
              {
                options: ''
              }
            ]
            ]
          }
        ]
      }
    }
  },
  computed: {},
  mounted () {},
  methods: {
    
  }
}
</script>
<style lang="scss" scoped>

</style>

正常嵌套在里面的验证应该都会跟上面的代码这样写都是这样会报错的

正确代码如下:

<template>
  <div class="box">
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <div v-for="(item, index) in dynamicValidateForm.domains" :key="item.key" class="ietm_content">
        <el-form-item
          label="测试"
          :prop="'domains.' + index + '.jobtitle'"
          :rules="[
            {
              required: true,
              message: '标题不能为空',
              trigger: 'blur',
            },
            { min: 5, max: 200, message: '标题最少 5 个字符,最多 200 个字符', trigger: 'blur' },
          ]">
          <el-input :rows="5" v-model.trim="item.jobtitle" maxlength="200" show-word-limit placeholder="字数限制5-200,标题不可重复" class="long1" type="textarea"></el-input>
        </el-form-item>
        <!-- 正确选项 -->
        <el-form-item label="正确选项">
          <el-button style="margin-left: 10px" @click="addcorrect(item)" type="primary">添加选项</el-button>
          <span style="color: #606266; margin-left: 10px">最多共可设置 5 个</span>
        </el-form-item>
        <div v-for="(itemr, indexr) in item.correct" :key="indexr.key">
          <el-form-item
            :label="`选项${indexr + 1}`"
            :prop="'domains.' + index + '.correct.' + indexr + '.options'"
            :rules="[
              {
                required: true,
                message: '选项不能为空',
                trigger: 'blur',
              }]">
            <el-input v-model.trim="itemr.options" class="long1" placeholder="请输入"clearable> 
           </el-input>
         
          </el-form-item>
        </div>
      </div>
    </el-form>
  
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      dynamicValidateForm: {
        domains: [
          {
            jobtitle: '',
            correct: [
              {
                options: ''
              }
            ]
            ]
          }
        ]
      }
    }
  },
  computed: {},
  mounted () {},
  methods: {
    
  }
}
</script>
<style lang="scss" scoped>

</style>

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Certainly! To transfer a valid prop path to a form item, you need to follow these steps: 1. Identify the form item you want to transfer the prop path to. It can be an input field, a select dropdown, or any other form element. 2. Determine the prop path that you want to transfer. A prop path is typically a string that specifies the location of the data property within the form item. 3. Ensure that the prop path is valid and matches the data structure of the form item. For example, if you have a nested object for the form item data, the prop path should include the nested property names separated by dots (e.g., 'user.address.city'). 4. Pass the prop path as a prop to the form item component. This can be done by adding a prop attribute to the form item component and assigning the prop path value to it. Here's an example of how you can transfer a prop path to a form item using Vue.js: ```vue <template> <div> <label for="name">Name:</label> <input id="name" v-model="formData.name" :prop-path="'name'"/> <label for="email">Email:</label> <input id="email" v-model="formData.email" :prop-path="'email'"/> </div> </template> <script> export default { data() { return { formData: { name: '', email: '' } }; } }; </script> ``` In this example, we have two input fields for name and email. The prop path is transferred using the `:prop-path` binding, and it corresponds to the respective properties in the `formData` object. Remember to adapt this example to your specific framework or library if you're not using Vue.js.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值