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
--
Posted via http://www.ruby-forum.com/.
On 2/28/06, Matthew Feadler <matthew@feadler.com> wrote:> 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. >Have you tried this? def validate errors.add(:AZ_resident, "You must be an Arizona resident") if AZ_resident = false end
> Have you tried this? > > def validate > errors.add(:AZ_resident, "You must be an Arizona resident") if > AZ_resident = false > endAfter 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/Validations/ClassMethods.html, I''m doing everything right. Thanks folks, -ELf -- Posted via http://www.ruby-forum.com/.
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 On 3/1/06, Matthew Feadler <matthew@feadler.com> wrote:> > 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/Validations/ClassMethods.html, > I''m doing everything right. > > Thanks folks, > > -ELf > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Nithin Reddy 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 -- Posted via http://www.ruby-forum.com/.
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
On 3/1/06, Matthew Feadler <matthew@feadler.com>
wrote:> Nithin Reddy 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
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
or even better...
validates_acceptance_of :toc, {:message => "You must accept this
before proceeding", :allow_nil => false}
On 3/2/06, Nithin Reddy <jashugan@gmail.com>
wrote:> 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
>
> On 3/1/06, Matthew Feadler <matthew@feadler.com> wrote:
> > Nithin Reddy 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
> >
> >
> > --
> > Posted via http://www.ruby-forum.com/.
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>