Hi! These days I have been having trouble with a test that tried to test DateTime functionality. I have discovered that a NULL DateTime is auto-type casted to NIL by Rails. My problem here is that I have a field :datetime and I want to allow NULL datetimes but not wrong datetimes. With this validation if deadline is wrong or if is is blank it returns true. How I can accomplish this? protected def validate if deadline.to_s.blank? true else begin dt = DateTime.parse(deadline.to_s) rescue ArgumentError errors.add(:deadline, "Deadline has invalid syntax") end end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 3/20/08, Florencio Cano <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi! > These days I have been having trouble with a test that tried to test > DateTime functionality. I have discovered that a NULL DateTime is > auto-type casted to NIL by Rails. > My problem here is that I have a field :datetime and I want to allow > NULL datetimes but not wrong datetimes. With this validation if deadline > is wrong or if is is blank it returns true. How I can accomplish this? > > protected > def validate > if deadline.to_s.blank? > true > else > begin > dt = DateTime.parse(deadline.to_s) > rescue ArgumentError > errors.add(:deadline, "Deadline has invalid syntax") > end > end > enddef validate deadline_str = deadline_before_typecast unless deadline_str.blank? begin DateTime.parse(deadline_str) rescue ArgumentError errors.add(:deadline, "Deadline has invalid syntax") end end end Note that the return value of the validate method isn''t used, what counts is whether the model has any errors after all of the validations have been run. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You could also attack it without overriding validate: validate_each :deadline, :if=>lambda{|record| ! record.deadline_before_typecast.blank?} do |record, attr, value| DateTime.parse(deadline_before_typecast) rescue ArgumentError errors.add(:deadline, "has invalid date syntax") end end On Mar 20, 7:41 am, "Rick DeNatale" <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 3/20/08, Florencio Cano <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > > Hi! > > These days I have been having trouble with a test that tried to test > > DateTime functionality. I have discovered that a NULL DateTime is > > auto-type casted to NIL by Rails. > > My problem here is that I have a field :datetime and I want to allow > > NULL datetimes but not wrong datetimes. With this validation if deadline > > is wrong or if is is blank it returns true. How I can accomplish this? > > > protected > > def validate > > if deadline.to_s.blank? > > true > > else > > begin > > dt = DateTime.parse(deadline.to_s) > > rescue ArgumentError > > errors.add(:deadline, "Deadline has invalid syntax") > > end > > end > > end > > def validate > deadline_str = deadline_before_typecast > unless deadline_str.blank? > begin > DateTime.parse(deadline_str) > rescue ArgumentError > errors.add(:deadline, "Deadline has invalid syntax") > end > end > end > > Note that the return value of the validate method isn''t used, what > counts is whether the model has any errors after all of the > validations have been run. > > -- > Rick DeNatale > > My blog on Rubyhttp://talklikeaduck.denhaven2.com/--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rick Denatale wrote:> def validate > deadline_str = deadline_before_typecast > unless deadline_str.blank? > begin > DateTime.parse(deadline_str) > rescue ArgumentError > errors.add(:deadline, "Deadline has invalid syntax") > end > end > endHow can I access to the variable before the typecast? At the validate method beginning I think the variable is already typecasted and converted to nil. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 Mar 20, 2008, at 1:00 PM, Florencio Cano wrote:> Rick Denatale wrote: >> def validate >> deadline_str = deadline_before_typecast >> unless deadline_str.blank? >> begin >> DateTime.parse(deadline_str) >> rescue ArgumentError >> errors.add(:deadline, "Deadline has invalid syntax") >> end >> end >> end > > How can I access to the variable before the typecast? At the validate > method beginning I think the variable is already typecasted and > converted to nil.Uh, just like Rick said. deadline_before_typecast give you the attribute "deadline" in its original format (which is often a string). -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---