# Password validation

**URL:** <https://www.ruby-forum.com/t/password-validation/86250>\
**Category:** Rails\
**Created:** [February 20, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/password-validation/86250 "2007-02-20T23:27:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [February 20, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/password-validation/86250/1 "2007-02-20T23:27:19Z")

</div>

i’m using the authentication chapter in the rails recipes book to help  
me out, but i ran into a snag…

it uses password\_hash and password\_salt as the fields for storing the  
database info. but in the model, there is a password=(pass) method  
that does the work for generating the salt and the encryption.

since “password” isn’t one of the database fields, i’m having  
validation problems. i have a ‘validates\_presence\_of’ for the password  
method which works fine, but my problem comes when i am updating a  
User record that doesn’t have a password field in the form. i am still  
getting the “password can’t be blank” error.

what’s the best workaround for this so i can only use the validations  
on “password” when i am updating the password.

i hope that makes sense…

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [February 21, 2007, 2:23am UTC](https://www.ruby-forum.com/t/password-validation/86250/2 "2007-02-21T02:23:32Z")

</div>

actually that did help out alot. i didn’t find the answer i was  
looking for in the article, but someone had the same problem as i did  
in the comments and posted their solution. they just wanted validation  
to work if there was a password field in the form, like if they wanted  
to change just their name or email address.

they put this in their model:

validates\_presence\_of :password, :if =\> :password\_required?

and further down had something like this:

protected  
def password\_required?  
!password.nil?  
end

i think i read that this is similar to the way that mephisto works.

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [February 20, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/password-validation/86250/3 "2007-02-20T23:46:21Z")

</div>

This is a great article:  
[http://www.aidanf.net/rails\_user\_authentication\_tutorial](http://www.aidanf.net/rails_user_authentication_tutorial)  
It does authentications like the rails recipe but it explains the  
validation process for password.

I implemented authentication by following this article and it worked  
great.

Hope that helps - K

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [September 11, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/password-validation/86250/4 "2007-09-11T15:46:36Z")

</div>

Kim wrote:

> This is a great article:  
> [http://www.aidanf.net/rails\_user\_authentication\_tutorial](http://www.aidanf.net/rails_user_authentication_tutorial)  
> It does authentications like the rails recipe but it explains the  
> validation process for password.

That was very useful, thanks.  
It answered my question about the best place to put the password  
validation (i.e. in the user model, despite the password not being in  
the database).
