Date validation using pattern. Why?

Hello, I’m new to this forum and also I’m new to programming, to Ruby
and to Ruby on Rails. I’m from Russia and started learingn RoR with book
“Agile Web D. with Rails”. Right now I’m trying to practice in
building web applications using Ruby on Rails. So, question:

I’ve scaffolded DB with following structure:

create_table(:products) do |t|
t.column :title, :string
t.column :description, :text
t.column :imageurl, :string
t.column :price, :decimal, :default => 0, :precision => 8, :scale => 2
t.column :date, :date
end

Then I’m trying to validate input data; everything is great, but I can’t
validate input :date. I’m always getting error about :date. (Don’t ask
why I’m trying to validate date - I’m just trying to have some
practice.)

def validate
date_pattern = /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/
errors.add(:date, “Date must be formatted YYYY-MM-DD #{date.to_s}”) if
date.nil? || date_pattern =~ date.to_s
end

When I’m trying to make date_pattern =~ “2007-01-15” it’s ok; it is 0
but why I’m getting error when trying to validate :date?

Hope to hear from you soon - thanks!

P.S. Sorry for my english. I know it is awful. :slight_smile: But I hope it will be
clear for you.

Thanks!

You need !~, not =~. Note that 0 is NOT false, it is true, so

puts “true” if 0

will print “true”

Might I suggest instead using:

Date.parse(date)

instead? This will also make certain the dates are valid, and will
accept things like 07-10-10 as well. You will need to protect that
call inside of a begin … rescue … end block though since it will
raise an exception on a format / data error.

–Michael

Michael, Thanks. Got it!

There is no info about Date.parse() in my Ruby book.
Please, could you gimme URL where I can read about it.

I’m real noob and I need some practice in finding manuals etc. for
different classes and functions.

I don’t have enough bookmarks about Rails which I can refer to.

Thanks!

I’d start with: RDoc Documentation

Scroll down in the middle column, and select “Date” to display
information about that class.

–Michael

Michael, Thanks. Got it!

There is no info about Date.parse() in my Ruby book.
Please, could you gimme URL where I can read about it.

http://www.ruby-lang.org/en/documentation/

in particular…

http://www.ruby-doc.org/core/
http://www.ruby-doc.org/stdlib/

and specifically:

http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/index.html

(far right top frame, scroll to the parse method).

For myself, I keep a local copy of the above and then link directly to
the
method frame like this:

http://www.ruby-doc.org/core/fr_method_index.html

Then a search for ‘parse’ would find what I want without having all
those
little frames :slight_smile:

On 10/15/07, Michael G. [email protected] wrote:

instead? This will also make certain the dates are valid, and will
accept things like 07-10-10 as well. You will need to protect that
call inside of a begin … rescue … end block though since it will
raise an exception on a format / data error.

Or if he wants to use the regexp

validates_format_of :date :with => /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Oct 15, 3:42 pm, Kir I. [email protected]
wrote:

P.S. Sorry for my english. I know it is awful. :slight_smile: But I hope it will be
clear for you.

Thanks!


Posted viahttp://www.ruby-forum.com/.

FYI:

there’s a couple neat libraries for parsing/validating dates/times:

http://runt.rubyforge.org/
http://chronic.rubyforge.org/

and your English is fine!

Rick, thanks. I’ve noticed this method too at
http://www.ruby-forum.com/topic/127958#new

Michael, Philip, thanks for useful links! Thumbs up!

On Oct 16, 1:53 am, “Kirizoid B.” [email protected]
wrote:

Gene, thanks. Great links! Especially I liked “Chronic” with it’s great
features like Chronic.parse(‘this tuesday 5:00’) etc.

Very useful. I will bookmark it and will refer to this libraries every
time when I need to parse or validate dates/times.

Thanks.


Posted viahttp://www.ruby-forum.com/.

one of the basic principles of ruby/rails programming is to look for a
plugin, gem or lib (or maybe a pastie) of some sort for a well-defined
requirement before you spend 3 days and nights keying and mousing
away. People have thought of nearly everything .

Gene, thanks. Great links! Especially I liked “Chronic” with it’s great
features like Chronic.parse(‘this tuesday 5:00’) etc.

Very useful. I will bookmark it and will refer to this libraries every
time when I need to parse or validate dates/times.

Thanks.

This is making more sense now, my date field in my model is persisted
as a date in the database. From my understanding of all of your
posts, there is no way to validate the input because it is an instance
of a date, not an instance of a string. Therefore when ruby converts
the date to the ISO string, its not possible to get it into the format
I expect during the validation phase. Are there any formatting hooks
in RoR? In other words, can I register input and output formatters
(Similar to JSF in Java) so that dates aren’t displayed with the ISO
format? Since my users will mostly be in the US, I don’t want to
always have the format as yyyy-mm-dd. My users would much rather have
mm/dd/yyyy, so I would like to have that format as both the input and
output, then I can wrap it with a javascript calendar to make it the
input easy to use. I’ve tried Googling online for some documentation
outlining the request lifecycle of a RoR app, but I can’t seem to find
what I need.

Thanks,
Todd

On Oct 18, 5:00 pm, Todd N. [email protected] wrote:

mm/dd/yyyy, so I would like to have that format as both the input and

On Oct 16, 1:53 am, “Kirizoid B.” [email protected]


Posted viahttp://www.ruby-forum.com/.

one of the basic principles of ruby/rails programming is to look for a
plugin, gem or lib (or maybe a pastie) of some sort for a well-defined
requirement before you spend 3 days and nights keying and mousing
away. People have thought of nearly everything .

request lifecycle:
http://brainspl.at/request_response.pdf

strftime:
http://noobonrails.blogspot.com/2005/11/prim-and-proper-dates.html

On 10/18/07, Todd N. [email protected] wrote:

always have the format as yyyy-mm-dd. My users would much rather have
mm/dd/yyyy, so I would like to have that format as both the input and
output, then I can wrap it with a javascript calendar to make it the
input easy to use. I’ve tried Googling online for some documentation
outlining the request lifecycle of a RoR app, but I can’t seem to find
what I need.

I think that the reason dates are stored in the database in ISO format
is to enable date comparisons.

“2007-10-19” > “2006-12-08”

but

“10/19/2007” < “12/08/2006”

But if the column type is time, date or datetime, ActiveRecord will
convert to and from Ruby Time and Date objects when you read and write
the record. You can then format as you wish using the strftime
method.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

This may be of interest
http://www.railslodge.com/plugins/111-validates-date-time