简体   繁体   English

带有回调的Rails reCAPTCHA

[英]Rails reCAPTCHA with callback

I am trying to integrate a recaptcha into my website and I wanted to add some client side validation. 我正在尝试将Recaptcha集成到我的网站中,我想添加一些客户端验证。

I chose to use a gem to include the recaptcha tags, but I wanted to trigger a function once the recaptcha is checked. 我选择使用gem来包含recaptcha标签,但我想在检查recaptcha后触发一个功能。

Looked through some google sites and found that a data-callback attribute with its value set to function name is all I need. 查看了一些Google网站,发现我需要一个data-callback属性,其值设置为函数名。

I used recaptcha_tags helper from the gem to set it up and then a jquery method to add this data-callback attribute as I have not seed an option to do this inside the gem. 我使用了gem中的recaptcha_tags辅助程序进行设置,然后使用jquery方法添加了此data-callback属性,因为我没有在gem中植入执行此操作的选项。

$('.g-recaptcha').attr("data-callback", "myFunctionName");

After clicking the recaptcha the function is not called. 单击Recaptcha后,不会调用该函数。 Why? 为什么?

I asume you have a form like this 我想你有这样的形式

app/views/contacts/contact.html.erb 应用程序/视图/联系人/ contact.html.erb

<%= form_for @contact do |f| %>
  <%= f.text_field :nombre, :placeholder => I18n.t("contacto_formulario_nombre"),:required => true %>
  <%= f.text_field :apellido :placeholder => I18n.t("contacto_formulario_apellidos"), :required => true %>
  <%= f.text_field :email, :placeholder => I18n.t("contacto_formulario_email"), :required => true %>
  /** (....other tags ....) **/
  <%= recaptcha_tags :display => 'clean' %>
  <%= f.button I18n.t("contacto_formulario_continuar"), type: "submit" %>
 <% end %>

app/assets/javascripts/contact.js 应用程序/资产/ JavaScript的/ contact.js

$(function() {
  $('.g-recaptcha').attr("data-callback", "recaptcha");
})
function recaptcha()
{
  console.log("captcha pulsado!");
  /** the actions you want, i.e. activate submit button **/
}

The magic ocurrs in the attribute "data-callback" 神奇之处在于属性“数据回调”

Hope it helps. 希望能帮助到你。

Marino 马里诺

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM