Module: Formz::Labels

Defined in:
lib/formz/labels.rb

Overview

Formz::Labels

The Formz:Labels module allows a :label attribute to be passed, which then adds a label tag to the markup output. Tags are nested nested within its label, in cases such as for checkboxes or radio buttons.

When using labels, a :required attribute may also be passed, which will alter the markup to indicate the input field is required.

Examples

tag :input, :type => :file, :name => :upload, :label => 'Upload', :required => true

<div class="form-upload form-file">
  <label for="upload">Upload<em>*</em>:</label>
  <input type="file" name="upload" />
</div>

Constant Summary collapse

NESTED_LABEL_INPUT_TYPES =
:checkbox, :radio

Instance Method Summary collapse

Instance Method Details

#create_tag(name, contents, attrs, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/formz/labels.rb', line 30

def create_tag name, contents, attrs, &block
  if name != :optgroup && string = attrs.delete(:label)
    label_attrs = { :for => attrs[:name], :required => attrs.delete(:required) }
    if attrs[:type].in? NESTED_LABEL_INPUT_TYPES
      label string, super, label_attrs
    else
      label(string, label_attrs) << super
    end
  else
    super
  end
end

#label(string, contents = nil, attrs = {}) ⇒ Object

Return a label with string. When contents is present the label will act as a wrapper for checkboxes, radios etc.



48
49
50
51
52
53
# File 'lib/formz/labels.rb', line 48

def label string, contents = nil, attrs = {}
  attrs, contents = contents, nil if contents.is_a? Hash
  Tagz.tag :label, contents.to_s + string + (contents ? '' :
        attrs.delete(:required) ? '<em>*</em>:' :
          ':'), attrs
end