'Preventing booking in the past and preventing users to select a date which is back in time
So I have this model. To not book in the past and 10 days from Date.today works fine. But when I try to book and for example book in the past, the validations still give a green tick next to the date instead of giving the error and letting the user know that their booking wasn't created due to them booking in the past. I'll attach a pic to clarify my problem.
And I was wondering if there is a way to stop users from being able to book in the past. If that would be possible the error on the top would automatically be dismissed
class Booking < ApplicationRecord
belongs_to :user
belongs_to :listing
validate :not_past_start_date
def not_past_start_date
if self.start_date < Date.today + 10
errors.add(:date, 'please start the booking 10 days from todays date')
end
end
validate :not_past_end_date
def not_past_end_date
if self.end_date < Date.today + 16
errors.add(:date, 'bookings have to be at least a week long')
end
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
