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