Sprintify is a lightweight Vue plugin that makes reactive form creation a cinch.
npm install sprintify-formly
import Vue from 'vue'
import Form from 'sprintify-formly'
Vue.use(Form)
You can create a simple form this way:
<form @submit.prevent="send()">
<v-input name="firstname"></v-input>
<v-input name="lastname"></v-input>
<v-input name="age"></v-input>
</form>
export default {
data () {
return {
form: this.$form.create({
schema: {
firstname: null,
lastname: null,
age: null
}
})
}
},
methods: {
send () {
this.form.post('/admin/user')
.then(response => {
alert('User successfully created!')
})
.catch(error => {
alert('Whoops!')
})
}
}
}
To generate dynamic lists, you proceed like this:
<form @submit.prevent="send()">
<button @click="form.tags.add()">Add a new tag</button>
<div v-for="(tag, i) in form.tags.list">
<v-input :name="'tags.' + i + '.name'"></v-input>
<v-input :name="'tags.' + i + '.category'"></v-input>
<button @click="form.tags.remove(i)">Remove this tag</button>
</div>
</form>
export default {
data () {
return {
form: this.$form.create({
schema: {
tags: [
{
name: null,
category: null
}
]
}
})
}
}
}
To add an element to a list: form.tags.add()
.
To remove an element from a list: form.tags.remove(i)
.
Vue.use(Form, {
locales: {"en": "English", "fr": "Français"}, // All app's locales
locale: "en", // Default locale
onFormSuccess: onSuccess, // callback when a form makes a successful request
onFormFail: onFail // callback when a form makes a failed request
})
You can set custom data for each field this way:
export default {
data () {
return {
form: this.$form.create({
schema: {
firstname: null,
tags: [
{
name: null,
category: null
}
]
},
data: {
firstname: 'John',
tags: [
{name: "red", category: "color"},
{name: "blue", category: "color"}
]
}
})
}
}
}
Code released under MIT license.
Copyright (c) 2018, François Lévesque.