-
-
Notifications
You must be signed in to change notification settings - Fork 621
[6.x] Extract form submission logic into SubmitForm action
#14375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
duncanmcclean
wants to merge
8
commits into
6.x
Choose a base branch
from
submit-form-action
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7491270
Extract form submission logic into `SubmitForm` action
duncanmcclean 4e6de8b
Add types
duncanmcclean 264f05c
Move logic related tests into `SubmitFormTest`
duncanmcclean 7dd7364
rename method
duncanmcclean 000eb09
refactor validation into action
duncanmcclean 6b30f38
private
duncanmcclean dc5afe7
dependency injection
duncanmcclean 726be41
avoid static
duncanmcclean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Forms; | ||
|
|
||
| use Illuminate\Contracts\Validation\Validator; | ||
| 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 | ||
| { | ||
| public function __invoke( | ||
| Form $form, | ||
| array $data = [], | ||
| array $files = [], | ||
| ?Site $site = null, | ||
| ): Submission { | ||
| $uploadedAssets = []; | ||
|
|
||
| $values = array_merge($data, $files); | ||
| $fields = $form->blueprint()->fields(); | ||
| $fields = $fields->addValues($values); | ||
| $site = $site ?? Facades\Site::default(); | ||
|
|
||
| $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 static function validator(Form $form, array $data, array $files = []): Validator | ||
duncanmcclean marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $values = array_merge($data, $files); | ||
| $fields = $form->blueprint()->fields()->addValues($values); | ||
|
|
||
| return $fields | ||
| ->validator() | ||
| ->withRules(static::extraRules($fields)) | ||
| ->validator(); | ||
| } | ||
|
|
||
| protected static function extraRules($fields): array | ||
duncanmcclean marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| return $fields->all() | ||
| ->filter(fn ($field) => $field->fieldtype()->handle() === 'assets') | ||
| ->mapWithKeys(function ($field) { | ||
| return [$field->handle().'.*' => ['file', new AllowedFile]]; | ||
| }) | ||
| ->all(); | ||
| } | ||
|
|
||
| /** | ||
| * Remove any uploaded assets. | ||
| * | ||
| * Triggered by a validation exception or silent failure. | ||
| */ | ||
| protected function removeUploadedAssets(array $assets): void | ||
duncanmcclean marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| collect($assets) | ||
| ->flatten() | ||
| ->each(function ($id) { | ||
| if ($asset = Asset::find($id)) { | ||
| $asset->delete(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.