Sha256: 078cce4d53ba6633eafbf351799b61cf1f6069515a994b126a6088ffc7e4e9c8

Contents?: true

Size: 702 Bytes

Versions: 2

Compression:

Stored size: 702 Bytes

Contents

module HungryForm
  module Elements
    class CheckboxField < Base::ActiveElement
      def set_value
        self.value = resolver.params[name] || attributes.delete(:value) || 0

        if value == 0
          attributes.delete(:checked)
        else
          self.value = 1
          self.attributes[:checked] = true
        end
      end

      def checked?
        !!attributes[:checked]
      end

      # Overriding the Validator methods
      module Validator
        def self.required(element, rule)
          if rule.respond_to? :call
            rule.call(element)
          else
            'is required' if element.value == 0 && rule
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hungryform-0.0.10 lib/hungryform/elements/checkbox_field.rb
hungryform-0.0.9 lib/hungryform/elements/checkbox_field.rb