Successfully reported this slideshow.
Your SlideShare is downloading. ×

ActiveRecord Validations, Season 2

More Related Content

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

ActiveRecord Validations, Season 2

  1. 1. Ror lab. season 2 - the 6th round - Active Record Validations September 15th, 2012 Hyoseong Choi ROR Lab.
  2. 2. Contents Validations Callbacks • The Object Life Cycle • Callbacks Overview • Validations Overview • Available Callbacks • Validation Helpers • Running Callbacks • Common Validation • Skipping Callbacks Options • Halting Execution • Conditional Validation • Relational Callbacks • Custom Validations • Conditional Callbacks • Working with Validation • Callback Classes Errors • Observers • Displaying Validation Errors • Transaction Callbacks in the View ROR Lab.
  3. 3. Validation Levels • Native DB constraints - DB-dependent • Client-side validations - javascript ? • Controller-level validations - keep skinny • Model-level validations - the best way ROR Lab.
  4. 4. “new” method • A new object instantiated vs. persisted? • not yet to store to database : new_record? • no validation called, but ... ROR Lab.
  5. 5. Validation Time Point ActiveRecord Web Server Model DB Validation save create update clients Model-level validations ROR Lab.
  6. 6. Validation Event Triggering validations Skipping validations • .create • .decrement! • .create! • #decrement_counter • .save • .increment! • .save! • #increment_counter • #update • .toggle! • .update_attributes • .touch • .update_attributes! • #update_all • .update_attribute deprecated • .update_column • #update_counters Ref.: ActiveRecord::Persistence ActiveRecord::CounterCache ROR Lab.
  7. 7. Call Validations • valid? or invalid? ROR Lab.
  8. 8. valid? vs Errors By definition, an object is valid if this collection is empty after running validations. ROR Lab.
  9. 9. Validation Helpers • acceptance • validates_associated • confirmation • validates_each • exclusion • validates_with • format • inclusion :on • save(default) • create • length • update • numericality :message • presence • uniqueness ROR Lab.
  10. 10. Common Options :allow_nil - nil :allow_blank - nil or whitespace :message - overriding default error message :on - :create / :update / :save 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 Grouping conditions : with_options ROR Lab.
  12. 12. Custom Validations • Custom validators modules : inherited from Two ★ ActiveModel::Validator ★ ActiveModel::EachValidator ★ Get the “record” argument as a parameter • Custom validation methods • Custom validation helpers ROR Lab.
  13. 13. Working with Validation Errors • errors • errors.messages • errors.full_messages( or errors.to_a) • errors[:attr] : for a specific attribute • errors.add(:attr, message)(or errors[:attr]=) • errors[:base] : object’s state as a whole • errors.clear : intentionally to clear • errors.size : count of errors 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. Validation Errors : https://github.com/joelmoss/dynamic_form Error CSS #error_explanation #error_explanation h2 #error_explanation p <%= form_for(@product) do |f| %> <% if @product.errors.any? %> <div id="error_explanation"> #error_explanation ul li <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved: </h2> <ul> <% @product.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> .field_with_errors generated by scaffold apps/assets/stylesheets/scaffolds.css.scss ROR Lab.
  16. 16. Validation Errors : https://github.com/joelmoss/dynamic_form Error CSS #error_explanation <%= form_for(@product) do |f| %> #error_explanation h2 <% if @product.errors.any? %> #error_explanation p <div id="error_explanation"> <h2><%= pluralize(@product.errors.count, "error") %> #error_explanation ul li prohibited this product from being saved: </h2> <ul> <% @product.errors.full_messages.each do |msg| %> <li><%= msg %></li> .field_with_errors <% end %> </ul> </div> <% end %> generated by scaffold apps/assets/stylesheets/scaffolds.css.scss ROR Lab.
  17. 17. Validation Errors : https://github.com/joelmoss/dynamic_form Error CSS apps/assets /stylesheets /scaffolds.css.scss #error_explanation .field_with_errors { #error_explanation h2 padding: 2px; background-color: red; #error_explanation p display: table; } #error_explanation { #error_explanation ul li width: 450px; border: 2px solid red; padding: 7px; padding-bottom: 0; margin-bottom: 20px; background-color: #f0f0f0; h2 { .field_with_errors text-align: left; font-weight: bold; padding: 5px 5px 5px 15px; font-size: 12px; margin: -7px; margin-bottom: 0px; background-color: #c00; color: #fff; } ul li { generated by scaffold font-size: 12px; list-style: square; ROR Lab.
  18. 18. Validation Errors : ‘dynamic_form’ • f.error_messages or • error_messages_for :product <%= form_for(@product) do |f| %> <%= f.error_messages %> <% end %> ROR Lab.
  19. 19. Validation Errors : https://github.com/joelmoss/dynamic_form ‘dynamic_form’ generated by dynamic_form r_ tag e :h ead :header_message :message <%= f.error_messages :header_message => "Invalid product!",   :message => "You'll need to fix the following fields:",   :header_tag => :h3 %> ROR Lab.
  20. 20. Validation Errors : https://github.com/joelmoss/dynamic_form Error HTML config/initializers/custom_error_message_html.rb field object ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|   errors = Array(instance.error_message).join(',') unless html_tag =~ /^<label/    %(#{html_tag}<span class="validation-error">&nbsp;#{errors}</span>).html_safe else %(#{html_tag}).html_safe end end #{html_tag} .validation-error ROR Lab.
  21. 21. Validation Errors : https://github.com/joelmoss/dynamic_form Error HTML <% if @product.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2> </div> <% end %> ROR Lab.
  22. 22. 감사합니다.

Editor's Notes

  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

×