Ruby on Rails :検証エラー


検証


検証は、データベースに保存されたデータが有効であることを保証するために柵で使用されます.それはさらに道をさらに下の問題を防ぐことができます.ユーザーがフォームを入力する場合など、それは彼らが有効なデータでそれを記入することが重要です-彼らの電話番号を確認するように10桁の長さが確認されます.これは多くのデバッグと頭痛を保存します.そして、それが使用されるまで、その問題が持続するのを防ぎます.
オブジェクトが有効かどうかをチェックする方法がいくつかあります.Railsでは、アクティブなレコードオブジェクトを保存する前に検証が実行され、エラーが発生した場合は保存されません.ユーザーが9桁で電話番号を入力しようとするならば、合法化はそれが有効でないと認めて、ユーザーにエラーメッセージを与えて、データベースの情報を保存しません.
我々は有効な使用できますか?または無効ですか?これをチェックする.
class Person < ApplicationRecord
   validates :phone, presence: true, length:{ minimum, 10 }
end
IRB/consoleセッションを開き、新しいインスタンスメソッドを保存することで、Personクラスの妥当性をテストできます.
irb> Person.create(phone: "555-321-112).invalid?
=> true
irb> Person.create(phone: "555-321-112).valid?
=> false
irb> Person.create(phone: "555-321-1128).valid?
=> true

有効?エラーが見つかった場合、エラーが発生した場合にtrueを返し、エラーが発生した場合にfalseを返します.無効?が偽で、エラーがない場合はエラーを返します.

妥当性検査エラー


我々は自分の検証を実行することができます私たちのコードを書くときに有効性を確認してください.アクティブレコードが妥当性検査を完了した後、エラーが発生する可能性のあるエラーをエラーインスタンスメソッドを通して確認できます.オブジェクトが有効であるためには、検証の実行後にエラーのコレクションは空でなければなりません.我々はこれを行うためにエラーコレクションを通過します.

エラーコレクション


Error :エラーを含むクラスのインスタンスを返します.これにより、エラーの別の詳細を調べることができます.エラーに対する一般的な方法は以下の通りです.メッセージ.FullTheseメッセージ(別名、別名)、そして.(属性)
irb> person = Person.new
irb> person.valid?
=> false
irb> person.errors.message
=> "is too short(minimum is 10 characters)"
irb> person.errors.full_messages
=> ["Phone is too short (minimum is 10 characters)"]
irb> person.errors.full_messages_for(:phone)
=> ["is too short (minimum is 10 characters)"]

irb> person = Person.new(phone: "555-643-2342")
irb> person.valid?
=> true
irb> person.errors.full_messages
=> []
ERROR []:オブジェクトの特定の属性を確認するには、Error [: attribute ]を実行できます.それは特定の属性のためにエラーメッセージストリングの配列を返します、そして、誤りがないならば、それは空に戻ります.
irb> person = Person.new(phone: "555-123-123")
irb> person.valid?
=> false
irb> person.errors[:phone]
=> ["is too short (minimum is 10 characters)"]

irb> person = Person.new(phone: "555-123-1234")
irb> person.valid?
=> true
irb> person.errors[:name]
=>[]

エラー.where :エラーオブジェクトの配列を返します.ちょうどメッセージより多くの情報が欲しいとき、我々はこれをエラー[]と比較して使用します.オブジェクトから、さらに詳しい情報を得るためにそれを見ることができます.
irb> person = Person.new
irb> person.valid?
=>false

irb> person.errors.where(:phone)
=>[...] #all errors for :phone attribute. Both presence and length errors

irb> person.errors.where(:phone, :too_short) 
=> [...] # :too_short errors for :phone attribute

irb> person.errors.where(:phone).first.attribute
=> :name
irb> person.errors.where(:phone).first.type
=> :too_short
irb> person.errors.where(:phone).first.options[:count]
=> 10

エラー.add :属性、エラータイプおよび追加オプションハッシュを取ることによって、エラーオブジェクトを作成します.これらは、独自のバリデータを作成している場合に便利です
class Person < ApplicationRecord
  validate do |person|
     errors.add :phone, :too_long, message: "has too many numbers"
  end
end
irb> person = Person.create
irb> person.errors.where(:phone).first.type
=> :too_long
irb> person.errors.where(:phone).first.full_message
=> "Phone has too many numbers

Error [: base ]:エラーと同様に特定の属性に関連するのではなく、全体としてオブジェクト状態にエラーを加えることができます.追加.また、独自の検証を書いているときにも良い.
class Person < ApplicationRecord
  validate do |person|
    errors.add :base, :invalid, message: "This phone number is invalid because ..."
  end
end
irb> person = Person.create
irb> person.errors.where(:base).first.full_message
=> "This phone number is invalid because ..."

エラー.クリア:意図的にエラーコレクションをクリアしたい場合に使用します.実際にオブジェクトを有効にしていませんが、エラーコレクションは空ですが、保存したり、そのメソッドを検証したりすると、妥当性検査が再び実行されます.
irb> person = Person.new
irb> person.valid?
=> false
irb> person.errors.empty?
=> false
irb> person.errors.clear
irb> person.errors.empty?
=> true

エラー.size :オブジェクトのエラーの総数を返します.
irb> person = Person.new
irb> person.valid?
=> false
irb> person.errors.size
=> 2 #phone does not exist, and is less than minimum length.

irb> person = Person.new(phone: "555-123-123")
irb> person.valid?
=> false
irb> person.errors.size
=> 1 # phone exists, but below minimum length.

カスタムメソッドに関連するエラー


我々は、彼らが無効であるときに我々のコレクションにエラーを追加することができます.我々は条件つきでエラーを出すことができる.
validate :phone_cannot_be_more_than_number

def phone_cannot_be_more_than_number
  if phone.present? && phone length > 11
  errors.add(:phone, "number is too long")

カスタムメソッドに追加されると、有効にするたびに実行されますか?または、オブジェクトを保存します.

ソース


https://guides.rubyonrails.org/active_record_validations.html#custom-methods
https://api.rubyonrails.org/v6.1.3/classes/ActiveModel/Errors.html#method-i-messages
https://guides.rubyonrails.org/active_record_validations.html#working-with-validation-errors