Forum: Ruby on Rails problems with validates_format_of

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.
7e78bd4dca5919ca41c295848931c957?d=identicon&s=25 Daniel Fac (solidregin)
on 2008-10-11 02:34
hi,
i'm trying to validate the format of a date and have the following
regular code:

validates_presence_of :dob
validates_format_of :dob,
:with => /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/,
:unless => Proc.new { |u| u.dob.blank? }

i've checked the regexp independently and it works, and i've put this
regexp with telephone, and it checks the dates correctly (telephone is a
integer type), however when i do it with dob it'll give me some strange
results:
23-12-2008 will be valid, while 23-13-2008 won't.  so i'm guessing it's
checking whether dates are valid completely independently from my
validations, however this isn't what i want.

am i missing something here?
thanks
daniel
7e78bd4dca5919ca41c295848931c957?d=identicon&s=25 Daniel Fac (solidregin)
on 2008-10-11 02:41
the error message i get if i write 23-13-2008 is "dob can't be blank"

Daniel Fac wrote:
> hi,
> i'm trying to validate the format of a date and have the following
> regular code:
>
> validates_presence_of :dob
> validates_format_of :dob,
> :with => /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/,
> :unless => Proc.new { |u| u.dob.blank? }
>
> i've checked the regexp independently and it works, and i've put this
> regexp with telephone, and it checks the dates correctly (telephone is a
> integer type), however when i do it with dob it'll give me some strange
>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-11 02:56
(Received via mailing list)
On Oct 11, 1:41 am, Daniel Fac <rails-mailing-l...@andreas-s.net>
wrote:
> the error message i get if i write 23-13-2008 is "dob can't be blank"
>
At the point the validation runs the string '23-13-2008' has been
turned into an actual date (ie an instance of Time or Date depending
on your column type), so validating its format is nonsensical.

Fred
7e78bd4dca5919ca41c295848931c957?d=identicon&s=25 Daniel Fac (solidregin)
on 2008-10-11 03:03
thank you I just noticed this as you wrote it :(
shit
i've been trying to fix this for four hours
damn ruby... :(
Frederick Cheung wrote:
> On Oct 11, 1:41�am, Daniel Fac <rails-mailing-l...@andreas-s.net>
> wrote:
>> the error message i get if i write 23-13-2008 is "dob can't be blank"
>>
> At the point the validation runs the string '23-13-2008' has been
> turned into an actual date (ie an instance of Time or Date depending
> on your column type), so validating its format is nonsensical.
>
> Fred
This topic is locked and can not be replied to.