diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c1da102..2631bf58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# v2.5.0 +## 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 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: 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/form.php b/form.php index ddf871a2..80c92663 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 = []; @@ -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; + } } } 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 %}