Last active
October 14, 2017 07:01
-
-
Save kawasaki-shohei/8db09aad217c0749d0a7052dd5712c36 to your computer and use it in GitHub Desktop.
Rails5でバリデーション
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Contact < ApplicationRecord | |
validates :name, presence: true, length:{ maximum: 255, too_long: "最大%{count}文字まで使用できます"} #データ型がcharacter varyingだから255文字まで | |
validates :content, presence: true | |
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i #メールアドレスのパターンを定数に格納 | |
validates :email, | |
presence: true, | |
length:{ maximum: 255, too_long: "最大%{count}文字まで使用できます"}, | |
format: { with: VALID_EMAIL_REGEX, message: "が適切ではありません"} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment