From 7aff9a893a010d5bd8c471e249b68f99a94fef14 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 14 Dec 2023 10:02:55 -0700 Subject: [PATCH 1/2] XHR/Ajax form submission --- CHANGELOG.md | 6 ++++++ form.php | 4 ++++ templates/form-xhr.html.twig | 1 + templates/forms/default/form.html.twig | 1 + templates/forms/form.html.twig | 5 +++++ templates/forms/layouts/xhr.html.twig | 21 +++++++++++++++++++++ 6 files changed, 38 insertions(+) create mode 100644 templates/form-xhr.html.twig create mode 100644 templates/forms/layouts/xhr.html.twig diff --git a/CHANGELOG.md b/CHANGELOG.md index e9ab078..38cde15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v7.3.0 +## mm/dd/2023 + +1. [](#new) + * Added XHR/Ajax form submission as an option in the form blueprint. See [Learn Forms](https://learn.getgrav.org/17/forms/forms/how-to-ajax-submission) for details. + # v7.2.2 ## 12/13/2023 diff --git a/form.php b/form.php index 8355a03..fb77515 100644 --- a/form.php +++ b/form.php @@ -1135,6 +1135,10 @@ protected function shouldProcessForm(): bool return false; } + if (isset($form->xhr_submit) && $form->xhr_submit) { + $form->set('template', $form->template ?? 'form-xhr'); + } + // Set page template if passed by form if (isset($form->template)) { $this->grav['page']->template($form->template); diff --git a/templates/form-xhr.html.twig b/templates/form-xhr.html.twig new file mode 100644 index 0000000..eea54d4 --- /dev/null +++ b/templates/form-xhr.html.twig @@ -0,0 +1 @@ +{% extends "forms/default/form.html.twig" %} \ No newline at end of file diff --git a/templates/forms/default/form.html.twig b/templates/forms/default/form.html.twig index 7be60d9..263972d 100644 --- a/templates/forms/default/form.html.twig +++ b/templates/forms/default/form.html.twig @@ -1,3 +1,4 @@ +{% block xhr %}{% endblock %} {% set form = form ?? grav.session.getFlashObject('form') %} {% set layout = layout ?? form.layout ?? 'default' %} {% set field_layout = field_layout ?? layout %} diff --git a/templates/forms/form.html.twig b/templates/forms/form.html.twig index 9d28cb3..53ec58c 100644 --- a/templates/forms/form.html.twig +++ b/templates/forms/form.html.twig @@ -26,4 +26,9 @@ You can also override individual fields by copying (using text field as an examp templates/forms/fields/text/text.html.twig -> templates/forms/fields/text/tailwind-text.html.twig #} + {% extends "forms/default/form.html.twig" %} + +{% block xhr %} +{% include 'forms/layouts/xhr.html.twig' %} +{% endblock %} diff --git a/templates/forms/layouts/xhr.html.twig b/templates/forms/layouts/xhr.html.twig new file mode 100644 index 0000000..c8d60bc --- /dev/null +++ b/templates/forms/layouts/xhr.html.twig @@ -0,0 +1,21 @@ +{% if form.xhr_submit == true %} + {% do assets.addInlineJs(" + document.addEventListener('DOMContentLoaded', function() { + var form = document.getElementById('" ~ form.id ~ "'); + form.addEventListener('submit', function(e) { + // prevent standard form submission + e.preventDefault(); + // submit the form via Ajax + var xhr = new XMLHttpRequest(); + xhr.open(form.getAttribute('method'), form.getAttribute('action')); + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.onload = function() { + if (xhr.status === 200) { + document.getElementById('" ~ form.id ~ "').innerHTML = xhr.responseText; + } + }; + xhr.send(new URLSearchParams(new FormData(form)).toString()); + }); + }); + ", {'group': 'bottom', 'position': 'before', 'priority': 100}) %} +{% endif %} \ No newline at end of file From 8870204d58edf6b53c1a8a86671198ebb520e02d Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 14 Dec 2023 12:28:34 -0700 Subject: [PATCH 2/2] prepare for release --- CHANGELOG.md | 2 +- blueprints.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38cde15..074fa22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # v7.3.0 -## mm/dd/2023 +## 12/14/2023 1. [](#new) * Added XHR/Ajax form submission as an option in the form blueprint. See [Learn Forms](https://learn.getgrav.org/17/forms/forms/how-to-ajax-submission) for details. diff --git a/blueprints.yaml b/blueprints.yaml index 3c33671..ccb759a 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,7 +1,7 @@ name: Form slug: form type: plugin -version: 7.2.2 +version: 7.3.0 description: Enables forms handling and processing icon: check-square author: