From ddf6e40cd8bc5f46a821837cdc52bd07af5f6c48 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 20 Apr 2017 12:02:19 -0600 Subject: [PATCH 1/5] Potential fix for non-form _POST --- form.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/form.php b/form.php index ddf871a2..f6e3705d 100644 --- a/form.php +++ b/form.php @@ -181,7 +181,7 @@ public function onPagesInitialized() } // Handle posting if needed. - if (!empty($_POST)) { + if (!empty($_POST) && isset($_POST['data'])) { $current_form_name = $this->getFormName($this->grav['page']); $this->json_response = []; From 6940129992ef531ab2251750b59a2caf1fc3a0b1 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 20 Apr 2017 12:06:51 -0600 Subject: [PATCH 2/5] Extra check for $this->form --- CHANGELOG.md | 6 ++++++ form.php | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e5cc764..2406ff39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v2.4.1 +## 04/xx/2017 + +1. [](#bugfix) + + # v2.4.0 ## 04/19/2017 diff --git a/form.php b/form.php index f6e3705d..80c92663 100644 --- a/form.php +++ b/form.php @@ -191,11 +191,13 @@ public function onPagesInitialized() $this->form = new Form($this->grav['page']); } - if ($this->grav['uri']->extension() === 'json' && isset($_POST['__form-file-uploader__'])) { - $this->json_response = $this->form->uploadFiles(); - } else { - $this->form->post(); - $submitted = true; + 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; + } } } From a93614ba88b41254ec2d234eac7323aa695d1600 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 21 Apr 2017 19:27:31 -0600 Subject: [PATCH 3/5] Support proper form handling with nested fields #141 --- CHANGELOG.md | 6 +-- classes/form.php | 60 +++++++++++++------------- templates/forms/default/data.html.twig | 2 +- templates/forms/default/data.txt.twig | 2 +- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2406ff39..220f7e0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ -# v2.4.1 +# v2.5.0 ## 04/xx/2017 -1. [](#bugfix) - +1. [](#new) + * Support proper form handling with nested fields [#141](https://github.com/getgrav/grav-plugin-form/issues/141) # v2.4.0 ## 04/19/2017 diff --git a/classes/form.php b/classes/form.php index 4607df0b..6d139bf8 100644 --- a/classes/form.php +++ b/classes/form.php @@ -192,36 +192,9 @@ public function reset() $name = $this->items['name']; $grav = Grav::instance(); - // Fix naming for fields (presently only for toplevel fields) + // Fix naming for fields (supports nested fields now!) if (isset($this->items['fields'])) { - foreach ($this->items['fields'] as $key => $field) { - // default to text if not set - if (!isset($field['type'])) { - $field['type'] = 'text'; - } - - $types = $grav['plugins']->formFieldTypes; - - // manually merging the field types - if ($types !== null && key_exists($field['type'], $types)) { - $field += $types[$field['type']]; - } - - // BC for old style of array style field definitions - if (is_numeric($key) && isset($field['name'])) { - array_splice($this->items['fields'], $key); - $key = $field['name']; - } - - // Add name based on key if not already set - if (!isset($field['name'])) { - $field['name'] = $key; - } - - // set any modifications back on the fields array - $this->items['fields'][$key] = $field; - - } + $this->items['fields'] = $this->processFields($this->items['fields']); } $items = $this->items; @@ -242,6 +215,35 @@ public function reset() } + protected function processFields($fields) + { + $types = Grav::instance()['plugins']->formFieldTypes; + + $return = array(); + foreach ($fields as $key => $value) { + + // default to text if not set + if (!isset($value['type'])) { + $value['type'] = 'text'; + } + + // manually merging the field types + if ($types !== null && key_exists($value['type'], $types)) { + $value += $types[$value['type']]; + } + + // Fix numeric indexes + if (is_numeric($key) && isset($value['name'])) { + $key = $value['name']; + } + if (isset($value['fields']) && is_array($value['fields'])) { + $value['fields'] = $this->processFields($value['fields']); + } + $return[$key] = $value; + } + return $return; + } + public function fields() { if (is_null($this->fields)) { diff --git a/templates/forms/default/data.html.twig b/templates/forms/default/data.html.twig index 9f54f282..d8a950fa 100644 --- a/templates/forms/default/data.html.twig +++ b/templates/forms/default/data.html.twig @@ -28,7 +28,7 @@ {% endblock %} {% endif %} {% else %} - {% if field.fields is iterable %} + {% if field.fields %} {{ _self.render_field(form, field.fields) }} {% endif %} {% endif %} diff --git a/templates/forms/default/data.txt.twig b/templates/forms/default/data.txt.twig index ff9eb864..0b03704d 100644 --- a/templates/forms/default/data.txt.twig +++ b/templates/forms/default/data.txt.twig @@ -5,7 +5,7 @@ {%- set value = form.value(field.name ?? index) %} {{- field.name ?? index }}: {{ string((value is iterable ? value|json_encode : value)) ~ "\r\n" }} {%- else %} - {%- if field.fields is iterable %} + {%- if field.fields %} {{- _self.render_field(form, field.fields) }} {%- endif %} {%- endif %} From da35f482b78d91d70ea74a3faec53d9ade43bc5c Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 24 Apr 2017 15:27:38 -0600 Subject: [PATCH 4/5] changelog updated --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 220f7e0a..f8ac5e9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,10 @@ # v2.5.0 -## 04/xx/2017 +## 04/24/2017 1. [](#new) * Support proper form handling with nested fields [#141](https://github.com/getgrav/grav-plugin-form/issues/141) +1. [](#bugfix) + * Added check for valid Grav forms before trying to create a form object # v2.4.0 ## 04/19/2017 From cd2be1b2c4a43eb0826e8fff3771d6dcdd02082a Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 24 Apr 2017 15:28:10 -0600 Subject: [PATCH 5/5] Prepare for release --- blueprints.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blueprints.yaml b/blueprints.yaml index c3163811..83ca45d0 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Form -version: 2.4.0 +version: 2.5.0 description: Enables the forms handling icon: check-square author: