Skip to content

[6.x] Extract form submission logic into SubmitForm action#14375

Open
duncanmcclean wants to merge 8 commits into6.xfrom
submit-form-action
Open

[6.x] Extract form submission logic into SubmitForm action#14375
duncanmcclean wants to merge 8 commits into6.xfrom
submit-form-action

Conversation

@duncanmcclean
Copy link
Copy Markdown
Member

@duncanmcclean duncanmcclean commented Mar 27, 2026

This pull request extracts the form submission logic from Statamic's FormController into a reusable SubmitForm action class, allowing third-party developers to use it in Livewire components or custom API endpoints.

Usage

use Statamic\Facades\Form;
use Statamic\Facades\Site;
use Statamic\Forms\SubmitForm;
use Statamic\Exceptions\SilentFormFailureException;
use Illuminate\Validation\ValidationException;

$form = Form::find('contact');

try {
    $submission = app(SubmitForm::class)->submit(
        form: $form,
        data: ['name' => 'John', 'email' => 'john@example.com'],
        files: [], // Optional
        site: Site::current(), // Optional
    );
} catch (ValidationException $e) {
    return back()->withErrors($e->errors());
} catch (SilentFormFailureException $e) {
    // Honeypot triggered or event listener rejected
    // $e->submission() contains the submission data

    return back()->with('success', 'Form submitted successfully!');
}

return back()->with('success', 'Form submitted successfully!');

Closes statamic/ideas#1409

@duncanmcclean duncanmcclean changed the title Extract form submission logic into SubmitForm action [6.x] Extract form submission logic into SubmitForm action Mar 27, 2026
Copy link
Copy Markdown
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make the action class injectable and you call submit on it, instead of invoking it.

The submit method should also perform validation. Otherwise it'd be very easy to forget to validate and pass in raw request values.

The validator could still be exposed from that class if you wanted to separately validate, like in a livewire component.

The FrontendFormRequest could be cleaned up to avoid validation, since the action would be doing it. And actually our controller seems to already handle ValidationExceptions (thrown by events) but it could catch the one thrown by submit too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Abstract FormController actions to a reusable behaviour

2 participants