-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Note on the interaction of conditional-formfields, multipage-form and ajax-form #89
Comments
Wouldn't work with the insert-tag |
I am aware of this - but this is the original template... |
Remember that ajaxform won't be used in Contao 5 anymore since it's part of the Contao Core in 5.1, thus you'd be able to use the template from the contao core 😄. Meaning you wouldn't need to bother about that. |
|
Below are the snippets for Contao 4.13 and the ajaxform. Also, make sure that you have the version 3.1.3 of the conditionalformfields extension. ajaxform.html5 <!-- indexer::stop -->
<div class="<?= $this->class ?> block"<?= $this->cssID ?><?php if ($this->style): ?> style="<?= $this->style ?>"<?php endif; ?>>
<?php if ($this->headline): ?>
<<?= $this->hl ?>><?= $this->headline ?></<?= $this->hl ?>>
<?php endif; ?>
<form<?php if ($this->action): ?> action="<?= $this->action ?>"<?php endif; ?> method="<?= $this->method ?>" enctype="<?= $this->enctype ?>"<?= $this->attributes ?><?= $this->novalidate ?>>
<div class="formbody">
<?php if ($this->method !== 'get'): ?>
<input type="hidden" name="FORM_SUBMIT" value="<?= $this->formSubmit ?>">
<input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}">
<?php endif; ?>
<?= $this->hidden ?>
<?= $this->fields ?>
</div>
</form>
<script>
window.addEventListener('DOMContentLoaded', () => {
const el = document.querySelector('input[name="FORM_SUBMIT"][value="<?= $this->formSubmit; ?>"]').form;
function request(method, form, url, body, callback) {
const xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Accept', 'text/html');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Contao-Ajax-Form', form.querySelector('[name="FORM_SUBMIT"]').value);
form.ariaBusy = 'true';
form.dataset.ajaxForm = 'loading';
form.classList.add('ajax-loading');
xhr.onload = () => {
form.dispatchEvent(new CustomEvent('ajax-form-onload', {
bubbles: true,
detail: { body, form, xhr },
}));
form.ariaBusy = 'false';
form.dataset.ajaxForm = '';
callback(xhr);
};
xhr.send(body || null)
}
function replaceForm(xhr, form) {
const range = document.createRange();
range.selectNode(form.parentNode);
const newForm = range.createContextualFragment(xhr.responseText).firstElementChild;
form.replaceWith(newForm);
if (!newForm.getAttribute('action')) {
newForm.action = xhr.responseURL;
}
initForm(newForm);
var event = new Event('ajax_change');
newForm.dispatchEvent(event);
window.dispatchEvent(event);
}
function initForm(form) {
form.addEventListener('submit', e => {
e.preventDefault();
const formData = new FormData(form);
// Send the triggered button data as well
if (e.submitter) {
formData.append(e.submitter.name, e.submitter.value);
// Prevent double form submission
e.submitter.disabled = true;
setTimeout(() => e.submitter.disabled = false, 30000);
}
request('POST', form, form.action, formData, xhr => {
const location = xhr.getResponseHeader('X-Ajax-Location');
// Handle the redirect header
if (location) {
request('GET', form, location, null, xhr => replaceForm(xhr, form));
return;
}
replaceForm(xhr,form);
});
});
}
initForm(el);
});
</script>
</div>
<!-- indexer::continue --> ajaxform_inline.html5 <form<?php if ($this->action): ?> action="<?= $this->action ?>"<?php endif; ?> method="<?= $this->method ?>" enctype="<?= $this->enctype ?>"<?= $this->attributes ?><?= $this->novalidate ?>>
<div class="formbody">
<?php if ($this->method !== 'get'): ?>
<input type="hidden" name="FORM_SUBMIT" value="<?= $this->formSubmit ?>">
<input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}">
<?php endif; ?>
<?= $this->hidden ?>
<?= $this->fields ?>
</div>
</form> |
To ensure that the conditions for previous steps work with MP_Forms, a data attribute is filled with values in the form tag.
When used with ajax-form, the form tag is not updated and therefore no current values can be read from conditional-formfields.
As a workaround, the template ajaxform_inline.html5 can be customised:
The behaviour with Contao 5 still needs to be checked.
The text was updated successfully, but these errors were encountered: