开发者

rails, retrieve maximum value of validates_length_of

I have a model wi开发者_JAVA百科th a validation on its attribute:

validates_length_of :description, :maximum => 1000

validation is done on server side, but I would like to add some constraints in the client size by using a jquery limitation plugin on the textarea input corresponding to :description attribute. I have succeded to do this. but I would like to avoid retyping the :maxium value (1000) for the javascript side. Is there a way to get that :maxmium value in rails ? to write it in the js part?


It is possible to get at the validations for a given ActiveModel class from the validators property. This returns a list of validator objects. The one you are interested in is a LengthValidator, and if you can select the one you are interested in the maximum value is available in the options hash. Here is a crude example:

require 'active_record'

class MyModel < ActiveRecord::Base
   validates_length_of :title, :maximum => 1000
end

puts MyModel.validators[0].options   # -> {:maximum=>1000}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜