#507 √ resolved
David Lowenfels

patch to add :tokenizer option to validates_length_of

Reported by David Lowenfels | June 29th, 2008 @ 01:47 AM | in 2.x

see ticket #422 for background info.

:tokenizer - Specifies how to split up the attribute string. (e.g. :tokenizer => lambda{ |str| str.scan(/\w+/)} to count words as in above example.)

Defaults to lambda{ |value| value.split(//) } which counts individual characters.

  def test_validates_length_of_with_block
    Topic.validates_length_of :content, :minimum => 5, :too_short=>"Your essay must be at least %d words.", :tokenizer => lambda{ |str| str.scan(/\w+/) }
    t = Topic.create!(:content => "this content should be long enough" )
    assert t.valid?
    
    t.content = "not long enough"
    assert !t.valid?
    assert t.errors.on(:content)
    assert_equal "Your essay must be at least 5 words.", t.errors[:content]
  end

Comments and changes to this ticket

  • David Lowenfels

    David Lowenfels June 29th, 2008 @ 01:48 AM

    • → Tag changed from “activerecord” to “activerecord patch”

    I decided to go with the convention of passing a keyed proc instead of an anonymous block.

  • José Valim
  • Clemens Kofler

    Clemens Kofler July 1st, 2008 @ 12:12 AM

    +1 - love it.

    How about also adding shortcuts to the most used options (i.e. :characters and :words)? I think that would make it even better!

  • Pratik

    Pratik July 1st, 2008 @ 04:44 PM

    • → Assigned user changed from “” to “Jeremy Kemper”
  • Repository

    Repository July 4th, 2008 @ 02:13 AM

    • → State changed from “new” to “resolved”

    (from [dd8946231c1d8a8f0ac4e299c66d87226a157a1a]) Add :tokenizer option to validates_length_of. [#507 state:resolved]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

  • Repository

    Repository July 4th, 2008 @ 02:13 AM

    (from [87fbcaa6229e9073095fb8d77c7a536c9466fbce]) Add :tokenizer option to validates_length_of. [#507 state:resolved]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

  • Tarmo Tänav

    Tarmo Tänav July 7th, 2008 @ 05:31 PM

    Did anyone notice that jeremy in ticket #422 suggested a block that would return "str.scan(/\w+/).size", but in this ticket that somehow got turned into a tokenizer, "str.scan(/\w+/)", which is a lot more limiting. Any reason why that happened?

    It seems obvious that returning a size would be enough for validates_length_of, and that there should be no real need to generate all the tokens, especially in cases where there would not be a sensible way to generate the tokens (for example if you for some reason wanted to count capitalized letters as two characters).

  • David Lowenfels

    David Lowenfels July 7th, 2008 @ 07:53 PM

    @Tarmo: yes, there was a reason. Look at the diff and you will see that it was less messy to substitute a block that returns an array, so that the size method is only called once. Go ahead and make an additional patch if you think it should behave differently.

    Personally, I think the concept of counting the number of tokens is the most semantic. Otherwise the validation code in the model becomes kind of cryptic.

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