Skip to content

Commit

Permalink
render min/max lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed May 5, 2014
1 parent ca5b37b commit 151f997
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
20 changes: 20 additions & 0 deletions app/models/formbuilder/response_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def has_length_validations?
length_validations(false).any?
end

def min_max_length_units
field_options[:min_max_length_units] || 'characters'
end

def minlength
field_options[:minlength].presence
end

def maxlength
field_options[:maxlength].presence
end

def min_max_validations
return_hash = {
min: field_options[:min],
Expand All @@ -62,6 +74,14 @@ def min_max_validations
return_hash.select { |k, v| v.present? }
end

def min
field_options[:min].presence
end

def max
field_options[:max].presence
end

def render_input(value, opts = {})
raise 'Not implemented'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/formbuilder/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Formbuilder
VERSION = "0.2.2"
VERSION = "0.2.3"
end
36 changes: 36 additions & 0 deletions lib/formbuilder/views/form_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def content
render_label if @response_field.input_field
rawtext @response_field.render_input(@value, entry: @entry)
div.clear
render_min_max_lengths
render_min_max
render_error if @response_field.input_field && @entry.error_for(@response_field)
render_description if @response_field.input_field && @response_field[:field_options]["description"].present?
}
Expand Down Expand Up @@ -40,6 +42,40 @@ def render_description
}
end

def render_min_max_lengths
return unless @response_field.input_field && @response_field.has_length_validations?

div.min_max {
if @response_field.minlength && @response_field.maxlength
text "Between #{@response_field.minlength} and #{@response_field.maxlength} #{@response_field.min_max_length_units}."
elsif @response_field.minlength
text "More than #{@response_field.minlength} #{@response_field.min_max_length_units}."
elsif @response_field.maxlength
text "Less than #{@response_field.maxlength} #{@response_field.min_max_length_units}."
end

text ' Current count: '
code.min_max_counter
text " #{@response_field.min_max_length_units}."
}
end

def render_min_max
return unless @response_field.input_field && @response_field.min_max_validations.present?

div.min_max {
div.min_max_info {
if @response_field.min && @response_field.max
text "Between #{@response_field.min} and #{@response_field.max}."
elsif @response_field.min
text "More than #{@response_field.min}."
elsif @response_field.max
text "Less than #{@response_field.max}."
end
}
}
end

end
end
end

0 comments on commit 151f997

Please sign in to comment.