Class: HungryForm::Elements::Base::ActiveElement
- Defined in:
- lib/hungryform/elements/base/active_element.rb
Overview
The ActiveElement class is used as a base class for all form fields that can contain values and be validated
Direct Known Subclasses
Instance Attribute Summary collapse
-
#error ⇒ Object
Returns the value of attribute error.
-
#required ⇒ Object
(also: #required?)
Returns the value of attribute required.
-
#value ⇒ Object
Returns the value of attribute value.
Attributes inherited from Element
#attributes, #dependency, #label, #name, #placeholders, #resolver, #visible
Instance Method Summary collapse
- #clear_error ⇒ Object
-
#initialize(name, parent, resolver, attributes = {}, &block) ⇒ ActiveElement
constructor
A new instance of ActiveElement.
-
#invalid? ⇒ Boolean
Element invalid? Performs elemen validation.
- #set_value ⇒ Object
-
#valid? ⇒ Boolean
Element valid? Performs element validation.
-
#validate_rule(attribute, rule) ⇒ Object
Validate a particular validation rule Searches for the validation attribute in the Validator module of the element’s class or in the global Validator module.
Methods inherited from Element
#configuration, #dependency_json
Methods included from Hashable
Constructor Details
#initialize(name, parent, resolver, attributes = {}, &block) ⇒ ActiveElement
Returns a new instance of ActiveElement.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hungryform/elements/base/active_element.rb', line 12 def initialize(name, parent, resolver, attributes = {}, &block) super clear_error if parent.visible? self.required = @attributes[:required] || false else self.required = false end # Determine validation attributes # They can be only the methods of the HungryForm::Validator class # or methods of the Validator module of the current class methods = HungryForm::Validator.singleton_class.instance_methods(false) if self.class.const_defined?(:Validator) methods += self.class.const_get(:Validator).instance_methods(false) end @validation_rules = @attributes.select { |key, _| methods.include?(key) } @attributes.delete_if { |key, _| @validation_rules.key?(key) } set_value end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
7 8 9 |
# File 'lib/hungryform/elements/base/active_element.rb', line 7 def error @error end |
#required ⇒ Object Also known as: required?
Returns the value of attribute required.
7 8 9 |
# File 'lib/hungryform/elements/base/active_element.rb', line 7 def required @required end |
#value ⇒ Object
Returns the value of attribute value.
7 8 9 |
# File 'lib/hungryform/elements/base/active_element.rb', line 7 def value @value end |
Instance Method Details
#clear_error ⇒ Object
86 87 88 |
# File 'lib/hungryform/elements/base/active_element.rb', line 86 def clear_error self.error = '' end |
#invalid? ⇒ Boolean
Element invalid? Performs elemen validation
57 58 59 |
# File 'lib/hungryform/elements/base/active_element.rb', line 57 def invalid? !valid? end |
#set_value ⇒ Object
82 83 84 |
# File 'lib/hungryform/elements/base/active_element.rb', line 82 def set_value self.value = resolver.params[name] || attributes.delete(:value) end |
#valid? ⇒ Boolean
Element valid? Performs element validation
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/hungryform/elements/base/active_element.rb', line 39 def valid? clear_error return true unless visible? @validation_rules.each do |key, rule| validate_rule(key, rule) unless error.empty? self.error = "This field #{error}" return false end end true end |
#validate_rule(attribute, rule) ⇒ Object
Validate a particular validation rule Searches for the validation attribute in the Validator module of the element’s class or in the global Validator module
Sample: text_field :txt, :required => true will search for the “required” singleton method in the HungryForm::Elements::TextField::Validator and in the HungryGorm::Validator
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/hungryform/elements/base/active_element.rb', line 70 def validate_rule(attribute, rule) if self.class.const_defined?(:Validator) validator = self.class.const_get(:Validator) end if validator.nil? || !validator.singleton_class.instance_methods(false).include?(attribute) validator = HungryForm::Validator end self.error = validator.send(attribute, self, rule) || '' end |