Skip to content

Commit

Permalink
Preserve keys in posted arrays when output
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Croker committed Apr 16, 2021
1 parent a8590f0 commit 03acdef
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Snaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions src/services/SnaptchaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
14 changes: 2 additions & 12 deletions src/templates/_error.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand All @@ -28,8 +18,8 @@ <h2>{{ settings.errorTitle }}</h2>
</p>

<form action="" method="post">
{% for name, value in postedValues if name != 'snaptcha' %}
{{ _self.hiddenInput(name, value) }}
{% for name, value in postedValues %}
{{ hiddenInput(name, value) }}
{% endfor %}

{{ craft.snaptcha.field }}
Expand Down

0 comments on commit 03acdef

Please sign in to comment.