Skip to content

Instantly share code, notes, and snippets.

@kawasaki-shohei
Last active October 14, 2017 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawasaki-shohei/8db09aad217c0749d0a7052dd5712c36 to your computer and use it in GitHub Desktop.
Save kawasaki-shohei/8db09aad217c0749d0a7052dd5712c36 to your computer and use it in GitHub Desktop.
Rails5でバリデーション
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