Ruby on Rails validation summary


I will summarize examples etc. mainly as a memorandum of Rails Tutorial. I would appreciate it if you could point out any mistakes.

What is validation?

Verifying the state of an object before saving it to the database. Verify that the value entered is not invalid. For example, you can prevent empty data from being saved, or you can increase the number of characters.

Basic writing

The basic entry method is as follows.


validates :Column name,helper

#If you want to apply to multiple columns
validates :Column name, :Column name, :Column name,helper

Main validation helpers

presence Make sure that the specified attribute is not empty.


validates column name, presence: true


validates :name, presence: true

#If you want to apply to multiple columns
validates :name, :login, :email, presence: true

length Validate the length of the attribute value. There are many options and you can specify different length limits.


validates column name, length: {Limits}


  validates :passward, length: { minimum: 5 }        #5 characters or more
  validates :name, length: { maximum: 50 }           #20 characters or less
  validates :passward, length: { in: 3..10  }        #3 to 10 characters
  validates :registration_number, length: { is: 6 }  #Only 6 characters allowed

uniqueness Verify that the value of the attribute is unique and unique.


validates :Column name, uniqueness: true


validates :name, uniqueness: true

acceptance Verify that the checkbox is selected when the form is submitted. It is used when you need to check "I accept the terms of service".


validates :Column name, acceptance: true


validates :terms_of_service, acceptance: true

confirmation Validates that the values entered in multiple forms match exactly. It is used to match the values of the email address and the confirmation email address.


validates :Column name, confirmation: true


validates :email, confirmation: true

numericality Verify that only numbers are used for the attribute.


validates column name, numericality: true


validates :points, numericality: true


#Only integers allowed
validates :age, numericality: { only_integer: true }
Main options
option Overview
:only_integer Must be an integer
:equal_to Must be equal to the specified value

format Verify by testing whether the regular expression given by the with option matches the attribute value.


validates :Column name, format: { with:Limits}


#Only alphanumeric characters allowed
validates :password, format: { with: /\A[a-zA-Z]+\z/ } 

#Allow only valid email addresses
 VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
 validates :email, format: { with: VALID_EMAIL_REGEX }

Regular expressions like the one above to allow only valid email addresses are based on the table below.

Regular expressions Overview
z\d-.]+.[a-z]+\z/i Full regular expression
/ Indicates the start of a regular expression
\A The beginning of the string
[\w+-.]+ Alphanumeric, underscore (_),plus(+),hyphen(-),Dot(.) At least one character or more
@ At sign
[a-z\d-.]+ Repeat at least one letter of lowercase letters, numbers, hyphens, or dots
. Dot
[a-z]+ Repeat at least one lowercase letter
\z End of string
/ Indicates the end of a regular expression
i Ignore case option

Common options

1 2
:message You can specify the error message that will be displayed when validation fails. If not specified, the default message is displayed.
:allow_nill If the target value is nil, validation will be skipped.
:allow_blank The value of the attribute is blank?Validation passes if: (nil, empty string, etc.).


#When writing a message directly
validates column name, presence: { message:“Custom error message”} 


validates :name, presence: { message:"Required fields"}


Active Record Validation-Rails Guide v6 \ .0

Recommended Posts

Ruby on Rails validation summary
Ruby on Rails Overview (Beginner Summary)
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Validation settings for Ruby on Rails login function
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails Basic Memorandum
Ruby on Rails Refactoring method example summary around MVC
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
[Ruby on Rails] Read try (: [],: key)
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
Ruby on Rails for beginners! !! Summary of new posting functions
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
[Ruby on Rails] About bundler (for beginners)
part of the syntax of ruby ​​on rails
Tailwind on Rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Ruby on Rails for beginners! !! Post list / detailed display function summary
Ruby on Rails in Visual Studio Codespaces
[Rails] Use validation on a specific controller
[Ruby on Rails] Follow function implementation: Bidirectional
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
Deploy to Heroku [Ruby on Rails] Beginner
[Ruby on Rails] Image slideshow using Skippr
Preparing to introduce jQuery to Ruby on Rails