> validates_format_of :closing_date, :with => /^\d\d\-\d\d\-\d\d\d\d$/, :message => "is not correct format"Does this seem correct or is there a better way to do this? Basically, I have a closing_date column in my database which is set as a date field. On the front end form, I''ve used a text box for the user to enter the date. I''ve entered the data exactly as the regular expression above dictates but I can''t seem to make it pass that instance method :/ Please help. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 24, 10:00 am, Pale Horse <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > validates_format_of :closing_date, :with => /^\d\d\-\d\d\-\d\d\d\d$/, :message => "is not correct format" > > Does this seem correct or is there a better way to do this? Basically, I > have a closing_date column in my database which is set as a date field. > On the front end form, I''ve used a text box for the user to enter the > date. I''ve entered the data exactly as the regular expression above > dictates but I can''t seem to make it pass that instance method :/ >by the time rails validates the record it has already turned it into a date. Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Frederick Cheung wrote:> > by the time rails validates the record it has already turned it into a > date. > > FredThanks for your swift response. What exactly does this mean for my validation, though? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 24 February 2010 10:07, Pale Horse <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote: >> >> by the time rails validates the record it has already turned it into a >> date.> Thanks for your swift response. What exactly does this mean for my > validation, though?validates_presence_of would probably suffice. If the user entered an non-parseable value, the closing_date field will be nil. try it and see :-) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote:> On 24 February 2010 10:07, Pale Horse <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Frederick Cheung wrote: >>> >>> by the time rails validates the record it has already turned it into a >>> date. > >> Thanks for your swift response. What exactly does this mean for my >> validation, though? > > validates_presence_of would probably suffice. If the user entered an > non-parseable value, the closing_date field will be nil. > > try it and see :-)I just fail to see why my method wouldn''t work, but I will try - thank you. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
validate :method_for_date def method_for_date yours code format specifications end try something like this may be help full -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Pale Horse wrote:> Michael Pavling wrote: >> On 24 February 2010 10:07, Pale Horse <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> Frederick Cheung wrote: >>>> >>>> by the time rails validates the record it has already turned it into a >>>> date. >> >>> Thanks for your swift response. What exactly does this mean for my >>> validation, though? >> >> validates_presence_of would probably suffice. If the user entered an >> non-parseable value, the closing_date field will be nil. >> >> try it and see :-) > > I just fail to see why my method wouldn''t work, but I will try - thank > you.Sorry, I misread this. I do not want to check for the presence of the closing date, I just want to check the format of it against this: "30-02-2010". -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 24 February 2010 10:21, Pale Horse <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Michael Pavling wrote: >> try it and see :-) > > I just fail to see why my method wouldn''t work, but I will try - thank > you.You''re doing a regex evaluation on a Date (or DateTime) object... so it fails. Try these in IRB:>> Date.today =~ /10/>> Date.today.to_s =~ /10/By definition, the format of the Date converted to a string is defined by the to_s method, so it''s a tautology to change it to a string and check its format! By all means check that it''s in the future or past, or within whatever range you want... but it''s a DATE - so do date operations on it, not string operations. If you want to check what the user *typed* you need to get to the controller and do some validation on params[:closing_date] (or pass that value into an attr_writer in your model, and do model validations on that parameter...) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.