franckverrot / activevalidators

Collection of ActiveModel/ActiveRecord validators
https://rubygems.org/gems/activevalidators
MIT License
306 stars 49 forks source link

Date validation breaks, maybe because of ActiveAdmin datepicker #94

Closed Koroeskohr closed 8 years ago

Koroeskohr commented 8 years ago

Using Ruby 2.3.1, Rails 4.2.6, ActiveValidators 4.0.0

I have a PressRelease model that has a datetime field, released_at. When using ActiveAdmin's datepicker, my date validation (that looks like this : )

class PressRelease < ActiveRecord::Base
  validates :released_at, presence: true, date: { before: lambda { Time.now }, after: lambda { 50.years.ago } }
end

breaks and throws an ArgumentError : wrong number of arguments (given 1, expected 0).

I also use the URL validation, no problem with this one.

Full trace : trace.txt

alyssais commented 8 years ago

This error comes from your before: and after: lambdas. Using a lambda means that any the number of arguments you've specified is enforced with an exception. The date validator passes an argument to these when it calls them, which is what's causing your error.

To fix the error, change your lambdas to procs to ignore the argument silently.

Koroeskohr commented 8 years ago

The documentation in the README would then be the issue here :

class Article
  validates :slug,            :slug => true
  validates :expiration_date, :date => {
                                          :after => lambda { Time.now },
                                          :before => lambda { Time.now + 1.year }
                                        }
end