0

A user submits a form and all of the information is valid.

I just discoverd that when they click the back button the following validation gets triggered.

validates_format_of :email,
             :with => /^[A-Z0-9._%-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i,
             :message => "should be something like youremail@something.com"

Why is this happening and how can I stop it from being triggered?

chell
  • 7,646
  • 16
  • 74
  • 140

1 Answers1

1

If the users re-submit the form - which the browser warns them about and which always happens when you go BACK to a POST-request in a browser - there is nothing you can do. It's how browsers work. Besides, just for clarity of language, it's not the "going back" that triggers a validation. It's the operation on the model you specified the validation for, probably before_save. So there is a model.save operation going on, and that's likely triggered (don't know YOUR code, you could write @somemodel.save anywhere in your controller, but it's standard) by that repeated POST operation. Which comes from the user, in the end. one should not go "back" to a form that was posted, it's the same all over the Internet regardless of backend technology.

Mörre
  • 5,699
  • 6
  • 38
  • 63
  • Why? That would mean to stop people from going "back" whenever they want. Even now they are warned when they do: "Do you want to resubmit the form?" If they say "yes", why do you want to prevent it? – Mörre Mar 10 '11 at 11:54
  • Actually, I think my answer is incomplete. It also depends on the headers with caching information, possibly, how the original form was sent. I just tried with my own pages and could go back and forth with no problems even after posting. This may be an issue especially/only with login - since you get a new session and the old one is invalidated. – Mörre Mar 10 '11 at 12:00
  • Also see http://stackoverflow.com/questions/2175285/what-happens-when-i-press-browser-back-button Different backend technology - does not matter, same browser issue. – Mörre Mar 10 '11 at 12:04
  • Also see http://www.google.com/support/forum/p/Chrome/thread?tid=1f06a1af8a83f86f&hl=en As I had said, a cache control issue, but maybe a better (longer) description there. This cache issue is mixed up with different behavior in various browsers - I hadn't known (or ever thought about) THAT. Thanks for bringing up something as an issue which I had learned to take for granted (and therefore never thought about) :) – Mörre Mar 10 '11 at 12:10