Accessing validations from view


I spent a while going through API docs but couldn't figure this out. Is there
a way to access a model's validation scheme from within the view?

I would like to do this so that I can programmatically generate form fields.
If this can be done then I will do things like:

- set maxlength of form fields to :maximum (from validates_length_of)
- put an asterisk (or whatever) beside any fields that have validations and
  allow_nil is not true - to indicate that it is a required field
- etc.

I realize that the user can submit the form and get all the feedback he needs
anyway but I just think it would be nice to help out the user a bit on the
initial form.

Nice idea, I don’t have a clue if this is possible tho :slight_smile:



I am part way through creating an autoform generator, and had same
question. Solved it with columns_hash.limit - see API doc,
ActiveRecord .

eg. For a model called Customer, referencing the field name using

<% field_length_max=Customer.columns_hash[field].limit %>


Cool thanks. Yes that gets me part way. This method seem to look
specifically at the information active record has gathered from the database
tables - that does give a reasonable assumption of maxlength but it doesn't
let me know whether allow_nil has been set to true or not in the validation.

Not with plain Rails. I suggest two of my plugins: