GithubHelp home page GithubHelp logo

krzyh1989 / date_validator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from codegram/date_validator

0.0 1.0 0.0 434 KB

A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel.

Home Page: http://blog.codegram.com/2011/2/date-validation-with-rails-3

License: MIT License

date_validator's Introduction

date_validator Build Status

A simple date validator for Rails 3. Should be compatible with all latest Rubies.

$ gem install date_validator

And I mean simple. In your model:

validates :expiration_date, date: true

or with some options, such as:

validates :expiration_date,
          date: { after: Proc.new { Time.now },
                  before: Proc.new { Time.now + 1.year } }
# Using Proc.new prevents production cache issues

If you want to check the date against another attribute, you can pass it a Symbol instead of a block:

# Ensure the expiration date is after the packaging date
validates :expiration_date,
          date: { after: :packaging_date }

For now the available options you can use are :after, :before, :after_or_equal_to and :before_or_equal_to.

If you want to specify a custom message, you can do so in the options hash:

validates :start_date,
  date: { after: Proc.new { Date.today }, message: 'must be after today' },
  on: :create

Pretty much self-explanatory! :)

If you want to make sure an attribute is before/after another attribute, use:

validates :start_date, date: { before: :end_date }

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send us a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2013 Codegram. See LICENSE for details.

date_validator's People

Contributors

oriolgual avatar cryo28 avatar delphaber avatar fny avatar jvperrin avatar lardawge avatar dgouyette avatar jrallison avatar txus avatar lapluviosilla avatar softwarebouwer avatar robworley avatar rudionrails avatar migol avatar t3hpr1m3 avatar yurijmi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.