[Rails] Customize devise validation

At first

When using devise to implement a new user registration feature I have given validation, but validation is not applied and I struggled to implement it, so I will leave it as a memorandum.

devise default validation

devise is a gem that makes it easy to implement the authentication function, but it was difficult for me as a beginner to understand what I was doing invisible because the gem worked on its own. This was the reason why it was not applied even though it was validated.


 = form_for(@user, url: user_registration_path) do |f|  
 = f.password_field: password, class: "main__box__bottom__content__group2__form", placeholder: "7 or more single-byte alphanumeric characters",

The conclusion was that the form used password_field, so Rails seems to recognize this input form as a password and automatically perform a simple validation.

devise customization procedure

1. Modify the form (to the proper form)

Fixed password_fileld to text_field.

 = form_for(@user, url: user_user_registration_path) do |f|  
  = f.text_field :password
end

This recognizes it as a password column.

2. devise validation settings

However, if you validate the addition of the password column of the model, it will be double validated, so Adding devise validation: Remove validatable.

class User < ApplicationRecord
 devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :validatable 
 # ↑ Delete.
end

Now devise validation is no longer required and password is now given your own customized validation.

Summary

-Validation has a priority to refer to Validation given to the text field> devise> Self-made validation It has become. -If you want to apply only custom validation, use text_field and delete: validatable of devise.

References

https://qiita.com/kouheiszk/items/215afa01eeaadbd99340

Recommended Posts

[Rails] Customize devise validation
[Rails] devise
[rails] Set validation
[Rails] Introducing devise
rails + devise + devise_token_auth
[Rails5] Rspec -validation-
[Rails 6] Validation by belongs_to
Rails: Japanese localization of validation messages including devise
Handle devise with Rails
[Rails] devise introduction method
[rails] About devise defaults
[Rails] Cancel / change the default password validation of devise
[Rails] How to edit and customize devise view and controller
What is Rails gem devise?
[Rails] Japanese longest surname (validation)
[Rails] gem devise installation flow
[Rails] How to install devise
(For beginners) [Rails] Install Devise
[Rails] Add column to devise
How to write Rails validation
[Rails] How to use validation
Ruby on Rails validation summary
[rails] error during devise installation
Memorandum [Rails] User authentication Devise
[Rails] How to use gem "devise"
Summary of rails validation (for myself)
[Rails] How to use devise (Note)
[Rails] Session timeout setting in devise
[rails] Login screen implementation in devise
[Devise] rails memo basic setting initial setting
[Rails] Validation settings and Japanese localization
[Rails] [Devise] Edit profile without password
Ruby On Rails devise routing conflict
[Rails] Add strong parameters to devise
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
Create My Page with Rails devise
[Rails] Manage multiple models using devise gem
[Rails] Function restrictions in devise (login / logout)
Rails g devise: install cannot be executed!
[Rails] Use validation on a specific controller
[Note] How to use Rails 6 Devise + cancancan
[Rails] Implementation of validation that maintains uniqueness
[Rails] How to translate devise into Japanese
Rails validation and null: false Personal notes