#422 √ wontfix
David Lowenfels

patch to add :count=>:words to validates_length_of

Reported by David Lowenfels | June 15th, 2008 @ 06:22 AM

This is a patch to add a feature to count words instead of characters, in the ActiveRecord validates_length_of method.

for instance:

validates_length_of :essay, :minimum=200, :count=>:words, :too_short=>"Your essay must be at least %d words."

The patch includes the following tests.

def test_validates_length_of_with_word_count_is
  Topic.validates_length_of( :title, :is => 5, :count => :words )
  
  assert !Topic.create("title" => "this title has too many words").valid?
  assert !Topic.create("title" => "too little").valid?
  assert Topic.create("title" => "this title has just enough").valid?
end

def test_validates_length_of_with_word_count_within
  Topic.validates_length_of( :title, :within => 3..5, :count => :words )

  t = Topic.new( :title => "not enough")
  assert !t.valid?
  assert_equal "is too short (minimum is 3 words)", t.errors.on(:title)

  t = Topic.new( :title => "this title has too many words")
  assert !t.valid?
  assert_equal "is too long (maximum is 5 words)", t.errors.on(:title)
end

Comments and changes to this ticket

  • Pratik

    Pratik June 27th, 2008 @ 04:33 PM

    • → Tag changed from “” to “activerecord patch”
    • → State changed from “new” to “invalid”

    I don't think this is a very common requirement. I'd suggest releasing a plugin. Although, you can try emailing railscore mailing list to see if there is enough interest in the patch, in which case we can roll it in.

    Thanks!

  • Jeremy Kemper

    Jeremy Kemper June 28th, 2008 @ 03:21 AM

    Seconded. I've never needed to count word length. If I did, I think I'd prefer adding a new kind of validation instead of another layer on validates_length_of.

  • David Lowenfels

    David Lowenfels June 28th, 2008 @ 03:53 AM

    It seemed like a natural fit to me because the existing rails code already is splitting on '', so I just modified it to optionally split on '\W'. It's a low hanging fruit. I thought a plugin would be overkill, as the core implementation happens on one line (which occurs twice in the code):

    • value = value.split(//) if value.kind_of?(String)
    • value = value.split(/#{split_token}/) if value.kind_of?(String)

    The rest is just icing on the cake to extend the API, modify the validation error messages, and add tests.

  • Jeremy Kemper

    Jeremy Kemper June 28th, 2008 @ 05:28 AM

    • → State changed from “invalid” to “wontfix”

    Good point, David! We split with // so we get Unicode-aware character counts instead of byte length. This is an implementation detail that goes away in Ruby 1.9.

    Perhaps we should accept an optional block to take an object and return a count.

    validates_length_of(:essay, :minimum => 200, :too_short=>"Your essay must be at least %d words.") { |str| str.scan(/\w+/).size }
    
  • David Lowenfels

    David Lowenfels June 28th, 2008 @ 11:26 PM

    @jeremy: that sounds like a good solution. Should I cook up a patch to accept a block?

  • Jeremy Kemper
  • David Lowenfels

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

Repository is at http://github.com/rails/rails

Check out the development master (Edge Rails):

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..

Shared Ticket Bins

People watching this ticket