I will summarize examples etc. mainly as a memorandum of Rails Tutorial. I would appreciate it if you could point out any mistakes.
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.
The basic entry method is as follows.
model
validates :Column name,helper
#If you want to apply to multiple columns
validates :Column name, :Column name, :Column name,helper
presence Make sure that the specified attribute is not empty.
model
validates column name, presence: true
model
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.
model
validates column name, length: {Limits}
model
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.
model
validates :Column name, uniqueness: true
model
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".
model
validates :Column name, acceptance: true
model
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.
model
validates :Column name, confirmation: true
model
validates :email, confirmation: true
numericality Verify that only numbers are used for the attribute.
model
validates column name, numericality: true
model
validates :points, numericality: true
model
#Only integers allowed
validates :age, numericality: { only_integer: true }
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.
model
validates :Column name, format: { with:Limits}
model
#Only alphanumeric characters allowed
validates :password, format: { with: /\A[a-zA-Z]+\z/ }
#Allow only valid email addresses
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[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 |
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.). |
model
#When writing a message directly
validates column name, presence: { message:“Custom error message”}
model
validates :name, presence: { message:"Required fields"}
Active Record Validation-Rails Guide v6 \ .0 https://railsguides.jp/active_record_validations.html
Recommended Posts