create dynamic forms with vue based on a json configuration
Parameters
inputs
Array A list of all the required form fields with initial values form more info see the params sectionrequest
Object A object containing an optional the configuration for the requests (optional)request.headers
Headers A Javascript Headers object containing custom headers (optional)request.credentials
String set credentials option in the request (optional) default value='omit'request.url
String The url which the form will be send to (optional) default value = ''(self)request.method
String The method of the request (optional) default value = 'post'
config
Object the configuration of the whole form
groups all form values in a formData object
Returns FormData data - a FormData object containing all form values
Resets all values in the form + clears the errors
Submit the form with the current values
Returns Promise promise based on the succes or failure of a request done with the fetch API
Sets the Errors of the form
Parameters
errors
Array An array containing error objects
an input object in the inputs array
Parameters
-
input
Object an input configurationinput.type
String (Required) - The type of input supported input types: text,email,password,hidden,tel,date,number,select,textareainput.name
String (Required) - The name of the input fieldinput.value
(Optional) - The value of the input fieldinput.label
String (Optional) - The label of the input fieldinput.class
String (Optional) - The Css class(es) of the input field
Input specific parameters
Check if an error exists
Parameters
field
String the name of the field you are looking for
Check if there are any Errors
Returns Boolean boolean
get an Error for a particular field
Parameters
field
String the name of the field you are looking for
Returns any if exists returns the error else returns false
Sets the error Object
Parameters
Errors
errorserr
Clear all the erros
add the component to the components
section of your Vue component
demo.vue
<template>
<div>
<!--
custom html element
custom atribute formdata is required
-->
<dynamic-form :formdata="formdata"></dynamic-form>
</div>
</template>
<script>
//import the library
import dynamicForm from 'vue-dynamic-forms'
import { createDemo } from './config'
export default {
name: 'demo',
components:{
//add the dynamicForm to the components section
dynamicForm
}
data () {
return {
//get the configuration and initialize it
formdata:createDemo()
};
}
};
</script>
<style lang="css" scoped>
</style>
configuration in the config.js
file
export fuction createDemo(){
return {
//request options
request:{
//post url
url:'/register',
//method
method:'post',
//adding the CSRF Token for laravel applications
headers:new Headers({
'X-CSRF-TOKEN':window.Laraval.csrfToken
}),
//include credentials
credentials:'include'
},
//title
title:'Demo Form'
//text of the submit button
submitText:'Register',
//inputs
inputs:[
{
type:'text',
label:'Firtsname',
name:'firstname',
class:'form-control',
value:''
},
{
type:'text',
label:'Lastname',
name:'lastname',
class:'form-control',
value:''
},
{
type:'email',
label:'Email',
name:'email',
class:'form-control',
value:''
},
{
type:'password',
label:'Password',
name:'password',
class:'form-control',
value:''
},
{
type:'password',
label:'Confirm password',
name:'password_confirmation',
class:'form-control',
value:''
},
{
type:'date',
label:'Date of birth',
name:'dateofbirth',
class:'form-control',
value:''
},
{
type:'tel',
label:'Telephone',
name:'phone',
class:'form-control',
value:''
},
{
type:'select',
label:'Gender',
name:'gender',
class:'form-control',
options:['male','female'],
value:""
},
]
}
}