rails validation plugin. validates word count for a block of text.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
config/locales
lib
README
init.rb

README

Validates Word Count
====================

When validating a string item, ensure the word count is in a specified range.

Example
=======

class Person < ActiveRecord::Base
  validates_word_count :name,
      :minimum => 2, :too_few_words => "your name is too short (must be 2 words)",
      :maximum => 4, :too_many_words => "your name is too long (must be less than 4 words)"
end

p = Person.new(:name => "Bob")
p.valid?   # false
p = Person.new(:name => "John Doe")
p.valid?   # true

You can also supply an :item_name parameter to customize the error message

class MyItem < ActiveRecord::Base
  validates_word_count :prompt, :minimum => 3, :item_name => :activity
end

i = MyItem.new(:activity => "cooking", :prompt => "I like green eggs and ham")
i.valid? # false
 --> describe what prompted your interest in cooking in 20 words or fewer


Localization
============
If you don't provide a configuration option (i.e. :too_many_words), validates_word_count use
Rails' I18n system to find the right string. It will try three strings:
1. activerecord.errors.models.#{class_name}.attributes.#{attribute_name}.too_many_words
   i.e. activerecord.errors.models.person.attributes.name.too_many_words
2. activerecord.errors.models.#{class_name}.too_many_words
   i.e. activerecord.errors.models.person.too_many_words
3. activerecord.errors.messages.too_many_words (this is supplied in the plugin)

Here are example en.yml files corresponding to (1) and (2):
en:
  activerecord:
    errors:
      models:
        person:
          attributes:
            name:
              too_many_words: "enter your name in %{words} words or less"

en:
  activerecord:
    errors:
      models:
        person:
          too_many_words: "enter your name in %{words} words or less"

Here's what the en.yml would look like when you use :item_name -- note the _with_name suffix on the attribute name.

en:
  activerecord:
    errors:
      models:
        myitem:
          attributes:
            prompt_with_name:
              blank: "enter what prompted your interest in %{name}"
              too_many_words: "describe what prompted your interest in %{name} in %{words} words or fewer"
            prompt:
              blank: "enter what prompted your interest"
              too_many_words: "describe what prompted your interest in %{words} words or fewer"


Copyright 2010-11 Paul Schreiber, released under the MIT license