2

I am using vuelidate to validate input field, the input field is dynamic i.e the value in input field is filled dynamically with jsonData using v-model

What I am trying to do is

On blur I want to show error if there is any, but here when I type anything inside my input field it shows nothing

what I am doing:- my input field

    <div v-for="data in displayProfileData" :key="data.email" >
     <p>{{data}}</p>
   <div class="row">
       <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-3">
                        <label for="phoneNo">Name</label>
              <input v-model="data.businessname"
              @blur="$v.form.name.$touch()"
              type="text" 
                            class="form-control" name="name"
                            id="name">
             <div v-if="$v.form.name.$error" class="form-error">
               <span v-if="!$v.form.name.required" class="text-danger">nameis required</span>
             </div>
                    </div>
          <p>{{$v}}</p>
   </div>
     </div>

I am displaying $v on UI to check but when I type in input field no changes is been detected

My script code :-

    <script>
import { required,minLength } from 'vuelidate/lib/validators'
import axios from '../../services/base-api'
export default {
    data (){
      return{
          form :{
        name:''
          },
           displayProfileData:[]
      }
  },
  validations: {
    form: {
      name:{required}, 
    }
  },
  created(){
        this.userId = localStorage.getItem('user-Id')
        axios().post('/api/v1/Profile/getProfileData',this.userId)
        .then(res=>{
                console.log(res.data)
                this.displayProfileData=res.data

})
.catch(err=>{
    this.$toasted.error(err,{duration:2000})
})

}
}
</script>

My data from server is in format like this { "businessid": "8126815643", "businessname": "manish",}

Issue Initially when page loads in input field it shows manish so when I change it to something else and focus out it shows error that name is required I don't what is going wrong

2:Dynamic Form- Check Here

please check this enter image description here

6
  • You need to check this - sub-collections-validation Commented Nov 6, 2019 at 6:17
  • @sugars that didn't help as my input field values are dynamic,please check my code if you can help accordingly Commented Nov 6, 2019 at 6:29
  • Your v-model seems to be written like this, v-model="$v.form.name.$model" or v-model="data.businessname.$model" Commented Nov 6, 2019 at 6:34
  • @sugars No, if I use v-model="$v.form.name.$model" then my input field is empty which is not the case or if I use v-model="data.businessname.$model" then also input field is empty and on typing throws error Commented Nov 6, 2019 at 6:40
  • All your inputs will have an id “name” in the v-for (I know it’s not what you are after, but I have to mention it) Commented Nov 6, 2019 at 6:49

1 Answer 1

3

According to vuelidate's documentation, you should make the following changes:

<div v-for="data in $v.displayProfileData.$each.$iter" :key="data.email">
  ...
  <input  v-model="data.businessname.$model"
          @blur="data.businessname.$touch()"
          type="text"
          class="form-control"
          name="name"
          id="name"
  >
  <div v-if="data.businessname.$error" class="form-error">
     <span v-if="!data.businessname.required" class="text-danger">name is required</span>
  </div>
...
</div>
 validations: {
    displayProfileData: {
      //required,
      //minLength: minLength(1),
      $each: {
        businessname: { required }
      }
    }
 }

Attach my codesandbox example link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.