Read more

validates_acceptance_of is skipped when the attribute is nil

Henning Koch
April 11, 2012Software engineer at makandra GmbH

validates_acceptance_of :terms Show archive.org snapshot only works if terms is set to a value. The validation is skipped silently when terms is nil.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

While this behavior is useful to validate acceptance in the frontend and not the admin backend, it also makes it very easy to unintentionally skip the validation altogether by forgetting to add the checkbox to a form. E.g. validates_acceptance_of :terms_with_typo will be skipped silently even if there is no column with that name (a virtual attribute terms_with_typo will be defined automatically).

In order to be safe, a Cucumber scenario should ensure that the box needs to be checked. Or you can use an actual boolean field for terms and validate acceptance using validates_presence_of :terms.

Posted by Henning Koch to makandra dev (2012-04-11 10:08)