UNPKG

@gavant/ember-validations

Version:

A form validator library using ember-changeset and ember-changeset-validations

132 lines (115 loc) 4.16 kB
import { _ as _initializerDefineProperty } from '../../initializerDefineProperty-b6f88891.js'; import { _ as _applyDecoratedDescriptor } from '../../applyDecoratedDescriptor-e87190e7.js'; import { setComponentTemplate } from '@ember/component'; import { hbs } from 'ember-cli-htmlbars'; import { action } from '@ember/object'; import Component from '@glimmer/component'; import { tracked } from '@glimmer/tracking'; import { reject, resolve, all } from 'rsvp'; var TEMPLATE = hbs("<form ...attributes>\n {{yield\n @changeset\n (hash\n submit=this.submitForm\n input=(component \"input-validator\" parent=this)\n child=(component \"form-validator/child\" parent=this)\n showAllValidationFields=this.showAllValidationFields\n )\n }}\n</form>"); var _class, _descriptor, _descriptor2, _descriptor3; let FormValidator = (_class = class FormValidator extends Component { constructor(...args) { super(...args); _initializerDefineProperty(this, "childValidators", _descriptor, this); _initializerDefineProperty(this, "didInvokeValidate", _descriptor2, this); _initializerDefineProperty(this, "showAllValidationFields", _descriptor3, this); } /** * Register a new child * * @param {FormValidatorChild<T>} child * @memberof FormValidator */ registerChild(child) { this.childValidators.push(child); } /** * Deregister child * * @param {FormValidatorChild<T>} child * @memberof FormValidator */ deregisterChild(child) { this.childValidators = this.childValidators.filter(item => item !== child); } /** * Validate a changeset * * @param {BufferedChangeset} changeset * @return {*} * @memberof FormValidator */ validateChangeset(changeset) { return changeset.validate().then(() => { if (changeset.isInvalid) { return reject(); } else { return resolve(); } }); } /** * Submit the form. Check parent changeset and all child changesets to see if they validate * If they do validate, try to invoke `submit`. Otherwise show all validation field errors * * @param {Event} event * @return {*} * @memberof FormValidator */ async submitForm(event) { event.preventDefault(); const ownChangeset = this.args.changeset; if (ownChangeset) { const validations = [this.validateChangeset(ownChangeset)]; const children = this.childValidators.reduce((prev, child) => { prev.changesets.push(child.args.changeset); prev.validations.push(this.validateChangeset(child.args.changeset)); return prev; }, { changesets: [], validations: [] }); validations.push(...children.validations); this.showAllValidationFields = false; this.childValidators.forEach(item => { item.showAllValidationFields = false; }); try { await all(validations); return this.args?.submit(ownChangeset, children.changesets); } catch (error) { this.showAllValidationFields = true; this.childValidators.forEach(item => { item.showAllValidationFields = true; }); return reject(); } } else { return reject(); } } }, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "childValidators", [tracked], { configurable: true, enumerable: true, writable: true, initializer: function () { return []; } }), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "didInvokeValidate", [tracked], { configurable: true, enumerable: true, writable: true, initializer: function () { return false; } }), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "showAllValidationFields", [tracked], { configurable: true, enumerable: true, writable: true, initializer: function () { return false; } }), _applyDecoratedDescriptor(_class.prototype, "submitForm", [action], Object.getOwnPropertyDescriptor(_class.prototype, "submitForm"), _class.prototype)), _class); setComponentTemplate(TEMPLATE, FormValidator); export { FormValidator as default };