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

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:



941
942
943
944
945
946
# File 'lib/al_form.rb', line 941

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

選択項目のハッシュ



927
928
929
# File 'lib/al_form.rb', line 927

def options
  @options
end

#separatorObject (readonly)

String

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



930
931
932
# File 'lib/al_form.rb', line 930

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タグ



993
994
995
996
997
998
999
1000
1001
# File 'lib/al_form.rb', line 993

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



1010
1011
1012
1013
1014
1015
1016
# File 'lib/al_form.rb', line 1010

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)

    セットする値



954
955
956
957
# File 'lib/al_form.rb', line 954

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

#validateBoolean

(AlSelector) バリデート

Returns:

  • (Boolean)

    成否



966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
# File 'lib/al_form.rb', line 966

def validate()
  @message = ""

  if @value == "" || @value == nil
    if @required
      @message = "#{@label}を選んでください。";
      return false
    end
    return true
  end

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

  return true
end