Open CeeBeeUK opened 9 years ago
Yup - I've been bitten by that one. The solution is to use
date: { allow_blank: true }
So the date validation won't get run if date is not present.
I have something like
validates :effective_to, date: { allow_blank: true, after: :effective_from }
It complains if effective_to is blank :( ["Effective to is not a date"]
@mlt Here's a section of code involved, which suggests that your code above should work:
A blank
string can be many things: nil, empty string, string containing only whitespace, false - which is it in your scenario?
My bad! I was messing around with parameter coercion substitution that I want to treat blank as infinite. This fragment indeed does work! However, there is another problem. It doesn't respect DateTime::Infinity
that was coming from my strong params filter. I forgot I added
p[:effective_to] = DateTime::Infinity if p.fetch(:effective_to, '').blank?
Note, that
validates :effective_to, date: { after: :effective_from }, unless: Proc.new{ |p| p.effective_to.blank? }
worked fine with blank (actually infinite) parameter. It seems to me they should be equivalent.
Hello,
I'm trying to set up date validation on a field that requires a valid date be input.
validates :dob, date: true, presence: true
renders both theblank
andnot_a_date
messages.I am working on a fork that would change the functionality so that passing:
nil
tovalidates :dob, date: true
wouldreturn en.errors.messages.blank
'bob'
tovalidates :dob, date: true
wouldreturn en.errors.messages.not_a_date
Is this something that would be welcomed as a pull request or should I keep it separate?