[Validation] rails How to specify after today's date

【goal】

When entering a date, you can only enter dates after today Validate

【merit】

■ Improvement of UX ■ Improved understanding of validate

【Development environment】

■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7

【Implementation】

model/item.rb





validate :date_before_start
validate :date_before_finish



  def date_before_start
    return if start_day.blank?
    errors.add(:start_day, "Please select the one after today") if start_day < Date.today
  end

  def date_before_finish
    return if finish_day.blank? || start_day.blank?
    errors.add(:finish_day, "Please select the one after the start date") if finish_day < start_day
  end

that's all! !!

[I want to read it together]

■ Differences between Time, Date, DateTime, TimeWithZone between Ruby and Rails https://qiita.com/jnchito/items/cae89ee43c30f5d6fa2c

■ [ERROR message display] A simplified version that can be used at any time with the rails partial template. https://qiita.com/tanaka-yu3/items/63b189d3f15653cae263

■ [Date.today] How to retrieve information only for today rails https://qiita.com/tanaka-yu3/items/741711bd743b80eda51a

Recommended Posts

[Validation] rails How to specify after today's date
How to write Rails validation
[Rails] How to use validation
How to specify validation for time_field
How to write Rails
How to uninstall Rails
[Rails] How to deal with URL changes after render
How to write a date comparison search in Rails
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails seed
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
How to write a migration from Rails datetime type to date type
How to specify db when creating an app with rails
Let's summarize how to extend the expiration date of Rails
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to use Ruby on Rails
[Rails] How to add new pages
Rails on Tiles (how to write)
[Rails] How to write exception handling?
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
How to return Rails API mode to Rails
How to get along with Rails
[Introduction to Rails] How to use render
How to install Swiper in Rails
How to install Pry after building Rails development environment with Docker
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails
How to implement date calculation in Java
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to convert from erb to haml
[Rails] How to upload images using Carrierwave
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method