Search code examples
ruby-on-railsrubyviewpassword-confirmation

Ruby on Rails: password confirmation doesn't work properly


I 'd like to confirm the password as my pattern then add it to database. I tried to input too many kind of input type, but it doesn't work. I didn't get any error but I can't add anything to my database.

Here is model/user.rb

class User < ActiveRecord::Base
has_secure_password

...

validates :first_name, presence: true
 validates :last_name, presence: true
 validates :email, presence: true,
                   uniqueness: {case_sensitive: false},
                   email: true

 validates :password, presence: true,
            length: {minimum: 6}
end

Email field works without any problem. Here my view page.

<div class="large-12 columns">
        <%= form.label :email %>
        <%= form.email_field :email, required: true,
                                    pattern: :email %>
    <small class="error"> Invalid value</small> 
    </div>

    <div class="large-6 columns">
        <%= form.label :password %>
        <%= form.password_field :password, required: true,
                                            pattern: :password %>
    <small class="error"> Gecerli bir deger giriniz'</small>
    </div>

    <div class="large-6 columns">
        <%= form.label :password_confirmation %>
        <%= form.password_field :password_confirmation, required: true,
                                                    pattern: :password %>
    <small class="error"> Gecerli bir deger giriniz</small>
    </div>

I tried to input more than six characters with/without symbols and numbers. If password field and confirm field are same or not same it doesn't matter every time I get something like that enter image description here

'Gecerli bir deger giriniz' mean: 'Please enter valid value'

Anybody know what I should do?


Solution

  • <%= form.password_field :password, required: true,
                                                pattern: :password %>
    

    By specifying pattern: :password you require user's password to be literally "Password". To specify the length you should use '.{3,}' instead like:

    <%= form.password_field :password, required: true,
                                                pattern: '.{3,}' %>
    

    Watch out! You have the same issue with e-mail, you require user to enter exactly the word "email" into the field. Looks like gem "html5_validators" will bind Rails Model validations with HTML5 input validations and you will not have to set up manually validations on the HTML fields - https://github.com/amatsuda/html5_validators