How to Validate the Date Such That It Is After Today in Rails

How to validate the date such that it is after today in Rails?

Your question is (almost) exactly answered in the Rails guides.

Here's the example code they give. This class validates that the date is in the past, while your question is how to validate that the date is in the future, but adapting it should be pretty easy:

class Invoice < ActiveRecord::Base
validate :expiration_date_cannot_be_in_the_past

def expiration_date_cannot_be_in_the_past
if expiration_date.present? && expiration_date < Date.today
errors.add(:expiration_date, "can't be in the past")
end
end
end

Rails date validation with conditional validation if and future? method

Thank you for Mark Merritt because I inspired his answer. The answer works as expected, but the problem is keeping the model DRY, and also, it has a long method name.

I created separate validator which is name at_future_validator.rb. I placed the file inside of lib/validators folder.

Then, I wrote this validator

# lib/validators/at_future_validator.rb
class AtFutureValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
if attribute.present? && value < Date.today
object.errors[attribute] << (options[:message] || 'must be in the future')
end
end
end

OK. The first part has done. The important part is, which I saw now on the guide, working with custom validator which we named at_future_validator. We need to require the validator inside of the house model.

# app/models/house.rb
class House < ApplicationRecord
require_dependency 'validators/at_future_validator'
# ...
validates :available_at, presence: true, at_future: true
# ...
end

Guides that I followed

#211 Validations in Rails 3 - 8:09

How to validate start date on update?

I can't test it right now but I think it might work:

validate :previous_start_date

def previous_start_date
old_start_date = Model.find(self.id).start_date
if(old_start_date > self.start_date)
self.errors.add(:start_date, "Can't be previous than the initial date")
end
end

At the moment of the validation, the object hasn't been saved yet, so, I believe that retrieving the object from the database will give you the previous value. With the value in hand you can compare with the current start_date and then add your custom error.

I hope it helps.

Only future dates - validation in Rails model

# lib/future_validator.rb
class FutureValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if record[attribute] < Time.now
record.errors[attribute] << (options[:message] || "can't be in the past!")
end
end
end

class User < ActiveRecord::Base
validates :name, presence: true
validates :date, presence: true
validates :future_event, future: true
end
  • http://guides.rubyonrails.org/active_record_validations.html#custom-validators

how to compare today date and input date in rails?

Do you want something like this in the controller?

counter = 0
@stages.each{|s| counter += 1 if s.planned_end_date.past?}

.past? method source

Check if DateTime value is today, tomorrow or later

Here are some useful ways to achieve it:

datetime = DateTime.now  => Sun, 26 Oct 2014 21:00:00

datetime.today? # => true
datetime.to_date.past? # => false (only based on date)
datetime.to_date.future? # => false (only based on date)

datetime.to_date == Date.tomorrow # => false
datetime.to_date == Date.yesterday # => false

Rails validate type date?

Rails doesn't support this directly; the closest it comes is probably validates_format_of, or supplying your own custom validator.

I think what you want is the validates_timeliness gem. It not only validates that something is a valid date, but you can specify whether it should be before or after today and various other date range checks.

How to validate uniqueness of date portion of datetime data type

You can add a before_create callback, replace Model and field with respective model and datetime field

before_create :check_date

def check_date
unless Model.where(field: field.to_date.beginning_of_day..field.to_date.end_of_day).blank?
return false
else
return true
end
end


Related Topics



Leave a reply



Submit