Comments (5)
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.
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.
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):
from date_validator.
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.
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)
- Wrong type of error in different cases HOT 2
- time range HOT 1
- Update activemodel version dependency HOT 1
- Add way to check is a string is a valid date HOT 5
- Complain of missing translation HOT 7
- Fail to detect errors when invalid strings are provided HOT 1
- Not working in certain time zones HOT 3
- Differentiating between `blank` and `not_a_date` HOT 4
- I18n conflicts with Activemodel I18n HOT 4
- v0.9.0 introduced a side effect about I18n.locale_available? HOT 5
- URL out of date in repository description HOT 1
- Rails 5 HOT 2
- Not really ORM-Agnostic HOT 2
- Is this gem still maintained ? HOT 2
- Add a changelog HOT 1
- Skip { after: :other_attribute } validation if other_attribute is nil HOT 1
- Proc in error details
- Validation doesn't work if the value is represented as a string
- Is there any way that I can make sure that ranges are exclusive?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from date_validator.