Skip to content
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

Craft 4 vs 5: different behavior when using s-action #408

Closed
janhenckens opened this issue Dec 22, 2024 · 10 comments
Closed

Craft 4 vs 5: different behavior when using s-action #408

janhenckens opened this issue Dec 22, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@janhenckens
Copy link

Bug Report

I'm updating the last of my plugins for Craft 5 and I'm seeing a strange difference in behavior.

I tried a bunch of syntaxes and different forms but to no avail - hoping you can point me in the right direction :)

My form looks like this:

<form sprig s-method="post" s-action="site/cp/save" >
    <p>
        <strong>{{ name ?? "Name not defined" }}</strong><br>
        <strong>{{ option ?? "Option not defined" }}</strong><br>
    </p>
    <div>
        <input id="name" type="text" name="name" value="{{ name ?? '' }}">
    </div>
    <div>
        <select sprig name="option" id="option">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
    </div>
    <button type="submit">Submit</button>
</form>
  • Filling out the name, skipping the select and clicking submit, I see all form fields the request my controller receives
  • But when I fill out the name, choose on option from the select field, the fields don't get submitted back to my controller, not even the select in question.

I have the same setup in my plugin and in Craft 4 this seems to work - so I'm not sure what the issue is.

Plugin Version

3.5.2

Craft CMS Version

5.57

PHP Version

8.2

@janhenckens janhenckens added the bug Something isn't working label Dec 22, 2024
@bencroker
Copy link
Collaborator

But when I fill out the name, choose on option from the select field, the fields don't get submitted back to my controller, not even the select in question.

Are you sure? Can you show me the request URL without the fields in the query string params?

@janhenckens
Copy link
Author

That might not have been the best explanation of what I'm seeing - let me try again :)

On Craft 4, the form reloads/refreshes when changing the select - but it doesn't post to my controller unless I click the submit button myself.
https://share.cleanshot.com/9K1tJsZH

But on Craft 5, the same form posts the controller straight away and then doesn't include any of the form fields
https://share.cleanshot.com/yxcYXFhW

The forms themselves and the controller are exactly the same for both versions.

@bencroker
Copy link
Collaborator

So it’s difficult to help you, seeing so little, but one thing I notice is that the 500 error appears to be the result of a POST request. Is that intentional? If you can send me some test code that I can use to replicate the issue then I’m sure I’ll be able to track it down. Right now my suspicion is that the forms are set up differently on your end.

@bencroker
Copy link
Collaborator

Closing due to lack of a response.

@janhenckens
Copy link
Author

Hey Ben, sorry for the delay.

Both are GET requests.

Full request on Craft 4 (form template here)
https://craft4.ddev.site/index.php?p=admin/actions/sprig-core/components/render&settings%5Bgroup%5D=2&name=&elementType=craft%5Celements%5CEntry&nextStep=2&sprig%3AsiteId=bcb9089341f0daf26aebd76861a8f4149a6025e65758a02f62fb594baaeb24f61&sprig%3Aid=eefe3d242df498823b44b99dc0972c0e757307f5665300556e4fb2db266f6b9fcomponent-rsfkra&sprig%3Acomponent=c5001590203edab0d60bb47f714c0a194534121f89d1eefb96f669a36019f7a1&sprig%3Atemplate=366572ab690ff6008bf8727b546cc358ccd4fb9c38006d03bf5f5e79129b54e9exporter%2Fsprig%2Felement%2Fstep_1&sprig%3Avariables%5BelementTypeOptions%5D=2b6f72015a5424298b56194d92f481697eb92deaa1e5a2179bde1f32a16fdd8c%7B%22craft%5C%5Celements%5C%5CEntry%22%3A%22Entries%22%2C%22craft%5C%5Celements%5C%5CCategory%22%3A%22Categories%22%7D&sprig%3Avariables%5BexportId%5D=c5001590203edab0d60bb47f714c0a194534121f89d1eefb96f669a36019f7a1&sprig%3Avariables%5Bs-trigger%5D=a5fc489640234e54ee743ed50273cdba7bb02664a9d635560426f83d9dfa5936load
Goes to this controller action

On Craft 5: (form template here)
https://craft5.ddev.site/index.php?p=admin/actions/sprig-core/components/render&elementType=craft%5Celements%5CCategory&name=&settings%5Bgroup%5D=&nextStep=2&sprig%3Aaction=c64650eef116005bfba98f35c21e6722919d326fd5935574792cbca6f3467bc3exporter%2Felement%2Fstep1&sprig%3Aconfig=f1ee21789f685a6a448b13772dab10933596a953b9f5565a05d955fc2e0d9315%7B%22id%22%3A%22component-iilnmi%22%2C%22siteId%22%3A1%2C%22template%22%3A%22exporter%5C%2Fsprig%5C%2Felement%5C%2Fstep_1%22%2C%22variables%22%3A%7B%22elementTypeOptions%22%3A%7B%22craft%5C%5Celements%5C%5CEntry%22%3A%22Entries%22%2C%22craft%5C%5Celements%5C%5CCategory%22%3A%22Categories%22%2C%22craft%5C%5Celements%5C%5CUser%22%3A%22Users%22%2C%22verbb%5C%5Cformie%5C%5Celements%5C%5CSubmission%22%3A%22Formie%20Submissions%22%7D%2C%22exportId%22%3Anull%2C%22s-trigger%22%3A%22load%22%7D%7D
Goes to this controller action

Diffs of both files show they are identical but the behavior is different between the two and can't figure out why...

@bencroker bencroker reopened this Jan 18, 2025
@bencroker
Copy link
Collaborator

I just tested the code you provided in the Sprig playground on a Craft 5 project and it works as expected. Can you please test this in your project (and possibly in a second project)?

Note also that you reported that you’re using version 3.5.2 of the plugin, but 3.5.1 is the latest.

<form sprig s-method="post" s-action="site/cp/save" >
    <p>
        <strong>{{ name ?? "Name not defined" }}</strong><br>
        <strong>{{ option ?? "Option not defined" }}</strong><br>
    </p>
    <div>
        <input id="name" type="text" name="name" value="{{ name ?? '' }}">
    </div>
    <div>
        <select sprig name="option" id="option">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
    </div>
    <button type="submit">Submit</button>
</form>

@janhenckens
Copy link
Author

I'm using https://github.com/putyourlightson/craft-sprig-core, which is on version 3.5.2.

I have this form in both a Craft 4 & 5 install and it behaves differently

On Craft 4, changing the fields triggers sprig but doesn't submit the form to the controller.
On Craft 5, changing the fields sends a get request - to my controller action, which expects a post request with body params and thus it crashes.

@bencroker
Copy link
Collaborator

I managed to track this down to s-action attributes being inherited by descendent elements. Fixed in putyourlightson/craft-sprig-core@c6a1daf and released in Sprig Core 3.5.3.

@janhenckens
Copy link
Author

Thanks, updated and that did the trick!

@bencroker
Copy link
Collaborator

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants