GithubHelp home page GithubHelp logo

Comments (5)

oriolgual avatar oriolgual commented on August 16, 2024

I'm not really sure I understand what you're trying to do. Just a quick note, have you tried something like:

validates :start_time, :date => {....} if start_time.present?

What would be an example value of start_time?

from date_validator.

amnesia7 avatar amnesia7 commented on August 16, 2024

I just want to validate that the value for start_time is actually a time, eg "10:30", and not something completely wrong like "random string". It can be any time during the day, I just want to make sure that it is actually a time.

from date_validator.

amnesia7 avatar amnesia7 commented on August 16, 2024

I've amended my model as you say, so it now contains:

validates :start_time,  :date => { :after_or_equal_to => Proc.new { Time.now.beginning_of_day }, 
                                :before_or_equal_to => Proc.new { Time.now.end_of_day },
                                :message => 'must be a valid time' }, 
                      :if => "start_time.present?"

and the test I had before trying to to post "random string" now fails correctly so that's good.

However, my app doesn't actually work at all now.

I use time_select to display 2 select boxes (hours, minutes) in my view. This new validation doesn't consider normal selections (eg 10 [hours] and 30 [minutes]) to be a valid time.

I've tried converting my view to use a text_field and tried typing "10:00" but that fails also.

Is it something to do with the way the time_select multiparameters thing works in rails?

Not sure if this helps or not but this is the gem I tried using previously (but it has a bug in it that prevents allow_nil/allow_blank from working correctly):

https://github.com/adzap/validates_timeliness/blob/master/lib/validates_timeliness/extensions/multiparameter_handler.rb

from date_validator.

amnesia7 avatar amnesia7 commented on August 16, 2024

I've been having a think about my issue and what it is that I'm actually trying to do.

I am currently trying to validate date and time individually when what I should be doing is coupling them together and validating them then.

My issue, therefore, is extremely similar to the following: http://stackoverflow.com/questions/6065860/trying-to-set-a-variable-in-before-validation-but-it-isnt-working (I'm also using a text field for date and dropdowns for the time). However, the SO user doesn't show their final resulting model content and I'm struggling to implement an equivalent based on the information on there.

Can you offer any advice?

When I add the following to make sure I'm implementing it correctly:

before_validation :construct_starting_at

validate :starting_at, :presence => true, :if => "start_date.present? && start_time.present?"

def construct_starting_at
  d = Time.parse(start_date)
  time = Time.parse(start_time)
  if start_date.present? && start_time.present?
    self.starting_at = Time.zone.parse("#{d.year}-#{d.month}-#{d.day} #{time.hour}:#{time.min}:00 UTC")
  end
end

but I just keep getting:

TypeError:
    can't dup NilClass

on the construct_starting_at line

and then the other tests fail saying:

NoMethodError:
    undefined method 'gsub!' for Mon, 16 Apr 2012:Date

also on the construct_starting_at line

from date_validator.

amnesia7 avatar amnesia7 commented on August 16, 2024

I'm still not having any joy with this. I'm using a text_field for the date which is in the format YYYY-MM-DD and then I use a time_select (using 2 select boxes) for the time.

I've opened a Question on Stack Overflow but no takers yet : http://stackoverflow.com/questions/9955977/validate-date-and-time-fields-together-in-rails-model/10152596

Any advice welcome.

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.