-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.html
105 lines (103 loc) · 4.47 KB
/
schema.html
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>VueFormulate PoC</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es2015"></script>
<!-- development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@braid/[email protected]/dist/formulate.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@braid/[email protected]/dist/snow.min.css">
</head>
<body>
<div id="app">
<h1>{{ message }}</h1>
<formulate-form
v-model="formValues"
:schema="schema"
@submit="formSubmit"
> <!-- v-model is not always required. You will receive the form data in the submit event -->
</formulate-form>
</div>
<script>
Vue.use(VueFormulate);
new Vue({
el: "#app",
data: {
message: "Hello world!",
formValues: {}, // v-model is not always required. You will receive the form data in the submit event
schema: [ // Schema for the form
{
type: 'text',
name: 'name',
label: 'What is your name?',
validation: 'required'
},
{
type: 'text',
name: 'age',
label: 'How old are you?',
validation: 'required|number|max:120',
},
{
label: 'Pets',
type: 'group',
repeatable: true,
name: 'pets',
addLabel: 'add pet',
children: [
{
name: 'name',
label: 'Name',
validation: 'required'
},
{
component: 'div',
class: 'double-row',
children: [
{
name: 'kind',
type: 'select',
label: 'Kind',
options: {
dog: 'Dog',
cat: 'Cat',
bird: 'Bird',
other: 'Other'
},
}
]
}
]
},
{
type: 'submit',
label: 'Send request'
}
],
},
// define methods under the `methods` object
methods: {
formSubmit(e) {
/* e is a plain object with the form data while this.formValues (mapped v-model) is a managed object that will detect model updates */
console.log("formSubmit", e, this.formValues);
console.log("Let's try async model update!");
setTimeout(() => { // setTimeout is not required, it's just an example
console.log("Async model update now!");
this.formValues.age++; // Increase age
this.message = `Hello ${this.formValues.name}!! It seems it took me a whole year to process your request!!!` ;
console.log("Pets", [...this.formValues.pets].map(p=>({...p})));
// If pets then let one reproduce
if (this.formValues.pets.length > 0) {
let parent = this.formValues.pets[Math.floor(Math.random()*this.formValues.pets.length)]; // Random parent
this.formValues.pets.push({name:parent.name + '.jr', kind:parent.kind});
}
}, 2000);
}
}
});
</script>
</body>
</html>