From 534f1bbcb93f4a524be9b0a39560470e22c33cba Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 12 May 2017 08:16:04 -0600 Subject: [PATCH 1/2] Refactor form-processing (#152) * refactor form-processing to take non-pre-processed forms into account * cleanup * Reference #149 in changelog * Fix on-page forms * Fix fieldset forms not being reopulated #140 --- CHANGELOG.md | 7 +++ form.php | 57 ++++++++----------- .../forms/fields/fieldset/fieldset.html.twig | 2 +- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69852e0c..d836324a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v2.6.1 +## 05/xx/2017 + +1. [](#bugfix) + * Fix issue with dynamically added forms (Registration, Profile, Comments, etc) not processed [#149](https://github.com/getgrav/grav-plugin-form/issues/149) + * Fixed issue with nested values not being repopulated on form error [#140](https://github.com/getgrav/grav-plugin-form/issues/140) + # v2.6.0 ## 05/04/2017 diff --git a/form.php b/form.php index 153a0665..449fd3c4 100644 --- a/form.php +++ b/form.php @@ -125,7 +125,7 @@ public function onPageProcessed(Event $e) public function onPagesInitialized() { $submitted = false; - + $this->json_response = []; $cache_id = $this->grav['pages']->getPagesCacheId() . '-form-plugin'; // Get and set the cache of forms if it exists @@ -141,35 +141,21 @@ public function onPagesInitialized() $this->flat_forms = $flat_forms; } - // Enable form events if there's a POST - if (!empty($_POST)) { - $this->enable([ - 'onFormProcessed' => ['onFormProcessed', 0], - 'onFormValidationError' => ['onFormValidationError', 0] - ]); - } - - if ($this->isAdmin() && !empty($_POST)) { + // No forms in pages, try the current one in the page + if (empty($this->forms)) { $page = $this->grav['page']; if (!$page) { return; } + // Create form from page $header = $page->header(); - if (isset($header->form) && is_array($header->form)) { - // Create form $this->form = new Form($page); - $this->form->post(); } - } elseif ($this->forms) { - - $this->enable([ - 'onFormFieldTypes' => ['onFormFieldTypes', 0], - ]); - + } else { // Regenerate list of flat_forms if not already populated if (empty($this->flat_forms)) { $this->flat_forms = Utils::arrayFlatten($this->forms); @@ -179,28 +165,33 @@ public function onPagesInitialized() if ($this->recache_forms) { $this->grav['cache']->save($cache_id, [$this->forms, $this->flat_forms]); } + } - // Handle posting if needed. - if (!empty($_POST) && (isset($_POST['data']) || isset($_POST['__form-file-uploader__']))) { + // Enable form events if there's a POST + if (isset($_POST) && isset($_POST['form-nonce'])) { + $this->enable([ + 'onFormProcessed' => ['onFormProcessed', 0], + 'onFormValidationError' => ['onFormValidationError', 0], + 'onFormFieldTypes' => ['onFormFieldTypes', 0], + ]); + // Retrieve the form if it's not already set + if (!isset($this->form)) { $current_form_name = $this->getFormName($this->grav['page']); - $this->json_response = []; $this->form = $this->getFormByName($current_form_name); + } - if (!$this->form && isset($this->grav['page']->header()->form)) { - $this->form = new Form($this->grav['page']); - } - - if ($this->form) { - if ($this->grav['uri']->extension() === 'json' && isset($_POST['__form-file-uploader__'])) { - $this->json_response = $this->form->uploadFiles(); - } else { - $this->form->post(); - $submitted = true; - } + // Post the form + if ($this->form) { + if ($this->grav['uri']->extension() === 'json' && isset($_POST['__form-file-uploader__'])) { + $this->json_response = $this->form->uploadFiles(); + } else { + $this->form->post(); + $submitted = true; } } + // Clear flash objects for previously uploaded files // whenever the user switches page / reloads // ignoring any JSON / extension call diff --git a/templates/forms/fields/fieldset/fieldset.html.twig b/templates/forms/fields/fieldset/fieldset.html.twig index 36a30517..b934e231 100644 --- a/templates/forms/fields/fieldset/fieldset.html.twig +++ b/templates/forms/fields/fieldset/fieldset.html.twig @@ -6,7 +6,7 @@ {% if field.fields %} {% for field in field.fields %} {% if field.type %} - {% set value = data.value(field.name) %} + {% set value = form.value(field.name) %} {% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %} {% endif %} {% endfor %} From 64b252ec779b9b533cc53275c3e35e5b67cd4020 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 16 May 2017 12:54:36 -0600 Subject: [PATCH 2/2] Prepare for release --- CHANGELOG.md | 4 ++-- blueprints.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d836324a..d4778965 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -# v2.6.1 -## 05/xx/2017 +# v2.7.0 +## 05/16/2017 1. [](#bugfix) * Fix issue with dynamically added forms (Registration, Profile, Comments, etc) not processed [#149](https://github.com/getgrav/grav-plugin-form/issues/149) diff --git a/blueprints.yaml b/blueprints.yaml index b9653690..515f9d93 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Form -version: 2.6.0 +version: 2.7.0 description: Enables the forms handling icon: check-square author: