Coder Social home page Coder Social logo

Comments (11)

AxisOfEval avatar AxisOfEval commented on July 26, 2024

I can't imagine a use case for this. The primary function is to stop robots from submitting forms. For that, one captcha per page is good enough.

from recaptcha.

Gee-Bee avatar Gee-Bee commented on July 26, 2024

Actually I had similar problem: on one page I had two forms (one to send email to friend recommending current page, other to send us opinion about page). Both forms should have been secured from bots.
The problem is recaptcha don't play well in such scenario. The workaround is to display recaptcha just before submitting the form.

My solution
Add recaptcha_tags wrapper which display recaptcha only if current form is submitted (additionaly check if current form object has any errors - in such case also delay displaying recaptcha).
In application_helper.rb:

def recaptcha_tags(options = {})
    form = options.delete(:form)
    form_path = form ? form.options[:url] : options.delete(:path)
    form_object = form ? form.object : options.delete(:object)
    raise 'Form (Form builder object) or path (form path) option for recaptcha_tags is required' unless form || form_path
    options[:display] ||= {}
    options[:display][:lang] ||= I18n.locale.to_s
    this_form_sent = request.method != 'GET' && form_path == request.path
    form_without_errors = form_object.nil? || begin
      errors = form_object.errors.dup
      errors.delete(:base); errors.delete(:recaptcha)
      errors.empty?
    end
    content = if(this_form_sent && form_without_errors)
      warning = if form_object && form_object.errors.include?(:recaptcha)
        content_tag(:p, content_tag(:strong, form_object.errors[:recaptcha].first), class: 'text-danger')
      else ''.html_safe end
      super + warning
    else ''.html_safe end
    content_tag(:div, content, class: 'recaptcha')
  end

You have to pass :form (form builder object) or :path (form path) options to properly identify current form.

In application_controller.rb:

def verify_recaptcha options={}
    if params[:recaptcha_response_field].blank? && options[:model]
      attribute = options[:attribute] || :base
      options[:model].errors.add attribute, I18n.translate('recaptcha.errors.empty_response') || 'Enter text from image'
      false
    else
      super
    end
  end

As a extra step you can add this coffescript snippet to remove recaptcha from other forms (if current form has recaptcha, and recaptcha is also present on other form(s)) - as I said recaptcha doesn't play well when put in multiple places on site:

$ ->
  $(document).on 'submit', 'form', ->
    $('form').not(this).find('.recaptcha').remove()
    return true

from recaptcha.

bfcoder avatar bfcoder commented on July 26, 2024

If you use your imagination like @Gee-Bee has, then there are certainly use cases for multiple reCAPTCHA on a page.

Google has docs on how to do this with v2. https://developers.google.com/recaptcha/docs/display So there is a little manual work you need to do because it isn't part of this gem as of yet.

I followed their example and used 'sitekey' : "<%= Recaptcha.configuration.public_key %>", in the erb for the js.

Maybe @ambethia will integrate the ability to do this at some point. But for now I'm fine doing it mostly manually.

from recaptcha.

ambethia avatar ambethia commented on July 26, 2024

Having an example of this I the documentation would be awesome. 


Jason L Perry

On Mon, Jul 6, 2015 at 5:13 PM, James Carbine [email protected]
wrote:

If you use your imagination like @Gee-Bee has, then there are certainly use cases for multiple reCAPTCHA on a page.
Google has docs on how to do this with v2. https://developers.google.com/recaptcha/docs/display So there is a little manual work you need to do because it isn't part of this gem as of yet.
I followed their example and used 'sitekey' : "<%= Recaptcha.configuration.public_key %>", in the erb for the js.

Maybe @ambethia will integrate the ability to do this at some point. But for now I'm fine doing it mostly manually.

Reply to this email directly or view it on GitHub:
#79 (comment)

from recaptcha.

bfcoder avatar bfcoder commented on July 26, 2024

@ambethia I submitted a PR with the example. Since you have not accepted it, is it not what you were wanting/thinking?

from recaptcha.

pandu-49-zz avatar pandu-49-zz commented on July 26, 2024

@bfcoder Your solution may work perfectly. Is there any solution with gem helper functions?

from recaptcha.

ambethia avatar ambethia commented on July 26, 2024

@bfcoder I'm showing that PR as merged?

from recaptcha.

bfcoder avatar bfcoder commented on July 26, 2024

@ambethia It was merged, but subsequently removed in #146 Not sure why @grosser decided it was not needed anymore.

from recaptcha.

grosser avatar grosser commented on July 26, 2024

moved it to the wiki since it's an edge-case usecase that not everybody
needs to read about ... still linked in the readme

https://github.com/ambethia/recaptcha/wiki/Add-multiple-widgets-to-the-same-page

On Tue, Nov 15, 2016 at 6:33 PM, James Carbine [email protected]
wrote:

@ambethia https://github.com/ambethia It was merged, but subsequently
removed in #146 #146 Not sure
why @grosser https://github.com/grosser decided it was not needed
anymore.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#79 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAsZyH7REeg7GOYn-EyAKCr_lw1z8HOks5q-mtqgaJpZM4AYN8K
.

from recaptcha.

bfcoder avatar bfcoder commented on July 26, 2024

ahh, I see. Thanks for clarifying.

from recaptcha.

ambethia avatar ambethia commented on July 26, 2024

Awesome, thanks Michael!

On Wed, Nov 16, 2016, 12:05 AM James Carbine [email protected]
wrote:

ahh, I see. Thanks for clarifying.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#79 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAABKgs1fRzulX0AyX3KQtufTZyLiqwEks5q-o8YgaJpZM4AYN8K
.


Jason L Perry

from recaptcha.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.