Active Record Validations, Season 1

3,161 views
3,090 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,161
On SlideShare
0
From Embeds
0
Number of Embeds
2,459
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Active Record Validations, Season 1

    1. 1. The 5th Round of ROR Lab. Active RecordValidations and Callbacks January 14th, 2012 Hyoseong Choi ROR Lab.
    2. 2. Contents• The Object Life Cycle • Callbacks Overview• Validations Overview • Available Callbacks• Validation Helpers • Running Callbacks• Common Validation Options • Skipping Callbacks• Conditional Validation • Halting Execution• Custom Validations • Relational Callbacks• Working with Validation • Conditional Callbacks Errors • Callback Classes• Displaying Validation Errors • Observers in the View • Transaction Callbacks ROR Lab.
    3. 3. Contents 6th round• The Object Life Cycle • Callbacks Overview• Validations Overview • Available Callbacks• Validation Helpers • Running Callbacks• Common Validation Options • Skipping Callbacks• Conditional Validation • Halting Execution• Custom Validations • Relational Callbacks• Working with Validation • Conditional Callbacks Errors • Callback Classes• Displaying Validation Errors • Observers in the View • Transaction Callbacks ROR Lab.
    4. 4. Validation Time Point Web Server ControllerClient CRUD Validation DB ROR Lab.
    5. 5. Validation EventTriggering validations Skipping validations create • decrement!• create! • decrement_counter• save • increment!• save! • increment_counter• update • toggle!• update_attributes • touch• update_attributes! • update_all • update_attribute • update_column • update_counters ROR Lab.
    6. 6. “new” method• no validation called. ROR Lab.
    7. 7. Call Validations• valid? or invalid? ROR Lab.
    8. 8. valid? vs ErrorsBy definition,an object is valid if this collection is emptyafter running validations. ROR Lab.
    9. 9. Validation Helpers• acceptance • uniqueness• validates_associated • validates_with• confirmation • validates_each• exclusion• format• inclusion• length• numericality• presence ROR Lab.
    10. 10. Common Options:allow_nil:allow_blank:message:on ROR Lab.
    11. 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. 12. Custom Validations• Inherited from Two modules ★ ActiveModel::Validator ★ ActiveModel::EachValidator ★ Get the “record” argument as a parameter★ Custom methods & helpers ROR Lab.
    13. 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. 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.
    15. 15. 감사합니다.

    ×