DEV Community

Moses Gathuku
Moses Gathuku

Posted on

Rails custom validation contexts

In rails, it is possible to specify when the validation should happen. sometimes you may want to introduce new validation or skip some. Rails on option helps us achieve this.

class Post < ApplicationRecord
 validates :body, presence: true
 validates :title, uniqueness: true, on: :create 
 validates :published, exclusion: [nil], on: :update 
end
Enter fullscreen mode Exit fullscreen mode

From the above validations:

  • it's not possible to create or update a post without a body
  • it will be possible to update a post with a duplicate title
  • it will be possible to create a record with published nil

Apart from the commonly used on: :create and on: :update rails allow us to provide custom contexts.

class Post < ApplicationRecord
 validates :title, presence: true 
 validates :published_at, presence: true, on: :publish
end
Enter fullscreen mode Exit fullscreen mode

From the above example, we are validating published_at presence on: :publish. Custom contexts are triggered explicitly by passing the name of the context to valid? invalid? or save

post = Post.new(title: "Rails validations")
post.valid? # true 
post.valid?(context: :publish) # false 
post.save(context: :publish) # false
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
kinduff profile image
Alejandro AR • Edited

Good post, if you want you can also do a block:

class Post < ApplicationRecord
  validates :title, presence: true

  with_options on: :publish do
    validates :published_at, presence: true
    validates :description, presence: true
  end
end
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gathuku profile image
Moses Gathuku

@kinduff with_options looks awesome