# Confirmation fields on forms

**URL:** <https://www.ruby-forum.com/t/confirmation-fields-on-forms/74386>\
**Category:** Rails\
**Created:** [November 6, 2006, 4:03am UTC](https://www.ruby-forum.com/t/confirmation-fields-on-forms/74386 "2006-11-06T04:03:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 6, 2006, 4:03am UTC](https://www.ruby-forum.com/t/confirmation-fields-on-forms/74386/1 "2006-11-06T04:03:59Z")

</div>

I am unsure how to use a confirmation only field in my forms. The  
perfect example is when registering a user, you want them to type their  
password twice but you only want to have one record for it.

The form fields;  
user[password]  
user[password\_confirmation]

form posted;  
user = User.new @params[:user]

Error is thrown because there is no method for password\_confirmation.

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 6, 2006, 4:10am UTC](https://www.ruby-forum.com/t/confirmation-fields-on-forms/74386/2 "2006-11-06T04:10:04Z")

</div>

You just need to add:

```
attr_accessor :password_confirmation

```

to your user model.

c.

Chad wrote:

> I am unsure how to use a confirmation only field in my forms. The  
> perfect example is when registering a user, you want them to type their  
> password twice but you only want to have one record for it.
> 
> The form fields;  
> user[password]  
> user[password\_confirmation]
> 
> form posted;  
> user = User.new @params[:user]
> 
> Error is thrown because there is no method for password\_confirmation.

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 6, 2006, 8:43am UTC](https://www.ruby-forum.com/t/confirmation-fields-on-forms/74386/3 "2006-11-06T08:43:36Z")

</div>

Cayce B. wrote:

> You just need to add:
> 
> ```
> attr_accessor :password_confirmation
> 
> ```
> 
> to your user model.
> 
> c.

Thanks! that works well… kind of…

It works on my User model above just as expected. On another model, it  
doesn’t work as expected…

The form fields;  
bank[bank\_account]  
bank[routing\_number]  
bank[bank\_account\_confirmation]  
bank[routing\_number\_confirmation]

In the model;  
attr\_accessor :bank\_account\_confirmation  
attr\_accessor :routing\_number\_confirmation  
validates\_confirmation\_of :bank\_account  
validates\_confirmation\_of :routing\_number

Testing with values that should pass validation, it fails. Here is the  
failure debugged;  
— &id001 !ruby/object:Bank  
attributes:  
routing\_number: “1”  
account\_id: 3  
bank\_account: “2”  
bank\_account\_confirmation: “2”  
errors: !ruby/object:ActiveRecord::Errors  
base: \*id001  
errors:  
routing\_number:  
- doesn’t match confirmation  
bank\_account:  
- doesn’t match confirmation  
new\_record: true  
routing\_number\_confirmation: “1”

Another oddity is that in the Bank model the errors will not show up by  
printing error\_messages\_for :bank

If I fail the User model, error\_messages\_for :user will print the  
expected messages.

Any clues?

Thanks to all.

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 6, 2006, 8:56am UTC](https://www.ruby-forum.com/t/confirmation-fields-on-forms/74386/4 "2006-11-06T08:56:13Z")

</div>

Chad wrote:

> I am unsure how to use a confirmation only field in my forms. The  
> perfect example is when registering a user, you want them to type their  
> password twice but you only want to have one record for it.
> 
> The form fields;  
> user[password]  
> user[password\_confirmation]
> 
> form posted;  
> user = User.new @params[:user]
> 
> Error is thrown because there is no method for password\_confirmation.

If you add a validates\_confirmation\_of to your model, an acessor for the  
confirmation is created for you. No attr\_accessor needed.

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 7, 2006, 11:05am UTC](https://www.ruby-forum.com/t/confirmation-fields-on-forms/74386/5 "2006-11-07T11:05:22Z")

</div>

deee
