Class: AlSelector

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

Overview

セレクタウィジェット スーパークラス

(チェックボックス、プルダウンメニュー、ラジオボタン)

Direct Known Subclasses

AlCheckboxes, AlOptions, AlRadios

Instance Attribute Summary collapse

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 = {}) ⇒ AlSelector

(AlSelector) constructor

Parameters:

  • name (String)

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

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

    引数ハッシュ

Options Hash (arg):

  • :options (Hash)

    選択オプションのハッシュ

  • :separator (String)

    HTMLタグを生成する場合のタグ間セパレータ

See Also:



946
947
948
949
950
951
# File 'lib/al_form.rb', line 946

def initialize( name, arg = {} )
  super( name, arg )
  @options = arg[:options]
  @separator = arg[:separator]
  raise "Need ':options' parameter when make widget."  if ! @options
end

Instance Attribute Details

#optionsObject (readonly)

Hash

選択項目のハッシュ



932
933
934
# File 'lib/al_form.rb', line 932

def options
  @options
end

#separatorObject (readonly)

String

HTMLタグを生成する場合のタグ間セパレータ



935
936
937
# File 'lib/al_form.rb', line 935

def separator
  @separator
end

Instance Method Details

#make_tag(appendix_tag = {}) ⇒ String

Note:

フラグ @hidden の時のみ対応。その他はサブクラスへ委譲。appendix_tagは使わない

(AlSelector) HTMLタグの生成

Parameters:

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

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

Returns:

  • (String)

    htmlタグ



998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/al_form.rb', line 998

def make_tag( appendix_tag = {} )
  @options.each do |k,v|
    if @value && @value.to_s == k.to_s
      return %Q(<input type="hidden" name="#{@name}" id="#{@name}" value="#{Alone::escape_html(k)}">\n)
    end
  end

  return ""
end

#make_value(*arg) ⇒ String

(AlSelector) HTML値の生成

Parameters:

  • arg (String)

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

Returns:

  • (String)

    html文字列



1015
1016
1017
1018
1019
1020
1021
# File 'lib/al_form.rb', line 1015

def make_value( *arg )
  v = arg.empty? ? @value : arg[0]
  if v == "" || v == nil
    return ""
  end
  return Alone::escape_html( @options[v] || @options[v.to_sym] || @options[v.to_s] || @options[v.to_i] )
end

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

(AlSelector) 値のセット

Parameters:

  • v (String, NilClass)

    セットする値



959
960
961
962
# File 'lib/al_form.rb', line 959

def set_value( v )
  # 選択されない場合をnilで統一するため、ヌルストリングはnilへ変換する。
  @value = (v == "") ? nil : v
end

#validateBoolean

(AlSelector) バリデート

Returns:

  • (Boolean)

    成否



971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/al_form.rb', line 971

def validate()
  @message = ""

  if @value == "" || @value == nil
    if @required
      @message = p_("al", "Please select %s.") % @label
      return false
    end
    return true
  end

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

  return true
end