-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathSubmitForm.php
More file actions
132 lines (109 loc) · 4.04 KB
/
SubmitForm.php
File metadata and controls
132 lines (109 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Statamic\Forms;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Support\Traits\Localizable;
use Illuminate\Validation\ValidationException;
use Statamic\Contracts\Forms\Form;
use Statamic\Contracts\Forms\Submission;
use Statamic\Events\FormSubmitted;
use Statamic\Events\SubmissionCreated;
use Statamic\Exceptions\SilentFormFailureException;
use Statamic\Facades;
use Statamic\Facades\Asset;
use Statamic\Rules\AllowedFile;
use Statamic\Sites\Site;
use Statamic\Support\Arr;
class SubmitForm
{
use Localizable;
public function submit(
Form $form,
array $data = [],
array $files = [],
?Site $site = null,
): Submission {
$values = array_merge($data, $files);
$fields = $form->blueprint()->fields();
$fields = $fields->addValues($values);
$site = $site ?? Facades\Site::default();
$uploadedAssets = [];
$files = $this->normalizeFiles($form, $files);
$this->withLocale($site->lang(), fn () => $this->validator($form, $data, $files)->validate());
$submission = $form->makeSubmission();
try {
throw_if(Arr::get($values, $form->honeypot()), new SilentFormFailureException($submission));
$uploadedAssets = $submission->uploadFiles($files);
$values = array_merge($values, $uploadedAssets);
$submission->data(
$fields->addValues($values)->process()->values()
);
// If any event listeners return false, we'll do a silent failure.
// If they want to add validation errors, they can throw an exception.
throw_if(FormSubmitted::dispatch($submission) === false, new SilentFormFailureException($submission));
} catch (ValidationException|SilentFormFailureException $e) {
$this->removeUploadedAssets($uploadedAssets);
throw $e;
}
if ($form->store()) {
$submission->save();
} else {
// When the submission is saved, this same created event will be dispatched.
// We'll also fire it here if submissions are not configured to be stored
// so that developers may continue to listen and modify it as needed.
SubmissionCreated::dispatch($submission);
}
SendEmails::dispatch($submission, $site);
return $submission;
}
public function validator(Form $form, array $data, array $files = []): Validator
{
$values = array_merge($data, $files);
$fields = $form->blueprint()->fields()->addValues($values);
return $fields
->validator()
->withRules($this->extraRules($fields))
->validator();
}
private function extraRules($fields): array
{
return $fields->all()
->filter(fn ($field) => $field->fieldtype()->handle() === 'assets')
->mapWithKeys(function ($field) {
return [$field->handle().'.*' => ['file', new AllowedFile]];
})
->all();
}
/**
* Normalize uploaded files to arrays.
*
* The assets fieldtype expects arrays, even for `max_files: 1`,
* but we don't want to force that on the front end.
*/
private function normalizeFiles(Form $form, array $files): array
{
$assetFields = $form->blueprint()->fields()->all()
->filter(fn ($field) => in_array($field->fieldtype()->handle(), ['assets', 'files']))
->keys();
foreach ($assetFields as $handle) {
if (isset($files[$handle])) {
$files[$handle] = Arr::wrap($files[$handle]);
}
}
return $files;
}
/**
* Remove any uploaded assets.
*
* Triggered by a validation exception or silent failure.
*/
private function removeUploadedAssets(array $assets): void
{
collect($assets)
->flatten()
->each(function ($id) {
if ($asset = Asset::find($id)) {
$asset->delete();
}
});
}
}