Forum: Ruby on Rails How To Validate Boolean Form Field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matthew F. (Guest)
on 2006-03-01 01:44
Hello all.

I've got the following in my model:

  class Individual < ActiveRecord::Base
    validates_presence_of :first_name, :last_name, :street, :city,
:state, :zip
    validates_acceptance_of :AZ_resident, :message => "You must be an
Arizona resident."
  end

The AZ_resident field is present and defined as boolean (PostgreSQL
8.1), but validation will not occur. I've tried adding :accept => "TRUE"
and variations on the theme, to no avail.

So, what am I doing wrong?  How do I validate a boolean form field?  It
may be of use to know that my view is standard scaffold-generated stuff
at the moment.

Thanks,

-ELf
Nithin R. (Guest)
on 2006-03-01 02:59
(Received via mailing list)
On 2/28/06, Matthew F. <removed_email_address@domain.invalid> wrote:
>
> The AZ_resident field is present and defined as boolean (PostgreSQL
> 8.1), but validation will not occur. I've tried adding :accept => "TRUE"
> and variations on the theme, to no avail.
>
> So, what am I doing wrong?  How do I validate a boolean form field?  It
> may be of use to know that my view is standard scaffold-generated stuff
> at the moment.
>
Have you tried this?

def validate
  errors.add(:AZ_resident, "You must be an Arizona resident") if
AZ_resident = false
end
Matthew F. (Guest)
on 2006-03-01 11:57
> Have you tried this?
>
> def validate
>   errors.add(:AZ_resident, "You must be an Arizona resident") if
> AZ_resident = false
> end

After correcting syntax errors, I tried:

  def validate
    errors.add(:AZ_resident, "You must be an Arizona resident") if
:AZ_resident == false
  end

No difference.  Forms with AZ_resident left FALSE are happily inserted
into the database; no errors raised.

But, moreover, what is wrong with my use of the validates_acceptance_of
method?  According to the docs found @
http://rubyonrails.org/api/classes/ActiveRecord/Va...,
I'm doing everything right.

Thanks folks,

-ELf
Nithin R. (Guest)
on 2006-03-01 21:41
(Received via mailing list)
To be able to validate a field, it must be present.

Try...
validates_presence_of :first_name, :last_name, :street, :city,
:state, :zip, :AZ_resident

- Nithin
Matthew F. (Guest)
on 2006-03-02 00:20
Nithin R. wrote:
> To be able to validate a field, it must be present.

Adding :AZ_resident to validates_presence_of throws a validation
exception no matter whether True or False is selected.  It's acting as
if there's no value in the field.

Is this normal behavior for a Boolean form field?

Thanks,

-ELf
Nithin R. (Guest)
on 2006-03-02 22:52
(Received via mailing list)
You got me curious about this, so I did some more investigating. It
seems that the behavior is broken, but you can fix it by doing
something like this:

class Individual < ActiveRecord::Base
  validates_acceptance_of :toc, :message => "You must accept this
before proceeding"

  def validate
    errors.add(:toc, "You must accept this before proceeding") if
self.toc.nil?
  end
end

HTH
Nithin R. (Guest)
on 2006-03-02 22:56
(Received via mailing list)
or even better...

validates_acceptance_of :toc, {:message => "You must accept this
before proceeding", :allow_nil => false}
This topic is locked and can not be replied to.