Skip to content

Commit

Permalink
Merge pull request #259 from craftcms/bugfix/254-single-attachment-va…
Browse files Browse the repository at this point in the history
…lidation

single attachment file validation
  • Loading branch information
brandonkelly committed Mar 11, 2024
2 parents 8d9d3df + e995b7a commit c5baf99
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Contact Form

## Unreleased

- Fixed a bug where it wasn’t possible to upload a single file with the `attachment` param. ([#254](https://github.com/craftcms/contact-form/issues/254))

## 2.5.2 - 2023-03-16

- Added translations for `Email` and `Name`. ([#235](https://github.com/craftcms/contact-form/issues/235))
Expand Down
4 changes: 4 additions & 0 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function send(Submission $submission, bool $runValidation = true): bool
if ($submission->attachment !== null) {
$allowedFileTypes = Craft::$app->getConfig()->getGeneral()->allowedFileExtensions;

if (!is_array($submission->attachment)) {
$submission->attachment = [$submission->attachment];
}

foreach ($submission->attachment as $attachment) {
if (!$attachment) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/SendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function actionIndex()
if (is_array($_FILES['attachment']['name'])) {
$submission->attachment = UploadedFile::getInstancesByName('attachment');
} else {
$submission->attachment = [UploadedFile::getInstanceByName('attachment')];
$submission->attachment = UploadedFile::getInstanceByName('attachment');
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/models/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class Submission extends Model
public $message;

/**
* @var UploadedFile[]|null[]|null
* @phpstan-var array<UploadedFile|null>|null
* @var UploadedFile|UploadedFile[]|null[]|null
* @phpstan-var UploadedFile|array<UploadedFile|null>|null
*/
public $attachment;

Expand Down

0 comments on commit c5baf99

Please sign in to comment.