2011-01-14 50 views
4

我驗證姓字段這樣驗證最小,並且在一個時間不同的案件最大長度

validates :surname, 
    :presence  => true, 
    :length   => { :within => min_surname_length..max_surname_length, :message => "is bad (minimum is #{min_surname_length}, maximum is is #{max_surname_length})" } 

但我想單獨情況下,當最大和最小而不這樣

validates_length_of :name, :minimum => 3 
validates_length_of :name, :maximum => 30 
單獨語法驗證

在幾句話,我願做這樣的事(我知道,這是不對的):

validates :surname, 
    :presence  => true, 
    :length   => { :within => min_surname_length..max_surname_length, 
         :message => "is bad (minimum is #{min_surname_length}" IF MINIMUM, 
         :message => "is bad (maximum is is #{max_surname_length})" IF MAXIMUM } 

一次可以做到這一點?


SOLUTION

這是我怎麼會使用它:

validates :surname, 
    :length   => { :within => min_password_length..max_password_length, 
         :too_short => 'too short message', 
         :too_long => 'too long message' } 

回答

6
validates_length_of :surname, 
    :within => 3..30, 
    :too_short => 'too short message', 
    :too_long => 'too long message'
+1

非常感謝! – user502052 2011-01-14 19:03:54

相關問題