[Vuejs]-How can I submit only form elements in Vue[2].js?

0👍

Instead of posting the entire ‘this.myForm’, just specify the fields you want to post in a new object.

submitForm() {
  if (!this.$route.params.id) {
    this.$store.dispatch('user/create', this.myForm)
  } else {
    this.$store.dispatch('user/update/', {
      name: this.myForm.name,
      email: this.myForm.email,
      password: this.myForm.password
    })
  }
}

UPDATE: Following comments from OP, if you’re expecting more that ‘name’, ’email’, and ‘password’, you can just delete the properties you don’t want to send.

 submitForm() {
  if (!this.$route.params.id) {
    this.$store.dispatch('user/create', this.myForm)
  } else {
    delete this.myForm.createdAt,
    delete this.myForm.updatedAt,
    delete this.myForm._id
    this.$store.dispatch('user/update/', this.myForm)
  }
}

Leave a comment