GithubHelp home page GithubHelp logo

Comments (4)

dgmstuart avatar dgmstuart commented on August 16, 2024

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.

from date_validator.

mlt avatar mlt commented on August 16, 2024

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

from date_validator.

dgmstuart avatar dgmstuart commented on August 16, 2024

@mlt Here's a section of code involved, which suggests that your code above should work:

def validate_each(record, attr_name, value)
before_type_cast = :"#{attr_name}_before_type_cast"
value_before_type_cast = if record.respond_to?(before_type_cast)
record.send(before_type_cast)
else
nil
end
if value_before_type_cast.present? && value.nil?
record.errors.add(attr_name, :not_a_date, options)
return
end
return if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
unless value
record.errors.add(attr_name, :not_a_date, options)
return
end

A blank string can be many things: nil, empty string, string containing only whitespace, false - which is it in your scenario?

from date_validator.

mlt avatar mlt commented on August 16, 2024

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.

from date_validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.