0

i've a problem with password confirmation validation

in User Model:

 validates :password, :confirmation => true

and in the view new.html.erb:

 <%= form_for(@user) do |f| %>
   <div class="field">
     <%= f.label :password %><br />
     <%= f.password_field :password %><br />
     <%= f.label :password_confirmation %><br />
     <%= f.password_field :password_confirmation %><br />
   </div>
   <div class="actions">
     <%= f.submit %>
   </div>
 <% end %>

so for any inserted data the validation throw error. i think that there's something wrong with :password_confirmation and RoR received it with nil value is it possibile? how can i do?

3
  • Can you paste the error? Commented Oct 5, 2012 at 12:48
  • Impossible to help you based on what you've posted. We would have to see how you're attempting to assign parameters to your model. Also, you shouldn't be doing this anyways. Use has_secure_password in Rails 3+. Commented Oct 5, 2012 at 12:51
  • it isn't an error... when i inserted the password and the password_confirmation form field and click on 'submit' remain in the same page and there the message error, impossible to save: password and his confirm are different. Commented Oct 5, 2012 at 12:59

1 Answer 1

1

try this

class User < ActiveRecord::Base
  validates :password, :presence =>true, :confirmation =>true
  validates_confirmation_of :password
end

The Controller is intended take the data from the view and try to perform save, this is the code of the view:

<%= form_for(@user) do |f|%>
  <% if @user.errors.any? %>
    ....
    ....
  <% end %>
  <%= f.label :email %><br />
  <%= f.text_field :email %><br />
  <%= f.label :password %><br />
  <%= f.password_field :password %><br />
  <%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %>
  <%= f.submit %>
<% end %>


NOTE: This check is performed only if password_confirmation is not nil, and by default    only on save. 
To require confirmation, make sure to add a presence check for the confirmation attribute:

http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

Sign up to request clarification or add additional context in comments.

2 Comments

i see... but it seems like the first version, doesn't it?
i have tried it. now it give me the error 2 times!! 2 errors prohibited this user from being saved: Password non coincide con la conferma Password non coincide con la conferma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.