Regular Expressions

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

Please help.

On Feb 24, 10:00 am, Pale H. [email protected] 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 :confused:

by the time rails validates the record it has already turned it into a
date.

Fred

On 24 February 2010 10:07, Pale H. [email protected] wrote:

Frederick C. 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 :slight_smile:

Frederick C. wrote:

by the time rails validates the record it has already turned it into a
date.

Fred

Thanks for your swift response. What exactly does this mean for my
validation, though?

Michael P. wrote:

On 24 February 2010 10:07, Pale H. [email protected] wrote:

Frederick C. 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 :slight_smile:

I just fail to see why my method wouldn’t work, but I will try - thank
you.

validate :method_for_date

def method_for_date
yours code format specifications
end

try something like this may be help full

On 24 February 2010 10:21, Pale H. [email protected] wrote:

Michael P. wrote:

try it and see :slight_smile:

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

Pale H. wrote:

Michael P. wrote:

On 24 February 2010 10:07, Pale H. [email protected] wrote:

Frederick C. 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 :slight_smile:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs