1
0
mirror of synced 2024-11-26 07:16:02 +03:00
vue-formulario/src/FormSubmission.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

import { cloneDeep } from './libs/utils'
import FileUpload from './FileUpload'
import FormularioForm from '@/FormularioForm.vue'
export default class FormSubmission {
public form: FormularioForm
2020-05-22 14:22:56 +03:00
/**
* Initialize a formulario form.
* @param {vm} form an instance of FormularioForm
*/
constructor (form: FormularioForm) {
2020-05-22 14:22:56 +03:00
this.form = form
}
2020-05-22 14:22:56 +03:00
/**
* Determine if the form has any validation errors.
*
* @return {Promise} resolves a boolean
*/
hasValidationErrors () {
return (this.form as any).hasValidationErrors()
2020-05-22 14:22:56 +03:00
}
2020-05-22 14:22:56 +03:00
/**
* Asynchronously generate the values payload of this form.
* @return {Promise} resolves to json
*/
values () {
return new Promise((resolve, reject) => {
const form = this.form as any
2020-05-22 14:22:56 +03:00
const pending = []
const values = cloneDeep(form.proxy)
2020-05-22 14:22:56 +03:00
for (const key in values) {
if (
Object.prototype.hasOwnProperty.call(values, key) &&
typeof form.proxy[key] === 'object' &&
form.proxy[key] instanceof FileUpload) {
2020-05-22 14:22:56 +03:00
pending.push(
form.proxy[key].upload().then((data: Object) => Object.assign(values, { [key]: data }))
2020-05-22 14:22:56 +03:00
)
}
}
Promise.all(pending)
.then(() => resolve(values))
.catch(err => reject(err))
})
}
}