# How to visualize mandatory fields

**URL:** <https://www.ruby-forum.com/t/how-to-visualize-mandatory-fields/132046>\
**Category:** Rails\
**Created:** [March 16, 2008, 12:10am UTC](https://www.ruby-forum.com/t/how-to-visualize-mandatory-fields/132046 "2008-03-16T00:10:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![html-kurs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea5d25/32.png) [@html-kurs](https://www.ruby-forum.com/u/html-kurs)\
**Post date:** [March 16, 2008, 12:10am UTC](https://www.ruby-forum.com/t/how-to-visualize-mandatory-fields/132046/1 "2008-03-16T00:10:44Z")

</div>

Hi,

I’d like to display an input form with some mandatory fields (e.g. with  
an extra “_"). Some fields aren’t mandatory ( so they don’t get the  
"_”).  
How do I get the information about the field if there’s an  
“validate\_presence\_of” in the model or not in the view?

TIA,  
Martin

---

<div class="post-metadata">

**Author:** ![html-kurs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea5d25/32.png) [@html-kurs](https://www.ruby-forum.com/u/html-kurs)\
**Post date:** [March 16, 2008, 5:23am UTC](https://www.ruby-forum.com/t/how-to-visualize-mandatory-fields/132046/2 "2008-03-16T05:23:48Z")

</div>

I am not totally sure how you would get an extra \*  
but if you want to outline the field that is mandatory from a  
validates\_presence\_of filter then you need to make sure  
your form\_for object is the same name as the model  
Example:  
Say you have a model like this:

class User \< ActiveRecord::Base  
validates\_presence\_of :username  
end

Then your form\_for object should be something like this:  
form\_for :user, :url =\> users\_path do |f|  
f.text\_field :username  
end

and when it fails it will be wrap with a div tag with a class name of  
“fieldWithErrors”  
and that would end up looking something like this:

then you can just outline it with css any color you want to indicate  
that the field is mandatory.  
the css would probably end up looking something like this:  
.fieldWithErrors\>input[type=“text”] {  
border:2px solid red;  
}

that’s the way I do it… I have yet to use the “\*” (asterisk) for my  
fields.

---

<div class="post-metadata">

**Author:** ![html-kurs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea5d25/32.png) [@html-kurs](https://www.ruby-forum.com/u/html-kurs)\
**Post date:** [March 16, 2008, 10:38am UTC](https://www.ruby-forum.com/t/how-to-visualize-mandatory-fields/132046/3 "2008-03-16T10:38:35Z")

</div>

Martin No wrote:

> I’d like to display an input form with some mandatory fields (e.g. with  
> an extra “_"). Some fields aren’t mandatory ( so they don’t get the  
> "_”).  
> How do I get the information about the field if there’s an  
> “validate\_presence\_of” in the model or not in the view?

You could do something like this:

In your model:

class MyModel \< ActiveRecord::Base  
@@mandatory\_fields = [:field1, :field2]  
validates\_presence\_of \*@@mandatory\_fields  
def self.mandatory\_fields ; @@mandatory\_fields ; end

Then in the view, for each field you can do something like:

\<%= ‘\*’ if MyModel.mandatory\_fields.include? :this\_field %\>  
This field: \<%= f.text\_field :this\_field %\>

For a reusable solution you could extend ActiveRecord::Base with  
something that does the equivalent and create a view helper to use it.  
Have a look here for something similar:

[http://www.ruby-forum.com/topic/146123](http://www.ruby-forum.com/topic/146123)

---

<div class="post-metadata">

**Author:** ![html-kurs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea5d25/32.png) [@html-kurs](https://www.ruby-forum.com/u/html-kurs)\
**Post date:** [March 16, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/how-to-visualize-mandatory-fields/132046/4 "2008-03-16T15:34:48Z")

</div>

Hi Mark,

> \<%= ‘\*’ if MyModel.mandatory\_fields.include? :this\_field %\>  
> This field: \<%= f.text\_field :this\_field %\>

that’s what I was looking for. Thanks.

> For a reusable solution you could extend ActiveRecord::Base with  
> something that does the equivalent and create a view helper to use it.  
> Have a look here for something similar:
> 
> [How to iterate through instance attribute names (attr\_accessor) - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/146123)

Because I’m new to ruby (and rails) I’ll study it and may ask  
agains 😉

Thanks a lot,  
Martin
