I often see services that are designed to enter the password twice for confirmation when entering the password. When I tried to implement the same thing, I could easily implement it by using the helper method of validation in Rails called confimartion, so I will leave it as a memorandum.
Use this when you need to get an exact match for what you receive in two text fields. For example, suppose you use a confirmation field in your email address or password. This validation helper creates virtual attributes. The name of the attribute will be the attribute name you want to check with "_confirmation" added. https://railsguides.jp/active_record_validations.html Quoted from above.
In short, I think there is no problem in recognizing that it is a method that applies validation that can only be registered with the same data in the two forms.
class User < ApplicationRecord
validates :password, confirmation: true
end
Add the above description to the column to which you want to apply the confimartion method to the model. This completes validation.
= form_for(@user, url: user_registration_path) do |f|
= f.text_field :password
= f.text_field :password_confirmation
end
For the attribute name of the second argument of the text field, add _confimartion to the attribute name you want to check with confimartion. The model and view actually described are as follows.
If the values entered in the two forms are not the same, it will be validated! If you want to add validation, just add validation to the attribute you want to check.
When first implemented ・ Create an attribute for confirmation -Create a conditional branch to check if the values entered in the two forms are the same ・ If they are the same, save I thought I had to write code like this, so using confirmation made the implementation very easy.
https://railsguides.jp/active_record_validations.html
Recommended Posts