# Form validation/errors not working

**URL:** <https://www.ruby-forum.com/t/form-validation-errors-not-working/173599>\
**Category:** Rails\
**Created:** [August 11, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599 "2009-08-11T21:40:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 11, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/1 "2009-08-11T21:40:46Z")

</div>

Hello. I’m trying to create a form. It has only a username and a  
password field.

The validation methods, such as validates\_presence\_of, aren’t working.  
Does anyone know why?

* * *

class UsersController \< ApplicationController

def index  
@users = User.all  
end

def show  
@user = User.find(params[:id])  
end

def new  
if session[:user\_id].nil?

```
  if params[:user].nil? #User hasn't filled the form
    @user = User.new
  else #User has filled the form
    user = User.new(params[:user])

    if user.save
      user.salt = rand(1000000000)
      user.password = Digest::MD5.hexdigest(user.salt.to_s +

```

user.password)  
user.save  
flash[:notice] = ‘User was successfully created.’  
session[:user\_id] = user.id  
session[:password] = user.password  
redirect\_to url\_for(:action=\>“index”,:controller=\>“users”)  
else  
render :action =\> “new”  
end  
end

```
else #User is already logged in
  flash[:notice] = 'You are already registered.'
  redirect_to url_for(:action=>"index")
end

```

end

# Some non-related methods removed…

end

* * *

ActionController::Routing::Routes.draw do |map|

map.connect ‘login/’, :controller =\> “users”, :action =\> “login”  
map.connect ‘logout/’, :controller =\> “users”, :action =\> “logout”  
map.connect ‘register/’, :controller =\> “users”, :action =\> “new”

map.resources :users # I want to remove this line in the future…

map.connect ‘:controller/:action/:id’ # I want to remove this line  
in the future…  
map.connect ‘:controller/:action/:id.:format’ # I want to remove  
this line in the future…  
end

* * *

# New user

\<% form\_for :user, :url =\>{:action=\>“new”, :controller=\>“users”} do |  
f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

\<%= f.label :password %\>  
 \<%= f.password\_field :password %\>

\<%= f.submit 'Create' %\>

\<% end %\>

\<%= link\_to ‘Back’, users\_path %\>

* * *

class User \< ActiveRecord::Base  
validates\_presence\_of :name, :password  
end

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 11, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/2 "2009-08-11T21:56:35Z")

</div>

According to the API, you will need to do seperate validations. This  
is due to the fact that if there’s a comma after name, it expects an  
option. :password is not an option.

So:  
class User \< ActiveRecord::Base  
validates\_presence\_of :name  
validates\_presence\_of :password  
end

Hope this helps! If it doesn’t, please check back.

Kind regards,  
Jaap H.  
w. [http://www.relywebsolutions.nl](http://www.relywebsolutions.nl)

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 11, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/3 "2009-08-11T21:57:08Z")

</div>

Gabriel B. wrote:

> Hello. I’m trying to create a form. It has only a username and a  
> password field.
> 
> The validation methods, such as validates\_presence\_of, aren’t working.  
> Does anyone know why?

please submit why you don’t think they are working; You received an  
error message? You checked the db and the record is/ins’t there with  
invalid data? Your form didn’t submit? What???

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 11, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/4 "2009-08-11T21:58:20Z")

</div>

Now it doesn’t submit with blank password.

But it doesn’t display the errors… Do you know why?

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 11, 2009, 9:59pm UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/5 "2009-08-11T21:59:30Z")

</div>

Ilan,

Before, it didn’t worked because the form was submitted even with the  
password field blank.

Now, the problem is that the errors don’t appear.

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 12, 2009, 10:44am UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/6 "2009-08-12T10:44:33Z")

</div>

He might be saving it twice because turning an empty string into MD5  
will give an MD5 hash as well. I’m not sure whether that’s also the  
case in Rails, but it is in PHP. Doing an if with the empty password  
will give an error and not save it at all. Doing an if with a password  
string will save it and then re-save the altered string.

> Now, the problem is that the errors don’t appear.

Where is it that they don’t appear? In your log? Because that sounds  
strange to me. It should say something, even if it’s actually saving  
everything.

It doesn’t give an error in your view because you don’t send any  
errors to your view:

else  
render :action =\> “new”  
end

It does send you back to the “new” view however. Does that happen? If  
you want it to show an error, you should actually send an error to the  
view.

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 12, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/7 "2009-08-12T18:40:30Z")

</div>

Yes, it goes to the view, but the errors doesn’t appear in the view…  
How do I send them?

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 12, 2009, 2:48am UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/8 "2009-08-12T02:48:00Z")

</div>

Gabriel B. wrote:

> Ilan,
> 
> Before, it didn’t worked because the form was submitted even with the  
> password field blank.
> 
> Now, the problem is that the errors don’t appear.

why do you have

if user.save  
…  
…

user.save

Are you trying to save multiple times?

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [August 12, 2009, 9:18pm UTC](https://www.ruby-forum.com/t/form-validation-errors-not-working/173599/9 "2009-08-12T21:18:34Z")

</div>

You might want to try something like this:

else  
flash[:warning] = @user.errors.full\_messages.join(“  
”)  
render :action =\> “new”  
end

and put \<%= flash[:warning] %\> somewhere in your view.

There’s also a way to get your model validations to show in the form,  
but I don’t have an example of how to do such a thing. Of course,  
@user.errors will also be sent to your view if you don’t specify the  
flash like I did above, but I like this way more because you will be  
able to re-use this solution or even include the flash somewhere in  
your layout.

Hope this helps!

Kind regards,  
Jaap H.  
w. [http://www.relywebsolutions.nl](http://www.relywebsolutions.nl)
