Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Validations of acceptance not working

#5501

Hi,
i have model like

class User < ActiveRecord::Base
  attr_accessible :username, :email, :password, :password_confirmation, :remember_me
  attr_accessor :terms

  validates :username,
            :presence => true,
            :format => { :with => /\A[a-z0-9\-]+\z/i },
            :uniqueness => true,
            :length => 3..20

  validates :email,
            :presence => true,
            :format => { :with => /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i },
            :uniqueness => true

  validates :password,
            :presence => true,
            :confirmation => true,
            :length => 6..20

  validates :terms,
            :acceptance => true

end

Everything is ok, but validation of :terms does not working.

Reported by Alexey Poimtsev · August 30th, 2010 @ 08:33 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Alexey Poimtsev
    Alexey Poimtsev
    • Tag changed from validates_acceptance_of rails3, validations to validations

    August 30th, 2010 @ 08:34 PM

  2. Raony vieira
    Raony vieira

    I've go a bit similar problem, my is not validating my collection_select.
    Does someone have a clue about that?
    cheers

    August 30th, 2010 @ 08:58 PM

  3. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    by default nil is allowed. You need to do

    validates :terms,
                :acceptance => true, :allow_nil => false
    

    I do agree that :allow_nil should be 'false' be default. That would make more sense. I will talk to rails core team member and if agree will change the default value.

    August 30th, 2010 @ 10:04 PM

  4. José Valim
    José Valim
    • State changed from new to invalid

    :allow_nil should be true. Because it allows you to create users through the console/tests without having to specify :terms => true, so it is a convenience.

    This won't be a problem in your views, because once you do: f.checkbox :terms, it will never be nil because it would already default to false.

    August 31st, 2010 @ 04:56 AM