Check the minimum and maximum length for individual cases at a time

I check 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 where the minimum and maximum without a separate syntax check, like this

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

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

  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 } 

How can this be done in one go?


Decision

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' } 
+4
source share
1 answer
 validates_length_of :surname, :within => 3..30, :too_short => 'too short message', :too_long => 'too long message' 
+6
source

Source: https://habr.com/ru/post/1335594/


All Articles