0

I want to have my form forward to recaptcha but only after form has passed all validation. How would I achieve this before users details are saved to DB?

def create
      @user = User.new(params[:user])     
      respond_to do |format|
      if @user.save

        #to recaptcha, but before save and only after sign up form passes validation
      else

      format.html { render :new }  
      format.js   { render :form_errors }
      end
    end
  end
LondonGuy
  • 10,778
  • 11
  • 79
  • 151
  • it would be better to have the recaptcha inside the form, and then say that the form is only valid if the recaptcha is filled in correctly – stephenmurdoch Oct 26 '11 at 20:38
  • for design purposes I won't be showing recaptcha on same page.. well ajax will fade it in.. – LondonGuy Oct 26 '11 at 20:40
  • Hmm, personally I think you should hide the form's **submit** button along with the CAPTCHA, and have a ` – stephenmurdoch Oct 26 '11 at 20:48

3 Answers3

1

Validation lives in the model, you could simply do this in the controller:

@user.valid?

and then do your recaptcha stuff.

Another solution is to use callbacks such as: before_save or before_create but only if recaptcha could be accessed in model (which I doubt).

apneadiving
  • 114,565
  • 26
  • 219
  • 213
1

This Railscast has all you need to know about multistep forms. The episode covers validation and moving back and forth between steps. http://railscasts.com/episodes/217-multistep-forms

It sounds like your form has two steps, the first being where they enter in all their information, and the second being just a captcha entry.

Now, in my opinion you should just roll the captcha into the main user entry form and keep it all to a single page rather than having a two step process, I've done both before and having the captcha be part of the same form is much, much easier and less complex. Having everything in a single form allows you to have all of your logic consolidated (mostly) into a single controller action. There may be logic you can abstract out of the controller into a helper method, like the verification of the captcha, which will make your controller action that much less complicated. The last thing you want to do is over-complicate your action logic.

Lester Peabody
  • 1,868
  • 3
  • 20
  • 42
1

Have a good look at The definitive guide to form-based website authentication and ask yourself if you really need a captcha.

Besides that, you can use Callbacks :after_validation, before_save, around_save, after_save, before_create, around_create, after_create, before_update, around_update, after_update to handle stuff still inside your transaction.

The way to call one of these callbacks is to simply declare them in your model

If you need to use a captcha however, I would do this with javascript and ajax, to append it to your form before the user sends it.

You should not do this in the controller after recieving a post of the form, since you will have to:

  1. Store the filled form values in the session after validation (dont save)
  2. Redirect the user to a captcha page (which will make any user confused)
  3. Check the captcha multiple times before it passes (they are quite unreadable)
  4. Get the model out of the session (which you have no idea of which one it is)
  5. Call save on the model to actually write it to your DB.

So basically you avoid starting a transaction before the captcha is passed.

Community
  • 1
  • 1
Ben
  • 13,297
  • 4
  • 47
  • 68