lib/hungryform/elements/base/active_element.rb in hungryform-0.0.4 vs lib/hungryform/elements/base/active_element.rb in hungryform-0.0.6
- old
+ new
@@ -1,53 +1,61 @@
module HungryForm
- module Elements::Base
- # The ActiveElement class is used as a base class for all
- # form fields that can contain values and be validated
- class ActiveElement < Element
- attr_accessor :error, :value, :required
- alias_method :required?, :required
+ module Elements
+ module Base
+ # The ActiveElement class is used as a base class for all
+ # form fields that can contain values and be validated
+ class ActiveElement < Element
+ attr_accessor :error, :value, :required
+ alias_method :required?, :required
- hashable :required, :value, :error
+ hashable :required, :value, :error
- def initialize(name, parent, resolver, attributes = {}, &block)
- super
+ def initialize(name, parent, resolver, attributes = {}, &block)
+ super
- clear_error
+ clear_error
- # Leave only the attributes that are being methods of the HungryForm::Validator class
- @validation_rules = @attributes.select { |key, _| HungryForm::Validator.respond_to?(key) }
+ if parent.visible?
+ self.required = @attributes[:required] || false
+ else
+ self.required = false
+ end
- if parent.visible?
- self.required = @attributes[:required] || false
- else
- self.required = false
+ # Leave only the attributes that are being methods of the HungryForm::Validator class
+ @validation_rules = @attributes.select { |key, _| HungryForm::Validator.singleton_class.instance_methods(false).include?(key) }
+ @attributes.delete_if { |key, _| @validation_rules.key?(key) }
+
+ set_value
end
- set_value
- end
+ def valid?
+ clear_error
+ is_valid = true
+ return true unless visible?
- def valid?
- clear_error
- is_valid = true
- return true unless visible?
-
- @validation_rules.each do |key, rule|
- error = HungryForm::Validator.send(key, self, rule) || ''
- unless error.empty?
- is_valid = false
- break
+ @validation_rules.each do |key, rule|
+ self.error = HungryForm::Validator.send(key, self, rule) || ''
+ unless error.empty?
+ self.error = "This field #{error}"
+ is_valid = false
+ break
+ end
end
+
+ is_valid
end
- is_valid
- end
+ def invalid?
+ !valid?
+ end
- def set_value
- self.value = resolver.params.key?(name) ? resolver.params[name] : @attributes[:value]
- end
+ def set_value
+ self.value = resolver.params[name] || attributes.delete(:value)
+ end
- def clear_error
- self.error = ''
+ def clear_error
+ self.error = ''
+ end
end
end
end
end