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

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:



1043
1044
1045
1046
1047
1048
# File 'lib/al_form.rb', line 1043

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)

    最大チェック数



1028
1029
1030
# File 'lib/al_form.rb', line 1028

def max
  @max
end

#minInteger

Returns 最小チェック数.

Returns:

  • (Integer)

    最小チェック数



1031
1032
1033
# File 'lib/al_form.rb', line 1031

def min
  @min
end

Instance Method Details

#make_tag(appendix_tag = {}) ⇒ String

(AlCheckboxes) HTMLタグの生成

Parameters:

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

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

Returns:

  • (String)

    htmlタグ



1119
1120
1121
1122
1123
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
# File 'lib/al_form.rb', line 1119

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文字列



1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
# File 'lib/al_form.rb', line 1161

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

    セットする値



1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/al_form.rb', line 1056

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)

    成否



1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'lib/al_form.rb', line 1085

def validate()
  @message = ""

  if @max && @value.size > @max
    @message = "#{@label}は、最大#{@max}個選んでください。"
    return false
  end
  if @min && @value.size < @min
    @message = "#{@label}は、最低#{@min}個選んでください。"
    return false
  end

  if @value.empty? && @required
    @message = "#{@label}を選んでください。";
    return false
  end

  @value.each { |v|
    if ! @options[v.to_sym] && ! @options[v.to_s] && ! @options[v.to_i]
      @message = "#{@label}の入力が、規定値以外です。";
      return false
    end
  }

  return true
end