validates_acceptance_of is skipped when the attribute is nil

Posted . Visible to the public.

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.

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.

Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-04-11 08:08)