Skip to content

Instantly share code, notes, and snippets.

@mattquest
Created November 29, 2019 03:47
Show Gist options
  • Save mattquest/2b8ad601f8f42b643302c765d4eac678 to your computer and use it in GitHub Desktop.
Save mattquest/2b8ad601f8f42b643302c765d4eac678 to your computer and use it in GitHub Desktop.
Javascript form class for use on Laravel frontend.
class Form {
constructor(url, fields) {
this.url = url
this.originalFields = { ...fields }
this.fields = fields
this.errors = {}
}
async post() {
let result = null
result = axios.post(this.url, this.fields)
result.catch(e => {
this.errors = { ...e.response.data.errors }
})
return result
}
resetStatus() {
this.errors = {}
}
reset() {
this.resetStatus()
this.fields = { ...this.originalFields }
}
}
export default Form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment