Simple Rails 3 validator to handle arbitrary "repeat up to" validations.
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.markdown

README.markdown

Repeats Validator

Simplistic validator that handles arbitrary "repeat up to n times" validations.

The counting mechanism is passed in to the validator via the options hash.

In addition, the validator will optionally add methods to the class under validation to indicate a repeats-validation-specific error, avoiding having to check the model's errors collection for both the field and error type.

Example

Assume a requirement that entries in a sweepstakes only allow three entries per email address.

class Entry
    # ...
    validates :email, :presence => true,
                      :email => true,
                      :repeats => {
                          :upto => 3,
                          :add_methods => true,
                          :count_with => lambda { |rec, attr, val| Entry.count(TODO) }
                      }
end

Then in a controller, say, we can check for this error specifically, and cleanly.

def create_entry
  # ...
  if @entry.save
      # ...
  else
      render @entry.email_repeats_error ? 'too_many_entries' : 'new'
  end
end