7

What's the best way to validate that the end date is not before the start date, and start date is after the end date in Rails?

I have this in my view controller:

<tr>
    <td><%= f.label text="Starts:" %></td>
    <td><%= f.datetime_select :start_date, :order => [:day, :month, :year]%></td>
</tr>
<tr>
    <td><%= f.label text="Ends:" %></td>
    <td><%= f.datetime_select :end_date,:order => [:day, :month, :year]</td>
</tr>

I want it to come up with a popup of sorts, with a meaningful message.

I would like to make a generic method that takes two parameters, start and end date, which I then can call in my viewcontroller ; fx in the code above. Or, do I need to use jQuery instead?

5 Answers 5

14

@YaBoyQuy client-side validation can work and avoids a hit to the server...

The question is also about end_date being after the start, so the validation - using gem date_validator - should also state

validates :end_date, presence: true, date: { after_or_equal_to:  :start_date}

The suggestion of

on: :create

would be incorrect for the end_date validation; logically this should also be run on an edit.

I upvoted on the basis of the succinct syntax.

1
  • 2
    This answer is very readable, but it requires the date_validator validation gem. It's not available in vanilla Rails. May 3, 2019 at 19:47
9

Clean and Clear (and under control?)

I find this to be the clearest to read:

In Your Model

validates_presence_of :start_date, :end_date

validate :end_date_is_after_start_date


#######
private
#######

def end_date_is_after_start_date
  return if end_date.blank? || start_date.blank?

  if end_date < start_date
    errors.add(:end_date, "cannot be before the start date") 
  end 
end
6

Avoid client side validation, because it only validate client side... Use the rails validaters built in.

  validates :start_date, presence: true, date: { after_or_equal_to: Proc.new { Date.today }, message: "must be at least #{(Date.today + 1).to_s}" }, on: :create
  validates :end_date, presence: true
1
3

If you want client side validation, use jQuery.

Or in rails, to validate server side, you could create your own I guess?

def date_validation
  if self[:end_date] < self[:start_date]
    errors[:end_date] << "Error message"
    return false
  else
    return true
  end
end

Rails >= 7.0 makes this a one-liner

validates_comparison_of :end_date, greater_than_or_equal_to: :end_date

PR, Rails Guides

1
  • why one need to return truth value from date_validation?
    – Amit Patel
    Aug 31, 2012 at 9:41
2

to use your validates :dt_end, :date => {:after_or_equal_to => :dt_start} you need to have a DateValidator such as this:


class DateValidator > ActiveModel::Validator
  def validate(record)
    the_end = record.dt_end
    the_start = record.dt_start
    if the_end.present?
      if the_end < the_start
        record.errors[:dt_end] << "The end date can't be before the start date. Pick a date after #{the_start}"
      end
    end
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.