Skip to content

Commit e65b363

Browse files
committed
Add file uploaded error message and enctype
1 parent eb9f3b3 commit e65b363

File tree

16 files changed

+81
-31
lines changed

16 files changed

+81
-31
lines changed

.github/workflows/code-analysis.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: 'PHPStan'
1818
run: composer phpstan
1919
- name: 'Coding Standards'
20-
run: composer fix-cs
20+
run: composer check-cs
2121
name: ${{ matrix.actions.name }}
2222
runs-on: ubuntu-latest
2323
steps:

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Release Notes for Snaptcha
22

3-
## 5.0.0 - 2024-04-08
3+
## 5.1.0 - Unreleased
4+
5+
- Added an error message that is displayed if Snaptcha identifies a submission as spam when a file is uploaded, in which case the user must go back and resubmit their data ([#32](https://github.com/putyourlightson/craft-snaptcha/issues/32)).
6+
- Added the ability to maintain the encoding type of the submitted form on the error page.
47

5-
### Added
8+
## 5.0.0 - 2024-04-08
69

710
- Added compatibility with Craft 5.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "putyourlightson/craft-snaptcha",
33
"description": "Automatically validates forms and prevents spam bots from submitting to your site.",
4-
"version": "5.0.0",
4+
"version": "5.1.0",
55
"type": "craft-plugin",
66
"homepage": "https://putyourlightson.com/plugins/snaptcha",
77
"license": "proprietary",

src/Snaptcha.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ public function validateField(ActionEvent $event): void
179179
if (!$this->snaptcha->validateField($value, $event->action)) {
180180
$variables = [
181181
'settings' => $this->settings,
182+
'hasFileUpload' => $this->snaptcha->hasFileUpload(),
183+
'encodingType' => $this->snaptcha->getEncodingType(),
182184
'postedValues' => $this->snaptcha->getPostedValues(),
183185
];
184186

src/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
// The message that will be displayed if Snaptcha identifies a submission as spam.
4141
//'errorMessage' => 'For some reason your submission was flagged as spam. Please click the button below to resubmit your data.'
4242

43+
// The message that will be displayed when a file is uploaded, in which case the user must go back and resubmit their data.
44+
//'errorMessageFileUploaded' => 'For some reason your submission was flagged as spam. Since a file was uploaded, you must go back and resubmit your data.'
45+
4346
// The error that will be displayed if Snaptcha identifies a submission as spam and JavaScript is disabled.
4447
//'errorJavascriptDisabled' => 'JavaScript must be enabled for your submission to be verified. Please enable it and refresh the page.'
4548

src/models/SettingsModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class SettingsModel extends Model
4949
*/
5050
public string $errorMessage = 'For some reason your submission was flagged as spam. Please click the button below to resubmit your data.';
5151

52+
/**
53+
* @var string
54+
*/
55+
public string $errorMessageFileUploaded = 'For some reason your submission was flagged as spam. Since a file was uploaded, you must go back and resubmit your data.';
56+
5257
/**
5358
* @var string
5459
*/

src/services/SnaptchaService.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,22 @@ public function getFieldValue(SnaptchaModel $model): ?string
6868
return $record ? $record->value : null;
6969
}
7070

71+
/**
72+
* Returns the encoding type of the request.
73+
*/
74+
public function getEncodingType(): string
75+
{
76+
$contentType = Craft::$app->getRequest()->getHeaders()->get('Content-Type');
77+
78+
return is_string($contentType) ? $contentType : '';
79+
}
80+
7181
/**
7282
* Returns flattened posted values.
7383
*/
7484
public function getPostedValues(): array
7585
{
76-
$values = Craft::$app->request->getBodyParams();
86+
$values = Craft::$app->getRequest()->getBodyParams();
7787

7888
if (isset($values[Snaptcha::$plugin->settings->fieldName])) {
7989
unset($values[Snaptcha::$plugin->settings->fieldName]);
@@ -82,6 +92,14 @@ public function getPostedValues(): array
8292
return $this->flattenValues($values);
8393
}
8494

95+
/**
96+
* Returns whether the request has a file upload.
97+
*/
98+
public function hasFileUpload(): bool
99+
{
100+
return !empty($_FILES) && $_FILES['file']['error'] === UPLOAD_ERR_OK;
101+
}
102+
85103
/**
86104
* Returns whether the controller action is excluded from validation.
87105
*/

src/templates/_error.twig

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@
1010
<h2>{{ settings.errorTitle }}</h2>
1111

1212
<p id="error" class="hidden">
13-
{{ settings.errorMessage }}
13+
{{ hasFileUpload ? settings.errorMessageFileUpload : settings.errorMessage }}
1414
</p>
1515

1616
<p id="warning" class="warning">
1717
{{ settings.errorJavascriptDisabled }}
1818
</p>
1919

20-
<form action="" method="post">
21-
{% for name, value in postedValues %}
22-
{{ hiddenInput(name, value) }}
23-
{% endfor %}
24-
25-
{{ craft.snaptcha.field }}
26-
27-
<button id="resubmit" type="submit" class="btn submit disabled" data-timeout="{{ settings.minimumSubmitTime }}">
28-
{{ settings.errorButtonText }}
29-
</button>
30-
<div id="graphic" class="spinner hidden"></div>
31-
</form>
20+
{% if hasFileUpload %}
21+
<button onclick="history.back()" class="btn submit">Back</button>
22+
{% else %}
23+
<form action="" method="post" enctype="{{ encodingType }}">
24+
{% for name, value in postedValues %}
25+
{{ hiddenInput(name, value) }}
26+
{% endfor %}
27+
28+
{{ craft.snaptcha.field }}
29+
30+
<button id="resubmit" type="submit" class="btn submit disabled" data-timeout="{{ settings.minimumSubmitTime }}">
31+
{{ settings.errorButtonText }}
32+
</button>
33+
<div id="graphic" class="spinner hidden"></div>
34+
</form>
35+
{% endif %}
3236

3337
{% endblock %}

src/templates/_settings.twig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@
7676
errors: settings.getErrors('errorMessage')
7777
}) }}
7878

79+
{{ forms.textField({
80+
required: true,
81+
label: 'Error Message File Uploaded'|t('snaptcha'),
82+
name: 'errorMessageFileUploaded',
83+
instructions: 'The message that will be displayed when a file is uploaded, in which case the user must go back and resubmit their data.'|t('snaptcha'),
84+
warning: (config.errorMessageFileUploaded is defined ? configWarning('errorMessageFileUploaded')),
85+
value: settings.errorMessageFileUploaded,
86+
errors: settings.getErrors('errorMessageFileUploaded')
87+
}) }}
88+
7989
{{ forms.textField({
8090
required: true,
8191
label: 'Error JavaScript Disabled'|t('snaptcha'),
File renamed without changes.

0 commit comments

Comments
 (0)