How to check checks existing in ActiveRecord :: Base Objects

I want to create a helper method for automatically displaying text fields with the maxlength attribute. I want this maxlegth to be set based on: max specified in the attributes of the validates_length field in order to remain DRY.

My question is: is there a good way to check the checks that are present in the attribute of the objects, and then extract the maximum length from this check.

I also plan on extracting other things, such as regex, from validates_format, so I can set the attribute in a text box, which js can then use to validate on the client side.

Thanks.

Reward Points: Why doesn't Rails automatically add maxlength to text fields for us?

+4
source share
2 answers

In Rails 3, you can call the _validators method on an object to get a list of _validators to be executed:

 t = Ticket.new t._validators 
+9
source

Well, I don't know if this is a “good” way, but can you initialize a new object by calling #valid? on it, and then call #errors to get a hash of attributes and error messages. Then you have to parse the error messages.

 user = User.new user.valid? errors_hash = user.errors 
+2
source

All Articles