Skip to content

[6.x] Support Alpine's CSP build in the Alpine forms JS driver - #16157

Open
edalzell wants to merge 1 commit into
statamic:6.xfrom
edalzell:alpine-driver-csp
Open

edalzell wants to merge 1 commit into
statamic:6.xfrom
edalzell:alpine-driver-csp

Conversation

@edalzell

@edalzell edalzell commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

This pull request makes the alpine forms JS driver work with Alpine's CSP build, including the copy bundled in Livewire's livewire.csp.js.

Conditional fields were never hidden on sites using the CSP build. The x-data and x-model output worked, but every show_field expression evaluated to undefined.

This was happening because the driver renders Statamic.$conditions.showField(...), and the CSP build's expression evaluator can only see Alpine scope and magics, not window globals. So Statamic is undefined. It also rejects any value that is a window global, so a magic that returns window.Statamic fails too (Accessing global variables is prohibited in the CSP build) when helpers.js loads before Alpine.

This PR fixes it by having helpers.js register a $statamic magic, and having the driver render $statamic.$conditions.showField(...) instead. The magic returns a copy of window.Statamic that shares the same $conditions and $passkeys instances, which the CSP evaluator accepts. It registers immediately if window.Alpine exists, otherwise on alpine:init, so helpers.js can load before or after Alpine.

window.Statamic is unchanged, so any custom templates that use Statamic.$conditions still work on the standard build.

Testing

  • Updated the FormCreateAlpineTest assertions for the new expression.
  • Added AlpineMagic.test.js, which covers both registration paths, checks the magic doesn't return the global itself, and toggles a field with real Alpine.
  • Tested in a browser using the real form markup from a Statamic 6.44 site, served with script-src 'self' (no unsafe-eval):
    • Livewire 4.4.8 livewire.csp.js, with helpers.js loaded before and after it: the conditional field hides and shows correctly. On 6.x it never hides.
    • Standard Alpine 3.15 CDN build: still works.

Notes

  • helpers.js still has to load before Alpine starts, same as now. If Alpine starts first, the fields break on 6.x and on this branch.
  • Sites need the updated helpers.js. statamic:install republishes it with --force, which runs on composer update for standard sites. A site that bundles or pins its own copy would need to update it.
  • There's no CSP-build JS test, since @alpinejs/csp isn't a dependency. Happy to add it as a dev dependency if you want that covered.

Alpine's CSP build can't see window globals, so the driver's `Statamic.$conditions.showField(...)` expressions never evaluated and conditional fields were always shown. The frontend helpers now register a `$statamic` magic and the driver uses it. `window.Statamic` is unchanged.

This branch has not been deployed

No deployments
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.

1 participant