My last question https://stackoverflow.com/questions/13112897/rails-begginer-validations was closed as too localised, so I'll try to be more generic.
Is it possible to pass value from current record (or from session) to validation option?
This works:
validates :name, :length => { :minimum => 2 }
But, if I have field "minlength" in my model, and want to validate length of the "name" against the value in "minlength" field - how can I do that?
Those examples don't work.
validates :name, :length => { :minimum => self.minlength }
validates :name, :length => { :minimum => :minlength }
validates :name, :length => { :minimum => session_variable_with_minlength_value }
you can pass custom validation for that :
for example
validate:name_length
def name_length
if self.name.size < self.minlength.to_i
errors[:base] << "whatever error message you want to display here"
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.