diff --git a/CHANGELOG.md b/CHANGELOG.md index 29eb452..869ec16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.0.2 - 2021-04-16 +### Changed +- The keys of posted arrays are now preserved when a submission is flagged as spam. + ## 3.0.1 - 2021-04-14 ### Fixed - Fixed a bug in which an error could be thrown if values were passed in as an array and a submission was flagged as spam. diff --git a/composer.json b/composer.json index 80ddc5e..cdb5ba6 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "putyourlightson/craft-snaptcha", "description": "Automatically validates forms and prevents spam bots from submitting to your site.", - "version": "3.0.1", + "version": "3.0.2", "type": "craft-plugin", "homepage": "https://putyourlightson.com/plugins/snaptcha", "license": "proprietary", diff --git a/src/Snaptcha.php b/src/Snaptcha.php index 3ddeb88..d87307d 100644 --- a/src/Snaptcha.php +++ b/src/Snaptcha.php @@ -102,7 +102,7 @@ public function validateField(ActionEvent $event) if ($this->validated === false) { $variables = [ 'settings' => $this->settings, - 'postedValues' => Craft::$app->request->getBodyParams(), + 'postedValues' => $this->snaptcha->getPostedValues(), ]; if ($this->settings->errorTemplate) { diff --git a/src/services/SnaptchaService.php b/src/services/SnaptchaService.php index 8d6f1bc..a7b42e5 100644 --- a/src/services/SnaptchaService.php +++ b/src/services/SnaptchaService.php @@ -67,6 +67,17 @@ public function getFieldValue(SnaptchaModel $model) return $record ? $record->value : null; } + public function getPostedValues(): array + { + $values = Craft::$app->request->getBodyParams(); + + if (isset($values[Snaptcha::$plugin->settings->fieldName])) { + unset($values[Snaptcha::$plugin->settings->fieldName]); + } + + return $this->_flattenValues($values); + } + /** * Returns whether the controller action is excluded from validation. * @@ -342,6 +353,32 @@ private function _getNormalizedArray($values): array return $values; } + /** + * Flattens a multi-dimensional array of values to a flat array that can + * be used to output hidden fields, preserving the keys. + * + * @param array $values + * @param string $currentKey + * @return array + */ + private function _flattenValues(array $values, string $currentKey = ''): array + { + $flattened = []; + + foreach ($values as $key => $value) { + $key = $currentKey ? $currentKey.'['.$key.']' : $key; + + if (is_array($value)) { + $flattened = array_merge($flattened, $this->_flattenValues($value, $key)); + } + else { + $flattened[$key] = $value; + } + } + + return $flattened; + } + /** * Rejects and logs a form submission. * diff --git a/src/templates/_error.html b/src/templates/_error.html index b2fd030..aebe977 100644 --- a/src/templates/_error.html +++ b/src/templates/_error.html @@ -4,16 +4,6 @@ {% set title = settings.errorTitle %} -{% macro hiddenInput(name, value) %} - {% if value is iterable %} - {% for val in value %} - {{ _self.hiddenInput(name ~ '[]', val) }} - {% endfor %} - {% else %} - {{ hiddenInput(name, value) }} - {% endif %} -{% endmacro %} - {% block message %} @@ -28,8 +18,8 @@