The document summarizes Active Record validations and callbacks in Ruby on Rails. It covers the object lifecycle, an overview of validations and callbacks, validation helpers like presence and uniqueness, common validation options, conditional validation, custom validations, working with validation errors, and displaying errors in views. It also discusses callback classes, observers, and transaction callbacks.
11. Conditional Validation
:if and :unless
• A Symbol : a method name
• A String : a really short condition
• A Proc : an inline condition
ROR Lab.
12. Custom Validations
• Inherited from Two modules
★ ActiveModel::Validator
★ ActiveModel::EachValidator
★ Get the “record” argument as a
parameter
★ Custom methods & helpers
ROR Lab.
13. Working with Validation
Errors
• errors.messages
• errors[:attr] : for a specific attribute
• errors.messages
• errors.full_messages( or errors.to_a)
• errors.add(:attr, message)(or errors[:attr]=)
• errors[:base] : object’s state as a whole
• errors.clear : intentionally to clear
• errors.size : count of errrors
ROR Lab.
14. Displaying Validation
Errors in the View
★ gem ‘dynamic_form’ ★ Error Messages CSS
.field_with_errors
#errorExplanation
#errorExplanation h2
#errorExplanation p
#errorExplanation ul li
ROR Lab.