Sha256: a4736f2efa47f51a7e61d82cdfc30e7327778c013f4904012bf350b9f23b7210

Contents?: true

Size: 1.67 KB

Versions: 5

Compression:

Stored size: 1.67 KB

Contents

export function initForms() {
    console.log('\tForms');

    var components = document.querySelectorAll('.v-form');
    for (var i = 0; i < components.length; i++) {
        var component = components[i];
        if (!component.vComponent) {
            component.vComponent = new VForm(component);
        }
    }
}

export class VForm {
    constructor(element) {
        this.element = element;
    }

    // Called whenever a form is about to be submitted.
    // returns true on success
    // returns on failure return an error object that can be processed by VErrors:
    //    { email: ["email must be filled", "email must be from your domain"] }
    //    { :page: ["must be filled"] }
    validate(form, params) {
        console.log("Form validate", form, params);
        var errors = [];
        for (let input of this.inputs()) {
            if (input.vComponent) {
                var result = input.vComponent.validate(form, params);
                if (result !== true) {
                    errors.push(result);
                }
            }
        }
        return errors;
    }

    inputs() {
        return this.element.querySelectorAll('input');
    }

    // Called to collect data for submission
    prepareSubmit(form, params) {
        for (let input of this.inputs()) {
            if (input.vComponent) {
                input.vComponent.prepareSubmit(form, params);
            }
        }
    }

    initEventListener(eventName, eventHandler) {
        for (let input of this.inputs()) {
            if (input.vComponent && input.vComponent.initEventListener) {
                input.vComponent.initEventListener(eventName, eventHandler);
            }
        }
    }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
voom-presenters-0.1.4 views/mdc/assets/js/components/forms.js
voom-presenters-0.1.3 views/mdc/assets/js/components/forms.js
voom-presenters-0.1.2 views/mdc/assets/js/components/forms.js
voom-presenters-0.1.1 views/mdc/assets/js/components/forms.js
voom-presenters-0.1.0 views/mdc/assets/js/components/forms.js