1

Does anyone know how can I submit it to JSON after the form validation?

Here is my code:

Contact.vue

<template>
  <v-container grid-list-md fluid id="contact">
    <v-container grid-list-md>
      <v-layout row wrap>
        <v-flex xs12>
          <h2 class="default-custom-title">contact</h2>
          <p class="text-description">
            Drop a message and we will be in contact soon.
          </p>
        </v-flex>
      </v-layout>
      <v-layout row wrap>
        <v-flex xs12>
          <form v-on:submit.prevent="onSubmit">
            <v-layout row wrap>
              <v-flex xs12>
                <v-text-field
                  id="name"
                  v-model="name"
                  label="Name"
                  alpha
                  :error-messages="errors.collect('name')"
                  v-validate="'required|alpha'"
                  data-vv-name="name"
                  required>
                </v-text-field>
              </v-flex>
            </v-layout>
            <v-layout row wrap>
              <v-flex xs12>
                <v-text-field
                  id="phone"
                  v-model="phone"
                  label="Phone"
                  numeric
                  :error-messages="errors.collect('phone')"
                  v-validate="'required|numeric'"
                  data-vv-name="phone"
                  required>
                </v-text-field>
              </v-flex>
            </v-layout>
            <v-layout row wrap>
              <v-flex xs12>
                <v-text-field
                  id="email"
                  v-model="email"
                  label="E-mail"
                  :error-messages="errors.collect('email')"
                  v-validate="'required|email'"
                  data-vv-name="email"
                  required
                ></v-text-field>
              </v-flex>
            </v-layout>
            <v-layout row wrap>
              <v-flex xs12>
                <v-text-field
                  id="message"
                  v-model="message"
                  label="Message"
                  :counter="300"
                  :error-messages="errors.collect('message')"
                  v-validate="'required|max:300'"
                  data-vv-name="message"
                  required>
                </v-text-field>
              </v-flex>
            </v-layout>
            <v-layout row wrap text-xs-right>
              <v-flex xs12>
                <v-btn @click="submit" secondary id="btn-submit">submit</v-btn>
                <v-btn @click="clear" id="btn-clear">clear</v-btn>
              </v-flex>
            </v-layout>
          </form>
        </v-flex>
      </v-layout>
    </v-container>
  </v-container>
</template>


<script>

export default {
  name: 'contact',
  data () {
    return {
      name: '',
      phone: '',
      email: '',
      message: ''
    }
  },
  methods: {
    submit () {
      this.$validator.validateAll()
    },
    clear () {
      this.name = ''
      this.phone = ''
      this.email = ''
      this.message = ''
      this.$validator.reset()
    }
  }
}

</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->

<style>

#btn-submit,
#btn-clear{
    margin: 1em 0 0 1em!important;
}

@media all and (max-width: 960px) { 
  .text-description {
    font-weight: 400;
    font-size: 1.3em;
  }
  .light-text {
      font-weight: 300;
      font-size: 1.2em;
  }
}
@media all and (max-width: 736px) { 
  .text-description {
    font-weight: 400;
    font-size: 1.3em;
  }
  .light-text {
      font-weight: 300;
      font-size: 1.2em;
  }
  .ecn-logo{
    width: 350px;
  }
}
@media all and (max-width: 480px) { 
  .ecn-logo{
    width: 300px;
  }
}

</style>

And here the main.js as well:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuetify from 'vuetify'
import './stylus/main.styl'
import App from './App'
import router from './router'
import VeeValidate from 'vee-validate'

Vue.use(Vuetify)
Vue.use(VeeValidate)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})

I'm currently using the Vuetify form with vee-validate as you can see here: https://vuetifyjs.com/components/forms and I don't want to use JQuery with Vue.

For now, the validation is working but after click on "Submit" button nothing changes.

Thank you!

2
  • What do you mean "nothing changes"? Afaics you don't have any ajax calls. if your submit function is triggered, and you want to ajax call after validation, you can use this.$validator.validateAll().then((isValid) => { if (isValid) { /*ajax if valid*/ } }); Commented Oct 10, 2017 at 20:41
  • Thanks @Traxo. I didn't realize it before. Now works fine. Commented Oct 11, 2017 at 6:20

1 Answer 1

2

Make ajax call after form validation:

this.$validator.validateAll().then(isValid => {
    if (isValid) {
        /* ajax call if valid */
    }
    else{
        /* something else if not valid */
    }
});
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.