Forum: Ruby on Rails date validation how to chech ArgumentError

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2009-06-08 19:31
(Received via mailing list)
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
4ab2692ba129cb296d4f2e90ba5241da?d=identicon&s=25 Sandip Ransing (san2821)
on 2009-06-09 07:55
(Received via mailing list)
On Mon, Jun 8, 2009 at 10:59 PM, Erwin <yves_dufour@mac.com> wrote:

>
> 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


> >>
>
> how can I write my validation Proc ?
>
> thanks for your help
>
> erwin
> >
>

- Sandip R~
--
Ruby on Rails Developer
http://sandip.sosblog.com
http://funonrails.wordpress.com
www.joshsoftware.com
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2009-06-09 08:41
(Received via mailing list)
thanks...    I was looking on one direction only....  better 2
eyes ;-))
This topic is locked and can not be replied to.