lib/ratpack/forms.rb in BrianTheCoder-ratpack-0.2.4 vs lib/ratpack/forms.rb in BrianTheCoder-ratpack-0.3.1

- old
+ new

@@ -1,11 +1,11 @@ module RatPack module Forms def error_messages_for(obj = nil, opts = {}) return unless obj.respond_to?(:errors) return if obj.errors.blank? - html = tag(:h2, "Form submission failed be cause of #{obj.errors.size} #{pluralize("error",obj.errors.size)}") + html = tag(:h2, "Form submission failed because of #{obj.errors.size} #{pluralize("error",obj.errors.size)}") msgs = obj.errors.map{|error| error.map{|msg| tag(:li,msg)}}.flatten tag(:div, html + tag(:ul,msgs), :class => "error") end %w(text password hidden file).each do |kind| @@ -38,10 +38,11 @@ end def check_box(attrs) html = "" label = build_field(attrs) + attrs[:checked] = "checked" if attrs[:checked] if attrs.delete(:boolean) on, off = attrs.delete(:on), attrs.delete(:off) html << hidden_field(:name => attrs[:name], :value => off) html << self_closing_tag(:input, {:type => "checkbox", :value => on}.merge(attrs)) else @@ -71,16 +72,20 @@ end private def form_field(type, content, attrs) - attrs[:id] = attrs[:name].gsub(/(\[)(.+?)\]$/,'_\2') unless attrs.has_key?(:id) + attrs[:id] = sanitize_name(attrs[:name]) unless attrs.has_key?(:id) build_field(attrs) + tag(type, content, attrs) end def closed_form_field(type, attrs) - attrs[:id] = attrs[:name].gsub(/(\[)(.+?)\]$/,'_\2') unless attrs.has_key?(:id) + attrs[:id] = sanitize_name(attrs[:name]) unless attrs.has_key?(:id) build_field(attrs) + self_closing_tag(type, attrs) + end + + def sanitize_name(name) + name.gsub(/\[\]$/,'').gsub(/(\[)(.+?)\]$/,'_\2') end def build_field(attrs) label = attrs.has_key?(:label) ? build_label(attrs) : "" hint = attrs.has_key?(:hint) ? tag(:div,attrs.delete(:hint), :class => "hint") : "" \ No newline at end of file