vue-dynamic-forms
v1.1.0
Published
a dynamic form component for Vue.js
Downloads
58
Readme
Dynamic forms
Table of Contents
create dynamic forms with vue based on a json configuration
Documetation
Form
Parameters
inputsArray A list of all the required form fields with initial values form more info see the params sectionrequestObject A object containing an optional the configuration for the requests (optional)request.headersHeaders A Javascript Headers object containing custom headers (optional)request.credentialsString set credentials option in the request (optional) default value='omit'request.urlString The url which the form will be send to (optional) default value = ''(self)request.methodString The method of the request (optional) default value = 'post'
configObject the configuration of the whole form
data
groups all form values in a formData object
Returns FormData data - a FormData object containing all form values
reset
Resets all values in the form + clears the errors
submit
Submit the form with the current values
Returns Promise promise based on the succes or failure of a request done with the fetch API
onFail
Sets the Errors of the form
Parameters
errorsArray An array containing error objects
Input
an input object in the inputs array
Parameters
inputObject an input configurationinput.typeString (Required) - The type of input supported input types: text,email,password,hidden,tel,date,number,select,textareainput.nameString (Required) - The name of the input fieldinput.value(Optional) - The value of the input fieldinput.labelString (Optional) - The label of the input fieldinput.classString (Optional) - The Css class(es) of the input field
Input specific parameters
Errors
has
Check if an error exists
Parameters
fieldString the name of the field you are looking for
any
Check if there are any Errors
Returns Boolean boolean
get
get an Error for a particular field
Parameters
fieldString the name of the field you are looking for
Returns any if exists returns the error else returns false
set
Sets the error Object
Parameters
Errorserrorserr
clear
Clear all the erros
Example
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
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:""
},
]
}
}