Skip to content

Validate > formaction #331

@mjbp

Description

@mjbp

We had a scenario with a form with multiple submit buttons with different formaction attributes. Currently the library does not support that.

Suggest changing post-validation to support multiple submit buttons with name/values, and using event.submitter to determine what the correct action should be:

import { hasNameValue, hasFormactionValue } from './utils';
import {
    createButtonValueNode,
    cleanupButtonValueNode
}  from '../dom';
import { PREHOOK_DELAY } from '../constants';

export const postValidation = (event, resolve, store) => {
    const { settings, form } = store.getState();
    let buttonValueNodes = [];
    let cachedAction = false;
    const submit = () => {
        if (settings.submit) settings.submit();
        else form.submit();

        buttonValueNodes.length && buttonValueNodes.forEach(node => cleanupButtonValueNode(node));
        cachedAction && form.setAttribute('action', cachedAction);
    };

    const formSubmitButtons = Array.from(form.querySelectorAll('[type="submit"]'));
    formSubmitButtons.forEach(formSubmitButton => {
        if (hasNameValue(formSubmitButton)) {
            buttonValueNodes = [...buttonValueNodes,createButtonValueNode(formSubmitButton, form)];
        }
    });
    if (event.submitter && hasFormactionValue(event.submitter)){
        cachedAction = form.getAttribute('action');
        form.setAttribute('action', event.submitter.getAttribute('formaction'));
    }
    if (event && event.target) {
        if (settings.preSubmitHook) {
            settings.preSubmitHook();
            window.setTimeout(submit, PREHOOK_DELAY);
        } else submit();
    }
    
    return resolve(true);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions