laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Sep-07 22:42 UTC
HOw do I perform this conditional validation?
Hi,
I have this validation rule to check if the user-entered date of birth
is at least 18 years of age ...
validates_each :date_of_birth do |record, attr, value|
record.errors.add attr, "You must be at least 18 years old to
use this site." if value > Date.new((Date.today.year - 18),
(Date.today.month),(Date.today.day))
end
Now I want to refine this so it validation only occurs if this
condition above is true and the user_type_id virtual attribute is
equal to one. How do I add this additional piece of logic into my
validation rule?
Thanks, - Dave
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Sep 7, 11:42 pm, "laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org" <laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org> wrote:> Hi, > > I have this validation rule to check if the user-entered date of birth > is at least 18 years of age ... > > validates_each :date_of_birth do |record, attr, value| > record.errors.add attr, "You must be at least 18 years old to > use this site." if value > Date.new((Date.today.year - 18), > (Date.today.month),(Date.today.day)) > end > > Now I want to refine this so it validation only occurs if this > condition above is true and the user_type_id virtual attribute is > equal to one. How do I add this additional piece of logic into my > validation rule?Just add record.user_type_id == 1 to that condition. Fred> > Thanks, - Dave--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Sep-08 14:05 UTC
Re: HOw do I perform this conditional validation?
I guess I''m slow. How would that look with the code above? - On Sep 8, 1:23 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 7, 11:42 pm, "laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org" > > > > > > <laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org> wrote: > > Hi, > > > I have this validation rule to check if the user-entered date of birth > > is at least 18 years of age ... > > > validates_each :date_of_birth do |record, attr, value| > > record.errors.add attr, "You must be at least 18 years old to > > use this site." if value > Date.new((Date.today.year - 18), > > (Date.today.month),(Date.today.day)) > > end > > > Now I want to refine this so it validation only occurs if this > > condition above is true and the user_type_id virtual attribute is > > equal to one. How do I add this additional piece of logic into my > > validation rule? > > Just add record.user_type_id == 1 to that condition. > > Fred > > > > > > > Thanks, - Dave- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8 Sep 2008, at 15:05, laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org wrote:> > I guess I''m slow. How would that look with the code above? - >just add it to the if you''ve got. Fred> On Sep 8, 1:23 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On Sep 7, 11:42 pm, "laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org" >> >> >> >> >> >> <laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org> wrote: >>> Hi, >> >>> I have this validation rule to check if the user-entered date of >>> birth >>> is at least 18 years of age ... >> >>> validates_each :date_of_birth do |record, attr, value| >>> record.errors.add attr, "You must be at least 18 years old >>> to >>> use this site." if value > Date.new((Date.today.year - 18), >>> (Date.today.month),(Date.today.day)) >>> end >> >>> Now I want to refine this so it validation only occurs if this >>> condition above is true and the user_type_id virtual attribute is >>> equal to one. How do I add this additional piece of logic into my >>> validation rule? >> >> Just add record.user_type_id == 1 to that condition. >> >> Fred >> >> >> >> >> >>> Thanks, - Dave- Hide quoted text - >> >> - Show quoted text -- Hide quoted text - >> >> - Show quoted text - > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Sep-08 19:49 UTC
Re: HOw do I perform this conditional validation?
Thanks. I think I''m still doing something wrong because I now have
validates_each :date_of_birth do |record, attr, value|
record.errors.add attr, "You must be at least 18 years old to
use this site." if :user_type_id != 1 && value >
Date.new((Date.today.year - 18),(Date.today.month),(Date.today.day))
end
but I''m getting an error upon trying to create my object. The error
is
NoMethodError in Super admin/subscriberController#create
You have a nil object when you didn''t expect it!
The error occurred while evaluating nil.>
app/models/user.rb:10
app/controllers/super_admin/subscriber_controller.rb:43:in `create''
Request
Parameters:
{"user"=>{"pharmacy_id"=>"333",
"work_phone_extension"=>"",
"credit_card_number"=>"",
"user_type_id"=>"1",
"ship_to_last_name"=>"Alvarado",
"ship_to_city"=>"Denver",
"credit_card_expiration_date(1i)"=>"2008",
"credit_card_expiration_date(2i)"=>"9",
"ship_to_country"=>"US",
"password_confirmation"=>"password",
"credit_card_security_code"=>"",
"ship_to_street"=>"1201 Galapago ",
"ship_to_zip"=>"80204",
"credit_card_expiration_date(3i)"=>"8",
"bill_to_city"=>"",
"credit_card_company_id"=>"1",
"phone"=>"512-222-3333",
"fax"=>"",
"bill_to_last_name"=>"",
"bill_to_street"=>"",
"ship_to_first_name"=>"Dave",
"ship_to_state"=>"CO",
"login"=>"bigpharma",
"password"=>"password",
"bill_to_state"=>"",
"ship_to_company"=>"Dave Pharm",
"email"=>"laredotornado-FKu6XEelbF7QT0dZR+AlfA@public.gmane.org",
"bill_to_first_name"=>"",
"bill_to_zip"=>""},
"commit"=>"Create"}
On Sep 8, 8:19 am, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 8 Sep 2008, at 15:05,
laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org wrote:
>
>
>
> > I guess I''m slow. How would that look with the code above? -
>
> just add it to the if you''ve got.
>
> Fred
>
>
>
> > On Sep 8, 1:23 am, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > wrote:
> >> On Sep 7, 11:42 pm,
"laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org"
>
> >> <laredotorn...-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org>
wrote:
> >>> Hi,
>
> >>> I have this validation rule to check if the user-entered date
of
> >>> birth
> >>> is at least 18 years of age ...
>
> >>> validates_each :date_of_birth do |record, attr, value|
> >>> record.errors.add attr, "You must be at least 18
years old
> >>> to
> >>> use this site." if value > Date.new((Date.today.year -
18),
> >>> (Date.today.month),(Date.today.day))
> >>> end
>
> >>> Now I want to refine this so it validation only occurs if this
> >>> condition above is true and the user_type_id virtual attribute
is
> >>> equal to one. How do I add this additional piece of logic
into my
> >>> validation rule?
>
> >> Just add record.user_type_id == 1 to that condition.
>
> >> Fred
>
> >>> Thanks, - Dave- Hide quoted text -
>
> >> - Show quoted text -- Hide quoted text -
>
> >> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---