Reputation: 1271
I have a model with 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?
Upvotes: 0
Views: 194
Reputation: 15736
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}
Upvotes: 2