Sha256: 72308f33bdc9f6f599b23be2f5057a2aac3d33a5422b1780becb28a77a2ae8dd

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

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

      hashable :required, :value, :error

      def initialize(name, parent, resolver, attributes = {}, &block)
        super

        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

        set_value
      end

      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
          end
        end

        is_valid
      end

      def set_value
        self.value = resolver.params.key?(name) ? resolver.params[name] : @attributes[:value]
      end

      def clear_error
        self.error = ''
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hungryform-0.0.4 lib/hungryform/elements/base/active_element.rb