Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:32:59+00:00 2026-06-09T21:32:59+00:00

I have a model with a custom validation function which verifies that the date

  • 0

I have a model with a custom validation function which verifies that the date is not in the past. Currently, the validation is hard-coded to check a single field, selected_date, in the model. How do I go about refactoring the validation so that I can either pass a parameter to the custom validation so I can test 2 fields?

class Appointment < ActiveRecord::Base
  attr_accessible :selected_date, :alternate_date

  validates_presence_of :selected_date
  validate :validate_date

  def validate_date
    if selected_date < Date.today
      errors.add(:selected_date, 'Date has passed')
    end
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:33:00+00:00Added an answer on June 9, 2026 at 9:33 pm

    create file lib/future_validator.rb:

    class FutureValidator < ActiveModel::EachValidator
    
      def validate_each(object, attribute, value)
        if value < Date.today
          object.errors[attribute] << "has passed"
        end
      end
    
    end
    

    In your models:

    validates :selected_date, presence: true, future: true
    validates :other_date, presence: true, future: true
    

    See this RailsCast: Validations in Rails 3

    NOTE: Make sure lib files are auto loaded in config/application.rb and restart the server after adding that file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom validation rule on a model that says the user can't
I have a very basic entity model which I'm trying to add custom validation
In my model I have custom validation method that sends request to the external
I have a model class that uses a custom validator as shown in following
I have a custom model binder which pulls an implementation of an interface from
In my application I have a custom model binder that I set to the
I have a Model that contains a List of a custom type. I want
I have a property on my view model that is a custom class with
I have some custom validation code, which includes a $formatter. (I store currency in
I have a model with a custom DateRange Validation Attribute public class Step1ViewModel {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.