GithubHelp home page GithubHelp logo

Comments (6)

oriolgual avatar oriolgual commented on July 17, 2024

The problem is that Time.now is evaluated when the class is loaded. You probably want to do this:

  validates :date_completed, :date => {
    :before => Proc.new { Time.now }, :message => 'must be before today'
  }

As noted at the readme :)

from date_validator.

socketwiz avatar socketwiz commented on July 17, 2024

Yeah, I tried that before posting, but it doesn't work the way I would expect. When using Proc.new, I get a validation error, that says that I need to choose a date before today, even when choosing a date before today. In fact it doesn't matter what day I choose when using Proc.new, today, tomorrow, yesterday, it just keeps telling me to choose a date before today. The date I am passing in is of the format: 2011-06-01, would that be the problem? I tried 06-01-2011 but that didn't seem to matter either. I'm sure its something silly I'm doing wrong, just not sure what :(

None of these seem to work:

validates :date_completed, :date => {
    :before => Time.now, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before => Date.today, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before => Proc.new {Time.now}, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before => Proc.new{Date.today}, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before_or_equal_to => Proc.new {Time.now}, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before_or_equal_to => Proc.new{Date.today}, :message => 'must be before today'
  }

from date_validator.

oriolgual avatar oriolgual commented on July 17, 2024

date_validator doesn't really care about the format, as long as its a Date, Time or DateTime. Are you sure you are passing a date object instead of a string?

from date_validator.

socketwiz avatar socketwiz commented on July 17, 2024

Are you sure you are passing a date object instead of a string?

Hmm, I am pretty sure it comes through as a string. I'm just passing it through my form:

  <div class="field">
    <strong><label>Date Completed</label></strong>
    <%= f.text_field :completed_on, :class => "inputbox" %>
  </div>

action:

  def create
    @project.save
    respond_with(@project)
  end

I'm going to have to research how to make it a date object. Off the top of my head I'm thinking you have to open up the params array prior to a save or update in the action? Does that sound right? Or maybe there is a date_field that you use in the view?

from date_validator.

oriolgual avatar oriolgual commented on July 17, 2024

Yes, you should you a date_input or time_input, this way Rails will autoconvert the value to a Date object. Here are some links to help you:

http://guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpers
https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/date_helper.rb#L138

from date_validator.

socketwiz avatar socketwiz commented on July 17, 2024

I see, well thank you for your help. I don't think I am going to be able to use this gem because I also want to use the jQuery datepicker: http://jqueryui.com/demos/datepicker/

But thank you for your time.

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.