Module: Reform::Form::ActiveModel::Validations
- Included in:
- Validator
- Defined in:
- lib/reform/form/active_model/validations.rb
Overview
AM::Validations for your form.
Note: The preferred way for validations should be Lotus::Validations, as ActiveModel::Validation’s implementation is old, very complex given that it needs to do a simple thing, is using globals like @errors, and relies and more than 100 methods to be mixed into your form.
Implements ::validates and friends, and #valid?.
Defined Under Namespace
Classes: Validator
Class Method Summary collapse
Instance Method Summary collapse
- #build_errors ⇒ Object
-
#read_attribute_for_validation(name) ⇒ Object
The concept of “composition” has still not arrived in Rails core and they rely on 400 methods being available in one object.
-
#valid? ⇒ Boolean
Needs to be implemented by every validation backend and implements the actual validation.
Class Method Details
.included(includer) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/reform/form/active_model/validations.rb', line 15 def self.included(includer) includer.instance_eval do include Reform::Form::ActiveModel extend Uber::InheritableAttr inheritable_attr :validator self.validator = Class.new(Validator) class << self extend Uber::Delegates delegates :validator, :validates, :validate, :validates_with, :validate_with # Hooray! Delegate translation back to Reform's Validator class which contains AM::Validations. delegates :validator, :human_attribute_name, :lookup_ancestors, :i18n_scope # Rails 3.1. end end end |
Instance Method Details
#build_errors ⇒ Object
32 33 34 |
# File 'lib/reform/form/active_model/validations.rb', line 32 def build_errors Reform::Contract::Errors.new(self) end |
#read_attribute_for_validation(name) ⇒ Object
The concept of “composition” has still not arrived in Rails core and they rely on 400 methods being available in one object. This is why we need to provide parts of the I18N API in the form.
38 39 40 |
# File 'lib/reform/form/active_model/validations.rb', line 38 def read_attribute_for_validation(name) send(name) end |
#valid? ⇒ Boolean
Needs to be implemented by every validation backend and implements the actual validation. See Reform::Form::Lotus, too!
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/reform/form/active_model/validations.rb', line 71 def valid? validator = self.class.validator.new(self) validator.valid? # run the Validations object's validator with the form as context. this won't pollute anything in the form. #errors.merge!(validator.errors, "") validator.errors.each do |name, error| # TODO: handle with proper merge, or something. validator.errors is ALWAYS AM::Errors. errors.add(name, error) end errors.empty? end |