Simple Captcha and Validatable

I need a captcha in a form, whose associated model is a Validatable. After googling, I decided to go with Simple Captcha. The Simple captcha is extremely easy to use, see its documentation, you’ll know. But it works fine with a ActiveRecord::Base, not a Validatable. There has to be some hack to make it work. I could use “Controller mode” of it. But it will be nice to put it into model, make it part of validation process. and here’s my code:

First, include SimpleCaptcha into your Validatable model:

YourValidatable.module_eval do
    class << self; include SimpleCaptcha::ModelHelpers; end

<br /><br />And overwrite <i>apply_simple_captcha</i><br /><br />

  def self.apply_simple_captcha(options = {})
    module_eval do
      require 'pstore'
      include SimpleCaptcha::ConfigTasks
      attr_accessor :captcha, :captcha_code,
      alias_method :valid_without_captcha?, :valid?
    @captcha_invalid_message =
 (options[:message].nil? || options[:message].empty?) ?
  " image did not match with text" : options[:message]

Finally, call apply_simple_captcha

That’s it.

In the controller, instead of calling valid? to decide if the object is valid, you call valid_with_captcha?

BTW: Follow this link to see how to install RMagick on Ubuntu

~ by Yi Wen on September 10, 2007.

2 Responses to “Simple Captcha and Validatable”

  1. Ugly side of CAPTCHA…

    I’ve mentioned this issue before in the post looks like since that day and captcha have memorized my ip and since that day I’ve been facing weird and strange forms of captcha . for example like when you post comment in some bl…

  2. test

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: