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 6763449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:28:38+00:00 2026-05-26T14:28:38+00:00

I am using Rails 3.0.3 where I have quite an extensive validation process. I

  • 0

I am using Rails 3.0.3 where I have quite an extensive validation process.

I want to make a validation where I check if a certain variable is a proper date

Basically this is what I want to do is:

def validate_date(this_date)?

    begin
       Date.parse(this_date)
    rescue
      proper_date = false
    else
          proper_date = true
    end
end

and call this method in my model using:

attr_accessor :given_date    
validate_date(given_date)

but also at another place in my validation file using for example:

attr_accessor :another_date
validate_date(another_date)

or something similar…

What do I need to do to make this work?

  • 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-05-26T14:28:38+00:00Added an answer on May 26, 2026 at 2:28 pm

    What you should do is something similar to nkm’s answer:

    validates :name,
      presence => true # or other standard validations
    validate :valid_my_date_variable
    
    def valid_my_date_variable
      if my_date_variable.present?
        errors[:my_date_variable] << "is not valid" unless valid_date(my_date_variable)
      end
    end
    
    def validate_date(this_date)?
      begin
        Date.parse(this_date)
      rescue
        proper_date = false
      else
        proper_date = true
      end
    end
    

    Or you can create validator for your date and use it as you use standard validators.

    Refer to documentation on how to create custom validator if you prefer keep your models cleaner.

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

Sidebar

Related Questions

I have been using MSSQL 2005 with Rails for quite a while now, and
I have a deployment using: rails 2.3.2 ruby 1.8.7 mysql db and 3 mongrel
I'm using jQuery with rails and have the following piece of code $('#related').html(<%= render
I am using rails beta 3 and I have a erb page named index.html.erb
I have a legacy project using Rails 2.3.5 but I can't find with which
I am using Rails 3.2. I have following models: Blog Comment User class Blog
I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it
I have been using the Rails console a fair bit lately and its making
I have a class User in Rails using Mongoid and Devise. I can't seem
I using the Rails resource_controller plugin. I have a ReleasesController, which is nested within

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.