P
P
postya2021-03-05 10:52:51
Vue.js
postya, 2021-03-05 10:52:51

How to validate the form of each object from an array in Element UI?

I'm using Element UI to render forms in Vue JS.
There is an array of objects. Through v-for, the name of the object, its form and the button on which validation should take place are displayed.
Validation works fine (if I leave an empty value in the field, for example, and click with the mouse outside the form, then it works)
. That is, the validation button does not work
Here is what I tried to do, but it did not work:

<div class="item" item in items>
<h1>{{item.title}}</h1>
<el-from>
    :ref="item"
    :model="item"
    label-position="top"
     :rules="rules">

 <el-form-item
     label="Название"
     prop="title"
    class="form-item">
         <el-input
          v-model="item.title"
          placeholder="Введите название"/>
 </el-form-item>
</el-form>

 <el-button
        type="primary"
        class="btn-item"
        @click.prevent="validateItem(item)">
       Проверить
</el-button>
</div>

async validateItem(formName) {
       try {
        if (await this.$refs[formName].validate()) {
         console.log('success')
        }
      } catch (e) {}
    },


data() {
    return {
    items: [
   {title: 'Title 1'},
   {title: 'Title 2} 
],
rules: {
title: [
         {
            required: true,
            message: 'Введите название',
            trigger: 'blur',
          },
   [
}
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question