[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
[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
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 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
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
[Rails] How to add new pages
[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
[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
[Rails] How to convert from erb to haml
[Rails] How to upload images using Carrierwave
How to insert a video in Rails
[Rails] How to use the map method
How to use MySQL in Rails tutorial
How to resolve errors when installing Rails 5.1.3
[Java] How to set the Date time to 00:00:00
How to fix system date in JUnit
[rails] How to configure routing in resources
How to implement TextInputLayout with validation function
How to get the date in java
[Ruby on Rails] How to use redirect_to
[rails] How to create a partial template
[Rails 5.x] How to introduce free fonts
How to implement ranking functionality in Rails
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to pass validation such as password when executing update action