module Reform::Form::Validate
Mechanics for writing to forms in validate
.
Attributes
input_params[R]
Private Class Methods
included(includer)
click to toggle source
# File lib/reform/form/validate.rb, line 72 def self.included(includer) includer.singleton_class.send :attr_accessor, :deserializer_class end
Public Instance Methods
deserialize(params)
click to toggle source
# File lib/reform/form/validate.rb, line 34 def deserialize(params) params = deserialize!(params) deserializer.new(self).from_hash(params) end
validate(params) { |params| ... }
click to toggle source
Calls superclass method
# File lib/reform/form/validate.rb, line 25 def validate(params) # allow an external deserializer. @input_params = params # we want to store these for access via dry later block_given? ? yield(params) : deserialize(params) super() # run the actual validation on self. end
Private Instance Methods
deserialize!(params)
click to toggle source
Meant to return params processable by the representer. This is the hook for munching date fields, etc.
# File lib/reform/form/validate.rb, line 42 def deserialize!(params) # NOTE: it is completely up to the form user how they want to deserialize (e.g. using an external JSON-API representer). # use the deserializer as an external instance to operate on the Twin API, # e.g. adding new items in collections using #<< etc. # DISCUSS: using self here will call the form's setters like title= which might be overridden. params end
deserializer(*args)
click to toggle source
# File lib/reform/form/validate.rb, line 67 def deserializer(*args) # DISCUSS: should we simply delegate to class and sort out memoizing there? self.class.deserializer_class || self.class.deserializer_class = deserializer!(*args) end
deserializer!(source = self.class, options = {})
click to toggle source
Default deserializer for hash. This is input-specific, e.g. Hash, JSON, or XML.
# File lib/reform/form/validate.rb, line 52 def deserializer!(source = self.class, options = {}) # called on top-level, only, for now. deserializer = Disposable::Rescheme.from( source, { include: [Representable::Hash::AllowSymbols, Representable::Hash], superclass: Representable::Decorator, definitions_from: ->(inline) { inline.definitions }, options_from: :deserializer, exclude_options: %i[default populator] # Reform must not copy Disposable/Reform-only options that might confuse representable. }.merge(options) ) deserializer end