Class: AlForm

Inherits:
Object
  • Object
show all
Defined in:
lib/al_form.rb,
lib/al_form/multipart.rb,
lib/al_form/make_tiny_form.rb,
lib/al_form/make_tiny_sheet.rb,
lib/al_form/generate_sql_table.rb,
lib/al_form/generate_form_template.rb,
lib/al_form/generate_sheet_template.rb

Overview

フォームクラス

Constant Summary

AL_LINE_EVEN_ODD =

HTMLタグ生成用クラス名テーブル

["al-line-even", "al-line-odd"]
METHOD_LIST =

ダイナミックローディングメソッド名テーブル

{ :make_tiny_form=>1,
  :make_tiny_sheet=>1,
  :generate_form_template=>1,
  :generate_sheet_template=>1,
#    :generate_list_template=>1,
  :generate_sql_table=>1,
}
@@request_get =

GETリクエストを解析してHash化した値のキャッシュ

{}
@@request_post =

POSTリクエストを解析してHash化した値のキャッシュ

{}
@@flag_request_oversize =

リクエストサイズ過大エラーフラグ

false

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AlForm) initialize(widgets = nil)

constructor



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/al_form.rb', line 138

def initialize( widgets = nil )
  @widgets = {}
  @values = {}
  @validation_messages = {}
  @validation_messages_user_count = 0
  @method = "POST"
  @action = Alone::request_uri()
  @tag_attr = {}
  @flag_setvalue_done = false

  set_widgets( widgets ) if widgets
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(name, *args)

ダイナミックローディング用フック



508
509
510
511
512
513
514
515
# File 'lib/al_form.rb', line 508

def method_missing( name, *args )
  if METHOD_LIST[ name ]
    require "al_form/#{name}"
    __send__( name, *args )
  else
    raise NoMethodError, "undefined method `#{name}' for AlForm"
  end
end

Instance Attribute Details

- (String) action

HTMLフォームのactionアトリビュート

Returns:

  • (String)

    HTMLフォームのactionアトリビュート



126
127
128
# File 'lib/al_form.rb', line 126

def action
  @action
end

- (Boolean) flag_setvalue_done

値のセットが終わっているかのフラグ(バリデーション前)

Returns:

  • (Boolean)

    値のセットが終わっているかのフラグ(バリデーション前)



132
133
134
# File 'lib/al_form.rb', line 132

def flag_setvalue_done
  @flag_setvalue_done
end

- (String) method

HTMLフォームのmethodアトリビュート “GET” or “POST“

Returns:

  • (String)

    HTMLフォームのmethodアトリビュート “GET” or “POST“



123
124
125
# File 'lib/al_form.rb', line 123

def method
  @method
end

- (Hash) tag_attr

HTMLタグ生成時の追加アトリビュート

Returns:

  • (Hash)

    HTMLタグ生成時の追加アトリビュート



129
130
131
# File 'lib/al_form.rb', line 129

def tag_attr
  @tag_attr
end

- (Hash) validation_messages (readonly)

バリデーションメッセージ。キーがウィジェット名のシンボル、値がメッセージ文字列

Returns:

  • (Hash)

    バリデーションメッセージ。キーがウィジェット名のシンボル、値がメッセージ文字列



120
121
122
# File 'lib/al_form.rb', line 120

def validation_messages
  @validation_messages
end

- (Hash) values (readonly)

バリデーションを通過したリクエストの値

Returns:

  • (Hash)

    バリデーションを通過したリクエストの値



117
118
119
# File 'lib/al_form.rb', line 117

def values
  @values
end

- (Hash) widgets (readonly)

このインスタンスが保持するウィジェット。キーがウィジェット名のシンボル、値がウィジェットのインスタンス

Returns:

  • (Hash)

    このインスタンスが保持するウィジェット。キーがウィジェット名のシンボル、値がウィジェットのインスタンス



114
115
116
# File 'lib/al_form.rb', line 114

def widgets
  @widgets
end

Class Method Details

+ (Object) fetch_request_multipart

(AlForm) POSTリクエストの取り込み / multipart



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/al_form/multipart.rb', line 17

def self.fetch_request_multipart()
  req = {}

  #
  # extract boundary
  #
  boundary = nil
  ENV['CONTENT_TYPE'].split( /\s?;\s/ ).each do |a|
    if /\s*([^=]+)(=([^\s]*))?/ =~ a
      if $1 == 'boundary'
        boundary = '--' + $3
      end
    end
  end
  return if ! boundary

  boundary.force_encoding( "ASCII-8BIT" )     # TODO: ruby1.9 only

  #
  # process post data
  # STRATEGY:
  #  ステートマシンを使って解析する
  #  ステートは、NONE -> HEADER -> CONTENT_START -> CONTENT_NEXT と遷移する
  #
  STDIN.set_encoding( "ASCII-8BIT" )          # TODO: ruby1.9 only
  remain_bytes = ENV['CONTENT_LENGTH'].to_i
  content = {}
  file = nil
  state = :STATE_NONE
  while remain_bytes > 0 && text = STDIN.gets do
    remain_bytes -= text.length

    #
    # check boundary
    #
    if text.start_with?( boundary )
      state = :STATE_HEADER
      if ! content[:name]; next; end

      key = content[:name].to_sym
      content[:data_body].force_encoding( AL_CHARSET )
      content[:data_body].chomp!

      case req[key]
      when NilClass
        req[key] = file ? content : content[:data_body]
        
      when String
        req[key] = [ req[key], content[:data_body] ]

      when Array
        req[key] << content[:data_body]
      end

      if file
        file.flush
        if file.size >= 2
          file.truncate( file.size - 2 )        # -2 is CR,LF
        end
        content[:size] = file.size
        file.close
      end

      content = {}
      file = nil
      next
    end

    #
    # process each content
    #
    case state
    when :STATE_HEADER
      case text.chop
      when /^Content-Disposition:(.+)$/i
        $1.split( ';' ).each do |a|
          if / *([^=]+)(="(.*)")?/ =~ a
            content[$1.to_sym] = $3
          end
        end
        
      when /^Content-Type: *([^ ]+)$/i
        content[:content_type] = $1

      when ''                         # data region starts at next line
        state = :STATE_CONTENT_START
      end
      

    when :STATE_CONTENT_START
      if content[:filename]           # this is input type="file" tag
        file = Tempfile.new( 'al_tmp', AL_TEMPDIR )
        content[:tmp_name] = file.path
        file.write( text )
        content[:data_body] = ''
      else
        content[:data_body] = text    # this is other input tag
      end
      state = :STATE_CONTENT_NEXT
      

    when :STATE_CONTENT_NEXT
      if file
        file.write( text )
      else
        content[:data_body] << text
      end
    end

  end  # end of all read

  @@request_post = req
end

+ (Object) get_parameter(w)

単一パラメータの取得/使い捨てミニフォーム機能

Examples:

id = AlForm.get_parameter( AlText.new( 'id', :validator=>/^\d{8}$/ ) )

Parameters:

  • (AlWidget) w

    取得するパラメータを表すウィジェット

Returns:

  • (Object)

    取得値。データタイプは、Widgetによって変わる。



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/al_form.rb', line 89

def self.get_parameter( w )
  case ENV["REQUEST_METHOD"]
  when "GET"
    AlForm.prefetch_request_get()
    value = @@request_get[w.name.to_sym]
  when "POST"
    AlForm.prefetch_request_post()
    value = @@request_post[w.name.to_sym]
  else
    raise "method error. need GET or POST."
  end

  if value
    # GET/POSTされている。フィルタをかけてウィジェットに渡した結果を返す。
    w.value = w.filter ? eval( w.filter ) : value
    return w.validate() ? w.value : nil
  else
    # ウィジェットの初期値を返す。
    return w.value
  end
end

+ (Object) prefetch_request_get

GETリクエストのキャッシュへの取り込み



32
33
34
35
36
37
# File 'lib/al_form.rb', line 32

def self.prefetch_request_get()
  return if ! @@request_get.empty?
  return if ! ENV["QUERY_STRING"]

  @@request_get = parse_urlencoded( ENV["QUERY_STRING"] )
end

+ (Object) prefetch_request_post

POSTリクエストのキャッシュへの取り込み



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/al_form.rb', line 43

def self.prefetch_request_post()
  return if ! @@request_post.empty?

  # check request size
  if ENV["CONTENT_LENGTH"].to_i > AL_FORM_MAX_CONTENT_LENGTH
    @@flag_request_oversize = true
  end

  # proc request data.
  if ENV['CONTENT_TYPE'] && ENV['CONTENT_TYPE'].start_with?( "multipart/form-data" )
    require 'al_form/multipart'
    fetch_request_multipart()
  else
    @@request_post = parse_urlencoded( STDIN.read( ENV["CONTENT_LENGTH"].to_i ) )
  end
end

+ (Hash) request_get

getter / request_get

Returns:

  • (Hash)

    GETリクエストのキャッシュ



66
67
68
# File 'lib/al_form.rb', line 66

def self.request_get()
  return @@request_get
end

+ (Hash) request_post

getter / request_post

Returns:

  • (Hash)

    POSTリクエストのキャッシュ



76
77
78
# File 'lib/al_form.rb', line 76

def self.request_post()
  return @@request_post
end

Instance Method Details

- (Object) add_message(message)

Note:

ユーザプログラムから、任意のメッセージをセットできる。 バリデーション validate() を行う前にクリアされるので、注意する。

メッセージの追加

Parameters:

  • (String) message

    メッセージ



277
278
279
280
# File 'lib/al_form.rb', line 277

def add_message( message )
  @validation_messages["al_user#{@validation_messages_user_count}"] = message
  @validation_messages_user_count += 1
end

- (Object) add_widget(widget)

ウィジェット追加

Parameters:

  • (AlWidget) widget

    ウィジェット



171
172
173
# File 'lib/al_form.rb', line 171

def add_widget( widget )
  @widgets[ widget.name.to_sym ] = widget
end

- (Object) checked(name, value) Also known as: selected

Note:

チェックボックス、または、ラジオボタンの、checked属性を生成する。 htmlをべたに書く時にのみ使用する。

inputタグ中の checked 属性生成

Examples:

<input name="radio1" type="radio" value="r1" <%= form.checked( :radio1, "r1" ) %> >

Parameters:

  • (String, Symbol) name

    ウィジェット識別名

  • (String, Symbol) value

    inputタグのvalue=”“ と同じ値を指定する。



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/al_form.rb', line 470

def checked( name, value )
  widget = @widgets[ name.to_sym ]
  case widget
  when AlCheckboxes
    return (widget.value.include?( value.to_sym ) || widget.value.include?( value.to_s )) ? "checked": ""

  when AlOptions
    return (widget.value && widget.value.to_sym == value.to_sym) ? "selected": ""

  when AlRadios
    return (widget.value && widget.value.to_sym == value.to_sym) ? "checked": ""

  else
    raise "Object not match. needs AlCheckboxes, AlOptions or AlRadios"
  end
end

- (Object) delete_widget(name)

ウィジェット削除

Parameters:

  • (String, Symbol) name

    ウィジェット識別名



181
182
183
184
# File 'lib/al_form.rb', line 181

def delete_widget( name )
  @widgets.delete( name.to_sym )
  @values.delete( name.to_sym )
end

- (Boolean) fetch_request(method = nil)

GET/POSTリクエストの取り込み

Parameters:

  • (String) method (defaults to: nil)

    メソッド “GET” or “POST“

Returns:

  • (Boolean)

    成否



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/al_form.rb', line 303

def fetch_request( method = nil )
  #
  # リクエストを取得し、@@request_XXXにキャッシュする。
  #
  case method || ENV["REQUEST_METHOD"]
  when "GET"
    AlForm.prefetch_request_get()
    req = @@request_get

  when "POST"
    return false if ENV["REQUEST_METHOD"] != "POST"
    AlForm.prefetch_request_post()
    req = @@request_post

  else
    raise "method error. need GET or POST."
  end

  #
  # リクエストが正当なものか確認
  #
  if @@flag_request_oversize
    add_message( "サイズが大きすぎます。" )
    return false
  end
  ret = false
  @widgets.each_key do |k|
    if req[k]
      ret = true
      break
    end
  end
  return ret if ! ret

  #
  # 各ウィジェットに、値を設定
  #
  @widgets.each do |k,w|
    if req[k]
      value = req[k]
      w.value = w.filter ? eval( w.filter ) : value
    else
      w.value = nil
    end
  end

  @flag_setvalue_done = true
  return ret
end

- (Object) generate_form_template(arg = {})

簡易フォームテンプレートの生成 al_erb使用を前提としたフォームのテンプレートを生成する。

Parameters:

  • arg (defaults to: {})

    引数ハッシュ

Options Hash (arg):

  • (Boolean) :use_table N/A

    テーブルタグを利用した整形

  • (Boolean) :use_error_class N/A

    バリデーションエラーの時、al-form-label-errorを出力するための動的コードを埋め込む



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/al_form/generate_form_template.rb', line 21

def generate_form_template( arg = {} )
  flags = { :use_table=>true, :with_error_class=>true }
  flags.merge!( arg )

  r = "<%= header_section %>\n<title></title>\n\n"
  r << "<%= body_section %>\n\n"
  r << "<%= @form.get_messages_by_html() %>\n\n"
  r << %Q(<form method="<%= @form.method %>" action="<%= @form.action %>">\n)

  if flags[:use_table]
    r << %Q(<table class="al-form-table">\n)
    lines = 0
    @widgets.each do |k,w|
      lines += 1
      r << %Q(  <tr class="#{AL_LINE_EVEN_ODD[lines & 1]} #{w.name}\">\n)
      if flags[:with_error_class]
        r << %Q(    <td class="al-form-label<%= @form.validation_messages[:#{w.name}] ? "-error" : "" %>">#{w.label}</td>\n)
      else
        r << %Q(    <td class="al-form-label">#{w.label}</td>\n)
      end
      r << %Q(    <td class="al-form-value"><%= @form.make_tag(:#{w.name}) %></td>\n  </tr>\n\n)
    end
    r << "</table>\n"
    
  else
    lines = 0
    @widgets.each do |k,w|
      lines += 1
      r << %Q(  <div class="#{AL_LINE_EVEN_ODD[lines & 1]} #{w.name}\">\n)
      if flags[:with_error_class]
        r << %Q(    <span class="al-form-label<%= @form.validation_messages[:#{w.name}] ? "-error" : "" %>">#{w.label}</span>\n)
      else
        r << %Q(    <span class="al-form-label">#{w.label}</span>\n)
      end
      r << %Q(    <span class="al-form-value"><%= @form.make_tag(:#{w.name}) %></span>\n  </div>\n\n)
    end
  end

  r << "</form>\n"
  r << "\n<%= footer_section %>"
  return r
end

- (Object) generate_sheet_template(arg = {})

簡易内容表示テンプレートの生成 al_erb使用を前提とした簡易表示のテンプレートを生成する。

Parameters:

  • arg (defaults to: {})

    引数ハッシュ

Options Hash (arg):

  • (Boolean) :use_table N/A

    テーブルタグを利用した整形



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/al_form/generate_sheet_template.rb', line 20

def generate_sheet_template( arg = {} )
  flags = { :use_table=>true }
  flags.merge!( arg )

  r = "<%= header_section %>\n<title></title>\n\n"
  r << "<%= body_section %>\n\n"

  if flags[:use_table]
    r << %Q(<table class="al-sheet-table">\n)
    lines = 0
    @widgets.each do |k,w|
      next if w.class == AlSubmit

      lines += 1
      r << %Q(  <tr class="#{AL_LINE_EVEN_ODD[lines & 1]} #{w.name}">\n)
      r << %Q(    <td class="al-sheet-label">#{w.label}</td>\n)
      r << %Q(    <td class="al-sheet-value"><%= @form.make_value(:#{w.name}) %></td>\n)
      r << %Q(  </tr>\n\n)
    end
    r << "</table>\n"
    
  else
    lines = 0
    @widgets.each do |k,w|
      next if w.class == AlSubmit

      lines += 1
      r << %Q(  <div class="#{AL_LINE_EVEN_ODD[lines & 1]} #{w.name}">\n)
      r << %Q(    <span class="al-sheet-label">#{w.label}</span>\n)
      r << %Q(    <span class="al-sheet-value"><%= form.make_value(:#{w.name} ) %></span>\n)
      r << %Q(  </div>\n\n)
    end
  end

  r << "\n<%= footer_section %>"
  return r
end

- (String) generate_sql_table(tname = "TABLENAME")

テーブル作成SQLのひな形作成

Parameters:

  • (String) tname (defaults to: "TABLENAME")

    テーブル名

Returns:

  • (String)

    SQL



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/al_form/generate_sql_table.rb', line 19

def generate_sql_table( tname = "TABLENAME" )
  a = []
  @widgets.each do |k,w|
    case w
    when AlInteger
      c = "#{k} integer"
    when AlFloat
      c = "#{k} double precision"
    when AlTimestamp
      c = "#{k} timestamp"
    when AlDate
      c = "#{k} date"
    when AlTime
      c = "#{k} time"
    when AlSubmit
      next
    else
      c = "#{k} text"
    end

    if w.required
      c << " not null"
    end
    a << c
  end
  return "create table #{tname} (#{a.join( ', ' )});"
end

- (String) get_messages_by_html

html形式でメッセージの取得

Returns:

  • (String)

    html形式でメッセージを返す



288
289
290
291
292
293
294
# File 'lib/al_form.rb', line 288

def get_messages_by_html()
  r = ""
  @validation_messages.each do |k,v|
    r << Alone::escape_html_br(v) << "<br>\n"
  end
  return r
end

- (Object) get_tainted_value(name) Also known as: -

Note:

alias - は、テンプレートで使われることを想定して定義した。 プログラム中では、使わない方が良いのではないか。

バリデーション前の値取得

Parameters:

  • (String, Symbol) name

    ウィジェット識別名

Returns:

  • (Object)



263
264
265
# File 'lib/al_form.rb', line 263

def get_tainted_value( name )
  return @widgets[ name.to_sym ].value
end

- (Object) get_value(name) Also known as: []

Note:

バリデーション後の値を取得する。 値として何が返るかは、ウィジェットの種類によって変わるので、 詳しくは各ウィジェットを参照。

値取得

Parameters:

  • (String, Symbol) name

    ウィジェット識別名

Returns:

  • (Object)



248
249
250
# File 'lib/al_form.rb', line 248

def get_value( name )
  return @values[name.to_sym]
end

- (AlWidget) get_widget(name)

ウィジェットの取得

Parameters:

  • (String, Symbol) name

    ウィジェット識別名

Returns:



193
194
195
# File 'lib/al_form.rb', line 193

def get_widget( name )
  return @widgets[ name.to_sym ]
end

- (String) make_tag(name, arg = {})

Note:

指定された名前を持つウィジェットのタグを生成し返す。

HTMLタグの生成

Examples:

<%= form.make_tag( :text1 ) %>

Parameters:

  • (String, Symbol) name

    ウィジェット識別名

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

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

Returns:

  • (String)

    htmlタグ



435
436
437
438
439
440
# File 'lib/al_form.rb', line 435

def make_tag( name, arg = {} )
  widget = @widgets[ name.to_sym ]
  raise %Q(No widget defined, named "#{name}".)  if ! widget

  return widget.make_tag( arg )
end

- (String) make_tiny_form(arg = {})

Note:

tableタグを使って、位置をそろえている。

簡易フォームの自動生成

Parameters:

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

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

Returns:

  • (String)

    生成したHTML



21
22
23
24
25
26
27
# File 'lib/al_form/make_tiny_form.rb', line 21

def make_tiny_form( arg = {} )
  r = %Q(<form method="#{@method}" action="#{Alone::escape_html(@action)}")
  (@tag_attr.merge arg).each do |k,v|
    r << %Q( #{k}="#{Alone::escape_html(v)}")
  end
  return "#{r}>\n#{make_tiny_form_main()}</form>\n"
end

- (Object) make_tiny_form_main

Note:

TODO: アプリケーションからこのメソッドが独自によばれることがなさそうなら make_tiny_form()へ吸収合併を考えること。

簡易フォームの自動生成 メインメソッド



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/al_form/make_tiny_form.rb', line 37

def make_tiny_form_main()
  lines = 0
  r = %Q(<table class="al-form-table">\n)
   = ""
  @widgets.each do |k,w|
    if w.
       << w.make_tag()
      next
    end

    lines += 1
    r << %Q(  <tr class="#{AL_LINE_EVEN_ODD[lines & 1]} #{w.name}">\n)
    if @validation_messages[ k ]
      r << %Q(    <td class="al-form-label-error">#{w.label}</td>\n)
    else
      r << %Q(    <td class="al-form-label">#{w.label}</td>\n)
    end
    r << %Q(    <td class="al-form-value">#{w.make_tag()}</td>\n  </tr>\n)
  end
  r << "</table>\n"
  if ! .empty?
    r << "<div>#{}</div>\n"
  end

  return r
end

- (String) make_tiny_sheet

Note:

tableタグを使って、位置をそろえている。

簡易内容表示

Returns:

  • (String)

    生成したHTML



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/al_form/make_tiny_sheet.rb', line 20

def make_tiny_sheet()
  lines = 0
  r = %Q(<table class="al-sheet-table">\n)
  @widgets.each do |k,w|
    next if w.class == AlSubmit || w.

    lines += 1
    r << %Q(  <tr class="#{AL_LINE_EVEN_ODD[lines & 1]} #{w.name}">\n)
    r << %Q(    <td class="al-sheet-label">#{w.label}</td>\n)
    r << %Q(    <td class="al-sheet-value">#{w.make_value()}</td>\n  </tr>\n)
  end
  r << "</table>\n"

  return r
end

- (String) make_value(name)

Note:

make_tag()との対称性をもたせるために存在する。

HTML値の生成

Parameters:

  • (String, Symbol) name

    ウィジェット識別名

Returns:

  • (String)

    html文字列



451
452
453
454
455
456
# File 'lib/al_form.rb', line 451

def make_value( name )
  widget = @widgets[ name.to_sym ]
  raise %Q(No widget defined, named "#{name}".)  if ! widget

  return widget.make_value()
end

- (Object) set_value(name, value)

Note:

受け付けるvalueは、ウィジェットの種類によって変わる。 詳しくは、各ウィジェットを参照。

値のセット

Parameters:

  • (String, Symbol) name

    ウィジェット識別名

  • (String, Array) value

    セットする値



207
208
209
210
211
# File 'lib/al_form.rb', line 207

def set_value( name, value )
  k = name.to_sym
  @widgets[k].value = value
  @values[k] = @widgets[k].value      # valueは、Widgetごとの都合による加工がされているかもしれない
end

- (Object) set_values(values) Also known as: values=

Note:

nilを与えられた場合は、何も行わない。 それ以外の場合は、fetch_request()を呼び出したのと同様、 validate()の値自動取得が働かなくなるので注意する。

値の一括セット

Parameters:

  • (Hash, NilClass) values

    セットする値



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/al_form.rb', line 223

def set_values( values )
  return if ! values
  raise "set_values() needs Hash parameter." if values.class != Hash

  values.each do |k,v|
    k = k.to_sym
    next  if ! @widgets[k]
    @widgets[k].set_value( v )
    @values[k] = @widgets[k].value      # valueは、Widgetごとの都合による加工がされているかもしれない
  end
  @flag_setvalue_done = true
end

- (Object) set_widgets(widgets) Also known as: widgets=

ウィジェットのセット

Parameters:

  • (Array) widgets

    ウィジェットの配列



157
158
159
160
161
162
# File 'lib/al_form.rb', line 157

def set_widgets( widgets )
  raise "Parameter type error. need Array."  if widgets.class != Array
  widgets.each do |w|
    @widgets[ w.name.to_sym ] = w
  end
end

- (Boolean) validate(names = nil)

Note:

引数を与えなければ、すべてのウィジェットについてバリデーションを実施する バリデーションが成功して、はじめて @values に使える値が用意される。

バリデーション

Parameters:

  • (Array) names (defaults to: nil)

    バリデーションするウィジェット名の配列

Returns:

  • (Boolean)

    成否



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/al_form.rb', line 363

def validate( names = nil )
  @validation_messages.clear

  #
  # リクエストの取得がまだ行われていないなら、自動で取得する
  #
  if ! @flag_setvalue_done
    return false  if ! fetch_request()
  end

  case names
  when NilClass
    #
    # すべてのウィジェットについてバリデーションを実施
    #
    @widgets.each do |k,w|
      next  if w.class == AlSubmit    # valueをとりたくないのでskip
      if w.validate()
        @values[k] = w.value
      else
        @validation_messages[k] = w.message
      end
    end

  when Array
    #
    # 選択的にバリデーションを実施
    #
    names.each do |k|
      k = k.to_sym
      w = @widgets[k]
      raise "Not found widget '#{k}'." if ! w
      next  if w.class == AlSubmit    # valueをとりたくないのでskip
      if w.validate()
        @values[k] = w.value
      else
        @validation_messages[k] = w.message
      end
    end

  when String, Symbol
    #
    # 一つだけバリデーションを実施
    #
    k = names.to_sym
    w = @widgets[k]
    if w.validate()
      @values[k] = w.value
    else
      @validation_messages[k] = w.message
    end

  else
    raise 'validate() needs an array, string or symbol argument.'

  end

  return @validation_messages.empty? ? true: false
end