Class: HungryForm::Elements::CheckboxField

Inherits:
Base::ActiveElement show all
Defined in:
lib/hungryform/elements/checkbox_field.rb

Defined Under Namespace

Modules: Validator

Instance Attribute Summary

Attributes inherited from Base::ActiveElement

#error, #required, #value

Attributes inherited from Base::Element

#attributes, #dependency, #label, #name, #placeholders, #resolver, #visible

Instance Method Summary collapse

Methods inherited from Base::ActiveElement

#clear_error, #initialize, #invalid?, #valid?, #validate_rule

Methods inherited from Base::Element

#configuration, #dependency_json, #initialize

Methods included from Base::Hashable

included, #to_hash

Constructor Details

This class inherits a constructor from HungryForm::Elements::Base::ActiveElement

Instance Method Details

#checked?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/hungryform/elements/checkbox_field.rb', line 15

def checked?
  !!attributes[:checked]
end

#set_valueObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/hungryform/elements/checkbox_field.rb', line 4

def set_value
  self.value = resolver.params[name] || attributes.delete(:value) || 0

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