开发者

Validate minimum and maximum length for separate cases in one time

开发者 https://www.devze.com 2023-02-04 23:28 出处:网络
I validate a surname field like this validates :surname, :presence=> true, :length=> { :within => min_surname_length..max_surname_length, :message => \"is bad (minimum is #{min_surname_le

I validate a surname field like this

  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})" }

but I would like to separate cases when minimum and maximum without separate syntax validation like this

validates_length_of :name, :minimum => 3
validates_length_of :name, :maximum => 30

In few words, I would like to do something like this (I know, it is wrong):

  validates :surname,
    :presence       => true,
    :length         => { :within => min_surname_length..max_surname_length, 
                         :message =>开发者_StackOverflow中文版; "is bad (minimum is #{min_surname_length}" IF MINIMUM, 
                         :message => "is bad (maximum is  is #{max_surname_length})" IF MAXIMUM }

How it is possible to do that in one time?


SOLUTION

This is how I will use it:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消