Edge Rails

[Edge Rails] 入力文字数の長さを定義する : Custom Length Validation Tokenizer

2008/08/03

validates_length_of では、:minimum, :maximumなどのオプションによって入力最小/最大文字数を定義することができます。いままでは単純に文字数のカウントを行っているだけでしたが、今回追加された :tokenizer オプションによって、文字のカウント方法を定義できるようになりました。

たとえば下記の場合、3行目で入力文字が何ワードあるかを調べるように定義し、10ワード以下であればエラーになるようになっています。

1
2
3
validates_length_of :article, :minimum => 10,
  :too_short => "Your article must be at least %d words in length.",
  :tokenizer => lambda {|str| str.scan(/\w+/) }

related posts


tags

, , ,


have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

:

:


« [Edge Rails] partialのcollection時に好きな変数名をつける:Collection Partial Variable Naming
» [Edge Rails] join時のcondtionsをわかりやすく : Easy Join Table Conditions