# Problem with using validation methods

**URL:** <https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129>\
**Category:** Rails\
**Created:** [November 18, 2009, 5:44am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129 "2009-11-18T05:44:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 18, 2009, 5:44am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/1 "2009-11-18T05:44:39Z")

</div>

Hi

I am making an application using ruby on rails. For user sign up I have  
given following validation rules

class User \< ActiveRecord::Base  
validates\_presence\_of :first\_name, :on =\> :create, :message =\> “First  
Name is mandatory”  
validates\_presence\_of :last\_name, :on =\> :create, :message =\> “Last  
Name is mandatory”  
validates\_presence\_of :login\_name, :on =\> :create, :message =\>  
“Login Name is mandatory”  
validates\_presence\_of :password, :on =\> :create, :message =\>  
“Password is mandatory”  
validates\_presence\_of :email, :on =\> :create, :message =\> “Email is  
mandatory”  
validates\_uniqueness\_of :login\_name, :on =\> :create, :message =\>  
“user already exists”  
validates\_format\_of :login\_name, :with =\> /\w/, :on =\> :create,  
:message =\> “Invalid character used for Login Name”  
validates\_confirmation\_of :password, :on =\> :create, :message =\>  
“password don’t match”  
validates\_length\_of :password, :within =\> 5…40, :message =\> “No. of  
characters in password is out of limit”  
validates\_format\_of :email, :with =\>  
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/, :on =\> :create, :message =\>  
“Wrong Format of Email ID”  
end

in the view file for sign up i am using both functions:  
error\_messages\_for ‘user’

and

error\_message\_on “user”, “field\_name”

I found that while attempting to create new object rails are checking  
all above conditions and it does not allow to create object if any of  
the condition fails but its not displaying the error messages.

I tried to find, whats wrong but all in vain…

Please help me if i made mistake in the syntax.

Thanks in advance  
Nitin

---

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 18, 2009, 6:03am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/2 "2009-11-18T06:03:18Z")

</div>

Hi Nitin M.

```
Please paste your form for new.html.erb

```

Sijo

---

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 18, 2009, 7:27am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/3 "2009-11-18T07:27:49Z")

</div>

Sijo k g wrote:

> Hi Nitin M.
> 
> ```
> Please paste your form for new.html.erb
> 
> ```
> 
> Sijo

Hi Sijo,

Thanks for replying. Here is the template to add new user:

## Please fill the following details:
 \<%= error\_messages\_for 'user' %\>

'\*' fields are mandatory

| Sign up for an account |
| --- |
| \<%= form\_tag :controller =\> "user", :action =\> "signup" %\>

```
   <p><font color="red">*</font> First Name: <%= text_field "user", 

```

“first\_name” %\>\<%= error\_message\_on “user”, “first\_name” %\>

  

_Last Name: \<%= text\_field “user”,  
“last\_name” %\>_

  

Login Name:\<font color=“green”,  
size=“2”\>(use A-Z, a-z, 0-9, “\_”) \<%= text\_field “user”,  
“login\_name” %\>\<%= error\_message\_on “user”, “login\_name” %\>

  

_Password:\<font color=“green”,  
size=“2”\>(password character range 5-40 )\<%= password\_field  
“user”, “password” %\>_

  

Confirm Password: \<%=  
password\_field “user”, “confirm\_password” %\>

  

\* Email address: \<%= text\_field  
“user”, “email” %\>

  

\<input type=“Submit”, value=“Sign up” /\> \<input type=“Reset”,  
value=“Reset” /\>

  
\<%= end\_form\_tag %\> |

Thnx again  
Nitin

---

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 18, 2009, 9:11am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/4 "2009-11-18T09:11:50Z")

</div>

Hi Sijo,

I made changes as you said. Now to display errors used

\<%= f.error\_messages %\>

I found it in the [api.rubyrails.org](http://api.rubyrails.org) website  
([ActionView::Helpers::FormHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001604))  
but I am getting an error:

undefined method ‘error\_messages’

Is there any other syntax to display these errors.

Thanks  
Nitin

---

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 18, 2009, 9:32am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/5 "2009-11-18T09:32:08Z")

</div>

Hi Nitin M.

```
 Can't ay without seeing code.Anyway try

```

\<%= error\_messages\_for ‘user’ %\> insted of f.error\_messages

Sijo

---

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 18, 2009, 7:58am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/6 "2009-11-18T07:58:47Z")

</div>

Hi Nitin M.

```
 It can be approached in a railsway like

```

First in views/users/new.html.erb add like below(I am writing only a  
skeleton Please modify it).

## Please fill the following details:
\<% form\_for(@user) do |f| %\>

'\*' fields are mandatory

\* First Name: \<%= f.text\_field :first\_name %\>

\* Last Name: \<%= f.text\_field :last\_name %\> ----- ------etc \<%= f.submit 'SignUp' %\> \<% end %\>

```
And in controller what you have in 'signup' action move to 'create' 

```

action

Sijo

---

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 18, 2009, 10:06am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/7 "2009-11-18T10:06:59Z")

</div>

Hi Nitin M.

```
Can you paste your relevant view and controller code?

```

Sijo

---

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 18, 2009, 9:39am UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/8 "2009-11-18T09:39:45Z")

</div>

Sijo k g wrote:

> Hi Nitin M.
> 
> ```
> Can't ay without seeing code.Anyway try
> 
> ```
> 
> \<%= error\_messages\_for ‘user’ %\> insted of f.error\_messages
> 
> Sijo

Hi Sijo,

That I have already tried. It did not work for me. [:(]

I don’t know where its going wrong. Anyways can you please tell  
me if there is any other method to display error messages when  
user does not enter data in the form as per mentioned in validation  
in the model file.

Sorry for bothering you. But I need help.

Thanks  
Nitin

---

<div class="post-metadata">

**Author:** ![nitin\_k\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nitin\_k\_m](https://www.ruby-forum.com/u/nitin_k_m)\
**Post date:** [November 20, 2009, 2:27pm UTC](https://www.ruby-forum.com/t/problem-with-using-validation-methods/179129/9 "2009-11-20T14:27:56Z")

</div>

Hey Sijo

I got other way to show messages. Thanks for your help.

Nitin
