Date Validation Gems in Rails 5

Steps

Step 1

Add the validates_timeliness gem to Gemfile.

gem 'validates_timeliness', '~> 4.0'

Run bundle install.

Step 2

Generate the validates_timeliness initializer files.

rails generate validates_timeliness:install

It creates the following files.

config/initializers/validates_timeliness.rb
config/locales/validates_timeliness.en.yml

Step 3

Add the validation provided by the validates_timeliness gem to the task model.

validates_datetime :due_date

The task model looks like this:

class Task < ApplicationRecord
  belongs_to :project, counter_cache: true
  has_many :comments

  validates_datetime :due_date  
end

Step 4

Change the update action in the tasks controller:

def update
  respond_to do |format|
    if @task.update(task_params)
      format.html { redirect_to @task, notice: 'Task was successfully updated.' }
      format.json { render :show, status: :ok, location: @task }
    else
      format.html { render :edit }
      format.json { render json: @task.errors, status: :unprocessable_entity }
    end
  end
end

This works for any value except junk string value for the date. Let's fix this problem.

Step 5

Add the date_validator gem to the Gemfile.

gem 'date_validator'

Run bundle install.

Step 6

Remove the old validation for due_date and add the following validation that uses the new gem.

validates :due_date, date: true

Step 7

Edit task. Junk text is now validated.

Summary

In this article, you learned how to use date_validator gem to validate the date field in Rails 5 apps.

Reference

date_validator gem


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.