14
13

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 5 years have passed since last update.

Active Recordでバリデーションの種類 Rails

Last updated at Posted at 2013-08-22

presence: true

空白禁止

allow_blank: true

空の検証をスキップさせるときに使用する

:length => { :maximum => 20 }

入力長さを指定する、上記の例では20文字まで

:uniqueness => true

データの内容がユニークである事、つまりデータベースに同じ内容のデータがない事
名前やmailは重複しないようにとかに使う

:numericality => only_integer

整数のみ

:format => { :with => /regex/ }

正規表現にマッチする

:confirmation => true

パスワードなどの確認入力項目が、比較元項目と一致しているかチェックを行う

:exclusion => { :in => %w(ruby python perl ) }

指定された値が含まれないことをチェックする

:inclusion => { :in => %w(small medium large) }

指定された値が含まれることをチェックする
他の例: validates :lock, inclusion: { allow_blank: true, in: %w(0 1 2) }

参考サイト
http://wiki.usagee.co.jp/ruby/rails/RailsGuides%E3%82%92%E3%82%86%E3%81%A3%E3%81%8F%E3%82%8A%E5%92%8C%E8%A8%B3%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F%E3%82%88/Active%20Record%20Validations%20and%20Callbacks#q8915bf2

14
13
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
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?