# Validating login form

**URL:** <https://www.ruby-forum.com/t/validating-login-form/87338>\
**Category:** Rails\
**Created:** [March 1, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/validating-login-form/87338 "2007-03-01T21:11:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubysharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a5b964/32.png) [@rubysharma](https://www.ruby-forum.com/u/rubysharma)\
**Post date:** [March 1, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/validating-login-form/87338/1 "2007-03-01T21:11:12Z")

</div>

Hi,

when i submit form with blank username and password its not showing  
errors. its only showing unsuccessful why?. by the by how to validate  
html forms with out atbase link…

Thanks in advance…

* * *

login.rhtml

* * *

\<%= error\_messages\_for “user” %\>  
\<%= start\_form\_tag :action=\> “login” %\>

### Login

Login:  
  
\<%= text\_field “user”, “login”, :size =\> 20 %\>

Password:  
  
\<%= password\_field “user”, “password”, :size =\> 20 %\>

\<%= submit\_tag “Submit” %\>

\<%= link\_to ‘Register’, :action =\> ‘signup’ %\> |  
\<%= link\_to ‘Forgot my password’, :action =\> ‘forgot\_password’ %\>

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

* * *

user\_controller.rb

* * *

class UserController \< ApplicationController  
layout “languages”

before\_filter :login\_required, :only  
=\>[‘welcome’,‘change\_password’,‘hidden’]

def signup  
@user = User.new(@params[:user])  
if request.post?  
if @user.save  
session[:user] = User.authenticate(@user.login, @user.password)  
flash[:message] = “Signup successful”  
redirect\_to :action =\> “welcome”  
else  
flash[:warning] = “Signup unsuccessful”  
end  
end

end

def login  
if request.post?  
if session[:user] = User.authenticate(params[:user][:login],  
params[:user][:password])  
flash[:notice] = “Login successful”  
redirect\_to\_stored  
else  
flash[:notice] = “Login Unsuccessful”  
end  
end

end

def logout

session[:user] = nil  
flash[:message] = ‘Logged out’  
redirect\_to :action =\> ‘login’

end

def delete  
end

def edit  
end

def forgot\_password  
if request.post?  
u= User.find\_by\_email(params[:user][:email])  
if u and u.send\_new\_password  
flash[:message] = “A new password has been sent by email.”  
redirect\_to :action=\>‘login’  
else  
flash[:warning] = “Couldn’t send password”  
end  
end  
end

def change\_password  
@user=session[:user]  
if request.post?  
@user.update\_attributes(:password=\>params[:user][:password],  
:password\_confirmation =\> params[:user][:password\_confirmation])  
if @user.save  
flash[:message]=“Password Changed”  
end  
end  
end

def welcome  
end

def hidden  
end

end

* * *

user.rb(model)

* * *

require ‘digest/sha1’  
class User \< ActiveRecord::Base

validates\_length\_of :login, :within =\> 3…40  
validates\_length\_of :password, :within =\> 5…40  
validates\_presence\_of :login, :email, :password,  
:password\_confirmation, :salt  
validates\_uniqueness\_of :login, :email  
validates\_confirmation\_of :password  
validates\_format\_of :email, :with =\>  
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i, :message =\> “Invalid email”

attr\_protected :id, :salt

attr\_accessor :password, :password\_confirmation  
def validate  
errors.add(:login, ‘cannot be the same as away team’) if home\_team\_id  
== away\_team\_id  
end

def self.authenticate(login, pass)  
u=find(:first, :conditions=\>[“login = ?”, login])  
return nil if u.nil?  
return u if User.encrypt(pass, u.salt)==u.hashed\_password  
nil  
end

def password=(pass)  
@password=pass  
self.salt = User.random\_string(10) if !self.salt?  
self.hashed\_password = User.encrypt(@password, self.salt)  
end

def send\_new\_password  
new\_pass = User.random\_string(10)  
self.password = self.password\_confirmation = new\_pass  
self.save  
Notifications.deliver\_forgot\_password(self.email, self.login,  
new\_pass)  
end

protected

def self.encrypt(pass, salt)  
Digest::SHA1.hexdigest(pass+salt)  
end

def self.random\_string(len)  
#generat a random password consisting of strings and digits  
chars = (“a”…“z”).to\_a + (“A”…“Z”).to\_a + (“0”…“9”).to\_a  
newpass = “”  
1.upto(len) { |i| newpass \<\< chars[rand(chars.size-1)] }  
return newpass  
end

end

* * *

* * *
