Skip to content

Commit 62fc908

Browse files
authored
InvisibleRecaptchaType: fix recaptcha setup and keep everything possible in one place (#1435)
1 parent 6aa1df8 commit 62fc908

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

Diff for: templates/forms.html.twig

+2-8
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,7 @@
3939

4040
{% block invisible_recaptcha_widget -%}
4141
{% if requires_recaptcha() %}
42-
<div
43-
id='recaptcha'
44-
class="g-recaptcha"
45-
data-sitekey="{{ recaptcha_site_key }}"
46-
data-size="invisible"
47-
></div>
48-
49-
<script nonce="{{ csp_nonce('script') }}" src="https://www.google.com/recaptcha/api.js" async defer></script>
42+
{# @see layout.html.twig for onloadRecaptchaCallback #}
43+
<script nonce="{{ csp_nonce('script') }}" src="https://www.google.com/recaptcha/api.js?render=explicit&amp;onload=onloadRecaptchaCallback" async defer></script>
5044
{% endif %}
5145
{%- endblock %}

Diff for: templates/layout.html.twig

+25-23
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,33 @@
2424

2525
{% if requiresRecaptcha|default(false) or (not app.user and requires_recaptcha()) %}
2626
<script nonce="{{ csp_nonce('script') }}" src="https://www.google.com/recaptcha/api.js?render=explicit&amp;onload=onloadRecaptchaCallback" async defer></script>
27-
28-
<script nonce="{{ csp_nonce('script') }}">
29-
var onloadRecaptchaCallback = function() {
30-
const forms = [
31-
{submit: '_submit', form: 'page_login'},
32-
{submit: '_request_password_submit', form: 'request_password'},
33-
{submit: '_register_submit', form: 'register_form'},
34-
{submit: '_submit_mini', form: 'nav_login'},
35-
{submit: '_reset_password_submit', form: 'reset_password'},
36-
];
37-
38-
forms.forEach(({submit, form}) => {
39-
if (document.getElementById(submit)) {
40-
grecaptcha.render(submit, {
41-
'sitekey' : '{{ recaptcha_site_key }}',
42-
'callback' : function (token) {
43-
document.getElementById(form).submit();
44-
}
45-
});
46-
}
47-
});
48-
};
49-
</script>
5027
{% endif %}
5128

29+
<script nonce="{{ csp_nonce('script') }}">
30+
var onloadRecaptchaCallback = function() {
31+
const forms = [
32+
{submit: '_submit', form: 'page_login'},
33+
{submit: '_request_password_submit', form: 'request_password'},
34+
{submit: '_register_submit', form: 'register_form'},
35+
{submit: '_submit_mini', form: 'nav_login'},
36+
{submit: '_reset_password_submit', form: 'reset_password'},
37+
{submit: '_change_password_submit', form: 'change_password'},
38+
{submit: '_edit_profile_submit', form: 'edit_profile'},
39+
];
40+
41+
forms.forEach(({submit, form}) => {
42+
if (document.getElementById(submit)) {
43+
grecaptcha.render(submit, {
44+
'sitekey' : '{{ recaptcha_site_key }}',
45+
'callback' : function (token) {
46+
document.getElementById(form).submit();
47+
}
48+
});
49+
}
50+
});
51+
};
52+
</script>
53+
5254
{% block head_additions %}{% endblock %}
5355
</head>
5456

Diff for: templates/user/change_password.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% extends "user/layout.html.twig" %}
22

33
{% block user_content %}
4-
{{ form_start(form, { 'action': path('change_password'), 'attr': { 'class': 'change_password col-md-6' } }) }}
4+
{{ form_start(form, { 'action': path('change_password'), 'attr': { 'class': 'change_password col-md-6', 'id': 'change_password' } }) }}
55
{{ form_errors(form) }}
66

77
<div class="form-group clearfix">
@@ -24,6 +24,6 @@
2424

2525
{{ form_widget(form) }}
2626

27-
<input type="submit" class="btn btn-block btn-inverse btn-lg" value="Change password" />
27+
<input type="submit" class="btn btn-block btn-inverse btn-lg" value="Change password" id="_change_password_submit" />
2828
{{ form_end(form) }}
2929
{% endblock user_content %}

Diff for: templates/user/edit.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% extends "user/layout.html.twig" %}
22

33
{% block user_content %}
4-
{{ form_start(form, { 'action': path('edit_profile'), 'attr': { 'class': 'edit_profile col-md-6' } }) }}
4+
{{ form_start(form, { 'action': path('edit_profile'), 'attr': { 'class': 'edit_profile col-md-6', 'id': 'edit_profile' } }) }}
55
{{ form_errors(form) }}
66

77
<div class="form-group clearfix">
@@ -43,7 +43,7 @@
4343

4444
{{ form_rest(form) }}
4545

46-
<input type="submit" class="btn btn-block btn-success btn-lg" value="Update" />
46+
<input type="submit" class="btn btn-block btn-success btn-lg" value="Update" id="_edit_profile_submit" />
4747

4848
<hr>
4949

0 commit comments

Comments
 (0)