EasyUI Forum
April 26, 2024, 09:06:41 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to validate multiple input with same name?  (Read 6387 times)
srw962
Newbie
*
Posts: 5


View Profile Email
« on: December 21, 2018, 12:45:30 AM »

I use method like below. But when all datebox have input, it also have error msgs.
Can validate without a form? Just like jquery Easyui.

Code:
<Form ref="form" :model="user" :rules="rules" @validate="errors=$event">
  <div style="margin-bottom:20px" v-for="(item, index) in user.task">
    <DateBox name="startTime" v-model="item.startTime"></DateBox>
    <span class="error">{{getError('startTime')}}</span>
  </div>
  <div style="margin-bottom:20px">
    <LinkButton :disabled="false" @click="submitForm()">Submit</LinkButton>
  </div>
</Form>


data() {
  return {
    user: {
      task: [{startTime: null},{startTime: null},{startTime: null}]
    },
    rules: {
      startTime: "required"
    },
    errors: {}
  };
},
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: December 22, 2018, 12:41:17 AM »

No, a form field must have a unique name. Here is the solution to solve your issue.
1. Extend from FieldBase and custom a component to display multiple DateBox components.
Code:
<template>
  <span>
    <Form ref="form" :model="model" :rules="rules" @validate="errors=$event">
        <div v-for="field in fields" :key="field" style="margin-bottom:20px">
            <DateBox :name="field" v-model="model[field]" @valueChange="onValueChange($event,field)"></DateBox>
            <div class="error">{{getError(field)}}</div>
        </div>
    </Form>
  </span>
</template>

<script>
import { FieldBase } from "vx-easyui";

export default {
  name: "DateGroup",
  extends: FieldBase,
  props: {
    value: Array
  },
  data() {
    return {
      model: {},
      rules: {},
      errors: {}
    };
  },
  computed:{
      fields(){
          return Object.keys(this.model)
      }
  },
  created() {
    for (let i = 0; i < this.value.length; i++) {
      this.model['field' + i] = this.value[i];
      this.rules['field' + i] = 'required';
    }
    this.$on('validate', (valid) => {
      this.$refs.form.validate()
    })
  },
  methods:{
    onValueChange(event, field){
      this.$emit('input', Object.values(this.model));
    },
    getError(name) {
      return this.errors[name] && this.errors[name].length
        ? this.errors[name][0]
        : null;
    },
    hasError(name) {
      if (name){
        return this.getError(name) != null;
      }
      let err = false;
      this.fields.forEach(f => {
        if (this.hasError(f)){
          err = true;
        }
      })
      return err;
    }
  }
};
</script>

2. Then apply it to the form.
Code:
<DateGroup ref="task" name="task" v-model="user.task"></DateGroup>

3. Define your form data.
Code:
export default {
  components: { DateGroup },
  data() {
    return {
      user: {
        task: [null, new Date()]
      },
      rules: {
       
      },
      errors: {}
    };
  },
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!