# validating password: when empty, don't save it!

**URL:** <https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725>\
**Category:** rubyonrails-talk\
**Created:** [July 16, 2007, 7:59pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725 "2007-07-16T19:59:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon\_Bogaert](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Leon\_Bogaert](https://discuss.rubyonrails.org/u/Leon_Bogaert)\
**Post date:** [July 16, 2007, 7:59pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/1 "2007-07-16T19:59:03Z")

</div>

Hi,

Users can edit their own passwords in my app. But when they leave both password-fields (\_confirmation) empty, they get "" as their password. Is their a way to get around this problem? This is my userobject when I try to save it:

--- !ruby/object:User attributes: &nbsp;&nbsp;updated\_at: 2007-07-16 21:54:27 &nbsp;&nbsp;id: "2" &nbsp;&nbsp;firstname: Leon &nbsp;&nbsp;lastname: Bogaert &nbsp;&nbsp;password: "" &nbsp;&nbsp;account\_number: 53.76.68.829 &nbsp;&nbsp;email: leon@tim-online.nl &nbsp;&nbsp;created\_at: 2007-03-29 00:24:21 password\_confirmation: ""

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![PatRoy](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@PatRoy](https://discuss.rubyonrails.org/u/PatRoy)\
**Post date:** [July 16, 2007, 8:08pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/2 "2007-07-16T20:08:27Z")

</div>

Add this in your User model:

&nbsp;&nbsp;validates\_presence\_of :password &nbsp;&nbsp;validates\_presence\_of :password\_confirmation &nbsp;&nbsp;validates\_confirmation\_of :password

p.s. I would use the restful\_authentication or act\_as\_authenticated plugins for logins 🙂

---

<div class="post-metadata">

**Author:** ![Leon\_Bogaert](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Leon\_Bogaert](https://discuss.rubyonrails.org/u/Leon_Bogaert)\
**Post date:** [July 16, 2007, 8:56pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/3 "2007-07-16T20:56:07Z")

</div>

PatRoy,

Thanks for your quick reply. I don't use a standard plugin for educational purposes.

When an user does not fill in his or her password it shouldn't give an error. The system should just not update the password. This is (a part of) my usermodel:

&nbsp;&nbsp;MINIMUM\_PASSPHRASE\_LENGTH = 8 &nbsp;&nbsp;MAXIMUM\_PASSPHRASE\_LENGTH = 64

&nbsp;&nbsp;validates\_presence\_of :password, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:on =\> :create, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:message =\> "^Geen wachtwoord ingevuld"

&nbsp;&nbsp;validates\_length\_of :password, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:in =\> MINIMUM\_PASSPHRASE\_LENGTH..MAXIMUM\_PASSPHRASE\_LENGTH, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:if =\> Proc.new { |u| ! u.password.blank? }, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:too\_long =\> "Het wachtwoord mag maximaal %d tekens lang zijn", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:too\_short =\> "^Het wachtwoord moet minimaal %d tekens lang zijn"

&nbsp;&nbsp;validates\_confirmation\_of :password, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:if =\> Proc.new { |u| ! u.password.blank? }, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:message =\> "^De wachtwoorden komen niet overeen"

---

<div class="post-metadata">

**Author:** ![PatRoy](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@PatRoy](https://discuss.rubyonrails.org/u/PatRoy)\
**Post date:** [July 16, 2007, 10:40pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/4 "2007-07-16T22:40:58Z")

</div>

when you save your object ...

---

<div class="post-metadata">

**Author:** ![Leon\_Bogaert](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Leon\_Bogaert](https://discuss.rubyonrails.org/u/Leon_Bogaert)\
**Post date:** [July 18, 2007, 8:31pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/5 "2007-07-18T20:31:35Z")

</div>

Thanks PayRoy!

But I would rather not do that in my controller. It's kind of business logic. So it would be more suitable to place it in my model.

---

<div class="post-metadata">

**Author:** ![Leon\_Bogaert](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Leon\_Bogaert](https://discuss.rubyonrails.org/u/Leon_Bogaert)\
**Post date:** [July 18, 2007, 9:23pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/6 "2007-07-18T21:23:41Z")

</div>

Thanks Patroy! But I would like to implement the code in my model. It's kind of business logic so I would like to implement it in the proper place.

---

<div class="post-metadata">

**Author:** ![Meng](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@Meng](https://discuss.rubyonrails.org/u/Meng)\
**Post date:** [July 18, 2007, 10:00pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/7 "2007-07-18T22:00:44Z")

</div>

Hi I just stumbled on to this

heres my solution:

&nbsp;&nbsp;validates\_presence\_of :password, :password\_confirmation, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:on =\> :update, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:if =\> :req\_password?

def req\_password? &nbsp;&nbsp;!password.blank? end

hope this helps.

---

<div class="post-metadata">

**Author:** ![Wim\_Ceulemans](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@Wim\_Ceulemans](https://discuss.rubyonrails.org/u/Wim_Ceulemans)\
**Post date:** [July 20, 2007, 1:52pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/8 "2007-07-20T13:52:30Z")

</div>

Or just use:

validates\_confirmation\_of :password, &nbsp;&nbsp;:message =\> "must match confirm password", &nbsp;&nbsp;:if =\> Proc.new { |u| !u.password.blank? }

Wim

Meng wrote:

---

<div class="post-metadata">

**Author:** ![Leon\_Bogaert](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@Leon\_Bogaert](https://discuss.rubyonrails.org/u/Leon_Bogaert)\
**Post date:** [July 20, 2007, 9:11pm UTC](https://discuss.rubyonrails.org/t/validating-password-when-empty-dont-save-it/15725/9 "2007-07-20T21:11:04Z")

</div>

Thanks Meng and Wim! But if the password is empty it will be saved to the database (empty). I think I'll use the empty? function to set the password to the old value.

Thanks for the help! I found a topic on tweakers.net that handles this same problem: [http://gathering.tweakers.net/forum/list\_messages/1035239/15](http://gathering.tweakers.net/forum/list_messages/1035239/15)
