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

Doesn't work with Phoenix Live View #68

Open
michaelforrest opened this issue Jul 13, 2021 · 4 comments
Open

Doesn't work with Phoenix Live View #68

michaelforrest opened this issue Jul 13, 2021 · 4 comments

Comments

@michaelforrest
Copy link

I'm struggling to get this to work with Phoenix Live View.

The live view submit handler is called before the recaptcha javascript can go through its sequence and update the form with a g-recaptcha-response field.


Here I'm inspecting the form generated with a phx_submit callback. The second handler is recaptcha's code, but the form has already been handled via live view by the time it gets to recaptchaCallback

I'm looking into live view's JS interoperability features but I'm not seeing a hook function that can be inserted before the submit event.

Actually it doesn't ever seem to be calling recaptchaCallback - I'm not sure how JS scope works if elements containing functions are replaced in the DOM. I see that the form value is being assigned to the window scope so maybe that's getting overwritten somehow as part of the live view render sequence.

I expect I'll be updating this thread with my findings as I go. Sorry not to have a test case for you.

@michaelforrest
Copy link
Author

I feel like I need to update the recaptcha result with an update callback in a live view hook (instead of on form submit) but this would run the risk of getting stale if a user takes too long to finish the form. But I'm not sure how else to insert it into the sequence.

@ivanhercaz
Copy link

ivanhercaz commented Oct 31, 2021

Hi @michaelforrest!
I am using ReCaptacha on a LiveView too and I had problems rendering it: Recaptcha.Template.display/1 renders the script tag and the container div but not the iframe. But... I solved it adding phx-update=ignore to the container div in which I added the display function.

I hope it can helps you. Although is not complicated, I leave here the code in case it can helps to illustrate what I explained:

<div phx-update="ignore">
  <%= raw Recaptcha.Template.display(hl: "es") %>
</div>

@michaelforrest
Copy link
Author

Thanks @ivanhercaz - I wasn't aware of phx-update="ignore" (somehow missed it despite having trawled through the docs and source code) so even though I'm now handling recaptcha outside of a live view this will be something to try next time!

@guess
Copy link

guess commented Jul 9, 2023

Thanks @ivanhercaz! This should really be added to the documentation.

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

No branches or pull requests

3 participants