1
0
mirror of synced 2024-11-30 00:56:09 +03:00
vue-formulario/src/FormSubmission.ts

49 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.
*/
hasValidationErrors (): Promise<boolean> {
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.
*/
values (): Promise<Record<string, any>> {
2020-05-22 14:22:56 +03:00
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: Record<string, any>) => Object.assign(values, { [key]: data }))
2020-05-22 14:22:56 +03:00
)
}
}
Promise.all(pending)
.then(() => resolve(values))
.catch(err => reject(err))
})
}
}