-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicForm.vue
76 lines (69 loc) · 1.8 KB
/
DynamicForm.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<template>
<div v-if="formSubmitted">
<h2>Thank you for reaching out</h2>
</div>
<Form v-else @submit="onSubmit" action="http://localhost:8888">
<div
v-for="{ as, name, label, ...attrs } in schema.fields"
:key="name"
>
<label :for="name">{{ label }}</label>
<Field :as="as" :id="name" :name="name" v-bind="attrs"/>
<ErrorMessage :name="name" />
</div>
<button>Submit</button>
</Form>
</template>
<script>
import { Form, Field, ErrorMessage } from 'vee-validate';
import axios from 'axios';
export default {
name: 'DynamicForm',
components: {
Form,
Field,
ErrorMessage,
},
data: () => {
return {
formSubmitted: false
}
},
props: {
schema: {
type: Object,
required: true,
},
},
methods: {
markFormSubmitted() {
this.$data.formSubmitted = true;
},
onSubmit(values, {evt}) {
let
text_arr = [],
post_data,
cmp = this
;
for (const property in values) {
text_arr.push(`${property}: ${values[property]}`)
}
post_data = {
'from': values.from,
'text': text_arr.join("\n")
}
axios(evt.target.action, {
headers: {
'Access-Control-Allow-Origin': evt.target.action
},
method: 'POST',
data: post_data
}).then(function(response){
cmp.markFormSubmitted();
}).catch(function(error){
console.log(error)
})
}
}
};
</script>