Class: Formulary::HtmlForm::Fields::NumberInput

Inherits:
Input
  • Object
show all
Defined in:
lib/formulary/html_form/fields/number_input.rb

Direct Known Subclasses

RangeInput

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Input

compatible_with?, supports_required?

Methods inherited from Field

#get_value_from_data_field, #is_hidden?, #label, #name, #set_value, supports_required?

Constructor Details

#initialize(html_form, element) ⇒ NumberInput

Returns a new instance of NumberInput.



7
8
9
10
11
12
# File 'lib/formulary/html_form/fields/number_input.rb', line 7

def initialize(html_form, element)
  @html_form, @element = html_form, element
  @min = @element.attributes['min'].try('value')
  @max = @element.attributes['max'].try('value')
  @step = @element.attributes['step'].try('value')
end

Class Method Details

.compatible_typeObject



3
4
5
# File 'lib/formulary/html_form/fields/number_input.rb', line 3

def self.compatible_type
  "number"
end

Instance Method Details

#errorObject



18
19
20
21
22
23
24
# File 'lib/formulary/html_form/fields/number_input.rb', line 18

def error
  return super if super.present?
  return "'#{label}' must be a valid number" unless number_correct?
  return "'#{label}' must be greater than or equal to #{@min}" unless min_correct?
  return "'#{label}' must be less than or equal to #{@max}" unless max_correct?
  return "'#{label}' must be a step of #{@step}, the nearest valid values are #{@lower_step} and #{@higher_step}" unless step_correct?
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/formulary/html_form/fields/number_input.rb', line 14

def valid?
  super && number_correct? && min_correct? && max_correct? && step_correct?
end