I’ve been dealing with a lot of forms lately, and wanted to have some
way of indicating to the user that a specified field is required, such
as displaying some icon or asterisk next to it, but if the form is
submitted with errors, this icon/asterisk will change to another
image, such as a big red X.
The best solution I could come up with was to override a few of the
input tag helpers, such as text_field and text_area so that they
accept an additional parameter, “:required => true” which will then
place a “…” around the element. My
“required” CSS class then uses a background image, offset to the
right, to denote that a field is required.
When a field is submitted with errors, rails adds the “…” around my “…” tag, so I can then use CSS rules to
change the image to something else (a red X in this case).
What I’m wondering is how to determine (from within my overridden
text_field method) which attributes of the model have been specified
in the “validates_presence_of” method. I figure it’d be cleaner to
simply check which fields are required to be non blank, as specified
in the model, rather than force the programmer to write “:required =>
true” for each of these fields.
If anyone has any suggestions, or ideas of where I should look, please
let me know. Thanks,