Forum: Ruby on Rails Validation - How to pop up error messages

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dinesh U. (Guest)
on 2006-03-27 16:30
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
Alan F. (Guest)
on 2006-03-27 16:49
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.
Dinesh U. (Guest)
on 2006-03-27 17:48
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 -
<selection> 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
Alan F. (Guest)
on 2006-03-27 19:57
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/Help...

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

Alan
This topic is locked and can not be replied to.