date validation how to chech ArgumentError

I wrote in my user model :

  def validate
    errors.add(:born_on, "is invalid") if Proc.new { |user|
DateTime.parse(user.born_on) }
   end

it's wrong I know, running in the console raises an ArgumentError

Loading development environment (Rails 2.3.2)

born_on = "99/99/9999"

=> "99/99/9999"

DateTime.parse(born_on)

ArgumentError: invalid date
  from /usr/local/lib/ruby/1.8/date.rb:1573:in `new_by_frags'
  from /usr/local/lib/ruby/1.8/date.rb:1618:in `parse'
  from (irb):2

how can I write my validation Proc ?

thanks for your help

erwin

I wrote in my user model :

def validate

errors.add(:born_on, "is invalid") if Proc.new { |user|

DateTime.parse(user.born_on) }

end

Try out this

def validate
DateTime.parse(self.born_on)

 rescue  
errors.add(:born_on, "is invalid")

end

it’s wrong I know, running in the console raises an ArgumentError

Loading development environment (Rails 2.3.2)

born_on = “99/99/9999”

=> “99/99/9999”

DateTime.parse(born_on)

ArgumentError: invalid date

    from /usr/local/lib/ruby/1.8/date.rb:1573:in `new_by_frags'

    from /usr/local/lib/ruby/1.8/date.rb:1618:in `parse'

    from (irb):2

how can I write my validation Proc ?

thanks for your help

erwin

  • Sandip R~

thanks... I was looking on one direction only.... better 2
eyes ;-))