3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

バリデーションの種類について Rails

Posted at

多分よく使うであろうバリデーション

#入力必須
presence: true

#一意性(被ったらエラー)
uniqueness: true

#最小文字数
length: { minimum: n }

#最大文字数
length: { maximum: n }

#パスワードは英数字を含める
PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?[\d])[a-z\d]+\z/i.freeze
validates_format_of :password

#email @を含める
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
format: { with: VALID_EMAIL_REGEX }

#全角カタカナ
format: { with: /\A[ァ-ヶー-]+\z/, message: '全角カタカナで入力して下さい' }

#全角カタカナ、ひらがな、漢字
format: { with: /\A[ぁ-んァ-ン一-龥々]+\z/, message: '全角ひらがな、全角カタカナ、漢字で入力して下さい' }

参考文献
https://qiita.com/h1kita/items/772b81a1cc066e67930e
https://qiita.com/necojackarc/items/cad2d4eb80f0629ad196

Railsガイドもわかりやすいです。
https://railsguides.jp/active_record_validations.html

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?