# User validation of password

**URL:** <https://www.ruby-forum.com/t/user-validation-of-password/198534>\
**Category:** Rails\
**Created:** [November 3, 2010, 3:17am UTC](https://www.ruby-forum.com/t/user-validation-of-password/198534 "2010-11-03T03:17:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 3, 2010, 3:17am UTC](https://www.ruby-forum.com/t/user-validation-of-password/198534/1 "2010-11-03T03:17:28Z")

</div>

I have a User model that validates password presence:  
validates :password, :presence =\> true,  
:confirmation =\> true,  
:length =\> { :within =\> 6…40 },

Password is never stored in the db. Now the problem is that the user  
has a:  
belongs\_to :owner, :polymorphic =\> :true

This means it contains a key to the owner. When setting the owner, the  
user.save will be called and the password validation will fail as  
password is not present at that time. I also have several other data  
like user.personal\_message that I was updating through save, :validate  
=\> false.

How can I still keep my password validations and be able to internally  
call save and run all but those validations(I.E I only want the  
password validations to run when user edits the user or creates a new  
user). Its really important to validate when editing( on update)

---

<div class="post-metadata">

**Author:** ![dpal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dpal](https://www.ruby-forum.com/u/dpal)\
**Post date:** [November 3, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/user-validation-of-password/198534/2 "2010-11-03T18:02:42Z")

</div>

On Nov 2, 2010, at 7:16 PM, dpal wrote:

> user.save will be called and the password validation will fail as  
> password is not present at that time. I also have several other data  
> like user.personal\_message that I was updating through save, :validate  
> =\> false.
> 
> How can I still keep my password validations and be able to internally  
> call save and run all but those validations(I.E I only want the  
> password validations to run when user edits the user or creates a new  
> user). Its really important to validate when editing( on update)

Only validate :password :on =\> :create. If they are editing their user  
account and change only their name for example, you don’t want or need  
to validate their password as they aren’t changing it.

Also, only validate it’s confirmation/length if it or it’s confirmation  
exists.

-philip
