Validation - How to pop up error messages


#1

Hi,

I’m trying to “validate” the input provided in the login form to see if
“username” is entered or not? How do i do this ?

In the model i have done like this

validates_presence_of username, :body, :message => “Missing required
field”

But I’m not this message (Missing required field ) getting printed at
all? How do i print this?

Do i need to add some thing in the “view” also ???
Meaning if the “username” field is blank, then i want to pop up this
message. But i do not see this message getting poped up when i leave the
username field blank.

Can somebody please help out?

Thank You


#2

Dinesh U. wrote:

Hi,

I’m trying to “validate” the input provided in the login form to see if
“username” is entered or not? How do i do this ?

In the model i have done like this

validates_presence_of username, :body, :message => “Missing required
field”

It should probably be validates_presence_of :username (note the colon)

Assuming the model is called user, in the view I think you’ll need

<%= error_messages_for ‘user’ %>

somewhere on the page

A.


#3

Alan F. wrote:

Dinesh U. wrote:

Hi,

I’m trying to “validate” the input provided in the login form to see if
“username” is entered or not? How do i do this ?

In the model i have done like this

validates_presence_of username, :body, :message => “Missing required
field”

It should probably be validates_presence_of :username (note the colon)

Assuming the model is called user, in the view I think you’ll need

<%= error_messages_for ‘user’ %>

somewhere on the page

A.

Hi Franchis,

Thanks for your immediate reply. Yes that worked, i’m able to see the
error message. In the final view i’m populating some values using a
relationship, but some how that does not work if i keep the above code
<%= error_messages_for ‘user’ %>. The moment i remove the “population -
tag” in the view, then the validation works. I will have to
look into it, probably some problem with my code

And finally i think we should be able to change the color,font etc of
the error message displayed on the view??

Again thanks for your reply


#4

Dinesh U. wrote:

And finally i think we should be able to change the color,font etc of
the error message displayed on the view??

Hi Dinesh, can’t speak to the former problem without some concrete code
to look at, but on this second one the error_messages_for produces some
xhtml which is styled from the scaffold.css (I think) it should be
possible to style the box differently by looking at the XHTML source in
the browser and noting down what classes and ids are mentioned. The div
by default is errorExplanation and the class on each mesage is again
errorExplanation. You can add options of the id and class to change
them if you like.

If you need more control, you can write your own helper and go to the
object.errors yourself.

See here:
http://api.rubyonrails.com/classes/ActionView/Helpers/ActiveRecordHelper.html

(pop open the source to see how easy it is to do your own)

Alan