Class: AlCheckboxes

Inherits:
AlSelector show all
Defined in:
lib/al_form.rb

Overview

チェックボックスウィジェット

Instance Attribute Summary collapse

Attributes inherited from AlSelector

#options, #separator

Attributes inherited from AlWidget

#filter, #foreign, #hidden, #label, #message, #name, #required, #tag_attr, #tag_type, #value, #value_format

Instance Method Summary collapse

Methods inherited from AlWidget

#set_attr

Constructor Details

#initialize(name, arg = {}) ⇒ AlCheckboxes

(AlCheckboxes) constructor

Parameters:

  • name (String)

    ウィジェット識別名 英文字を推奨

  • arg (Hash) (defaults to: {})

    引数ハッシュ

Options Hash (arg):

  • :max (Integer)

    最大チェック数

  • :min (Integer)

    最小チェック数

See Also:



1048
1049
1050
1051
1052
1053
# File 'lib/al_form.rb', line 1048

def initialize( name, arg = {} )
  super( name, arg )
  @value = set_value( @value )
  @max = arg[:max]
  @min = arg[:min]
end

Instance Attribute Details

#maxInteger

Returns 最大チェック数.

Returns:

  • (Integer)

    最大チェック数



1033
1034
1035
# File 'lib/al_form.rb', line 1033

def max
  @max
end

#minInteger

Returns 最小チェック数.

Returns:

  • (Integer)

    最小チェック数



1036
1037
1038
# File 'lib/al_form.rb', line 1036

def min
  @min
end

Instance Method Details

#make_tag(appendix_tag = {}) ⇒ String

(AlCheckboxes) HTMLタグの生成

Parameters:

  • appendix_tag (Hash) (defaults to: {})

    htmlタグへ追加するアトリビュートを指定

Returns:

  • (String)

    htmlタグ



1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
# File 'lib/al_form.rb', line 1124

def make_tag( appendix_tag = {} )
  r = ""
  if @hidden
    tag = %Q(<input type="hidden" name="#{@name}")
    @options.each do |k,v|
      if @value.include?(k.to_sym) || @value.include?(k.to_s)
        tagvalue = Alone::escape_html( k.to_s )
        r << %Q(#{tag} id="#{@name}_#{tagvalue}" value="#{tagvalue}">\n)
      end
    end
  else
    @options.each do |k,v|
      checked = @value.include?(k)
      if ! checked
        case k
        when Numeric
          checked = @value.include?(k.to_s)
        when String
          checked = @value.include?(k.to_sym)
        when Symbol
          checked = @value.include?(k.to_s)
        end
      end
      checked = checked ? " checked" : ""

      tagvalue = Alone::escape_html( k.to_s )
      r << %Q(<label><input type="checkbox" name="#{@name}" id="#{@name}_#{tagvalue}" value="#{tagvalue}"#{checked})
      make_tag_attr(r, appendix_tag)
      r << ">#{Alone::escape_html(v)}</label>#{@separator}\n"
    end
  end

  return r
end

#make_value(*arg) ⇒ String

(AlCheckboxes) HTML値の生成

Parameters:

  • arg (String, Array<String>)

    表示値。指定なければ内部値を使う。

Returns:

  • (String)

    html文字列



1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/al_form.rb', line 1166

def make_value( *arg )
  v = arg.empty? ? @value : arg[0]
  if v.class == String
    v = v.split(',')
  end
  r = ""
  v.each do |k|
    r << Alone::escape_html( @options[k.to_sym] || @options[k.to_s] || @options[k.to_i] ) << " "
  end
  return r
end

#set_value(value) ⇒ Object Also known as: value=

(AlCheckboxes) 値のセット

Parameters:

  • value

    セットする値



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/al_form.rb', line 1061

def set_value( value )
  case value
  when Array
    @value = []
    value.flatten.each do |v|
      case v
      when String,TrueClass,FalseClass,Numeric
        @value << v.to_s
      end
    end

  when String
    @value = value.split(',')

  when TrueClass,FalseClass
    @value = [ value.to_s ]

  else
    @value = []
  end
end

#validateBoolean

(AlCheckboxes) バリデーション

Returns:

  • (Boolean)

    成否



1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
# File 'lib/al_form.rb', line 1090

def validate()
  @message = ""

  if @max && @value.size > @max
    @message = p_("al", "Please select a maximum of %2$d for %1$s.") % [@label, @max]
    return false
  end
  if @min && @value.size < @min
    @message = p_("al", "Please select a minimum of %2$d for %1$s.") % [@label, @min]
    return false
  end

  if @value.empty? && @required
    @message = p_("al", "Please select %s.") % @label
    return false
  end

  @value.each { |v|
    if ! @options[v.to_sym] && ! @options[v.to_s] && ! @options[v.to_i]
      @message = p_("al", "The input for %s is not a valid value.") % @label
      return false
    end
  }

  return true
end