This project is archived and is in readonly mode.

#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

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

    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

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages