Zend_Form-Forms.xml 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.form.forms">
  5. <title>Zend_Form によるフォームの作成</title>
  6. <para>
  7. <classname>Zend_Form</classname> クラスは、フォームの要素や
  8. 表示グループ、そしてサブフォームの集約を行います。
  9. そして、その要素に対して次のような操作を行います。
  10. </para>
  11. <itemizedlist>
  12. <listitem><para>
  13. 入力の検証、エラーコードやメッセージの取得
  14. </para></listitem>
  15. <listitem><para>
  16. 値の集約 (すべての項目についてのフィルタリング前/
  17. フィルタリング後の値の取得など)
  18. </para></listitem>
  19. <listitem><para>
  20. すべての項目についての反復処理。
  21. 登録した順、あるいは各項目から取得した順序ヒントに従った順での処理
  22. </para></listitem>
  23. <listitem><para>
  24. フォーム全体のレンダリング。デコレータでレンダリング方法をカスタマイズしたり、
  25. フォームの各項目を順次処理したりなど
  26. </para></listitem>
  27. </itemizedlist>
  28. <para>
  29. <classname>Zend_Form</classname> で作成したフォームが複雑なものになることもありますが、
  30. シンプルなフォームで使用するのがもっともよくある使用例でしょう。
  31. 手早いアプリケーション開発 (RAD) やプロトタイピングでの使用に最適です。
  32. </para>
  33. <para>
  34. もっとも基本的な使用法は、単にフォームオブジェクトを作成するものです。
  35. </para>
  36. <programlisting language="php"><![CDATA[
  37. // 汎用的なフォームオブジェクト
  38. $form = new Zend_Form();
  39. // 独自のフォームオブジェクト
  40. $form = new My_Form()
  41. ]]></programlisting>
  42. <para>
  43. <classname>Zend_Config</classname> のインスタンスあるいは配列で設定を渡すことができます。
  44. これを使用すると、オブジェクトの状態を設定したり新しい要素を作成したりできます。
  45. </para>
  46. <programlisting language="php"><![CDATA[
  47. // 設定オプションを渡します
  48. $form = new Zend_Form($config);
  49. ]]></programlisting>
  50. <para>
  51. <classname>Zend_Form</classname> は順次処理が可能です。
  52. 各要素や表示グループ、サブフォームごとに処理できます。
  53. 処理順は、登録した順番および各項目に設定されている順番となります。
  54. これは、各要素のレンダリングを適切な順で行いたいときに便利です。
  55. </para>
  56. <para>
  57. <classname>Zend_Form</classname> は、要素や表示グループのファクトリとして使用できると同時に
  58. デコレータで自分自身をレンダリングすることもできます。
  59. </para>
  60. <sect2 id="zend.form.forms.plugins">
  61. <title>プラグインローダー</title>
  62. <para>
  63. <classname>Zend_Form</classname> では、
  64. <classname>Zend_Loader_PluginLoader</classname> を使用することで
  65. 別の要素やデコレータの位置を指定できます。
  66. それぞれにプラグインローダーを関連づけることができ、
  67. 汎用的なアクセサを使用してそれを取得したり変更したりします。
  68. </para>
  69. <para>
  70. 使用できるローダーの型は 'element' と 'decorator'
  71. です。さまざまなプラグインローダーメソッドが用意されています。
  72. 型の名前は大文字小文字を区別しません。
  73. </para>
  74. <para>
  75. プラグインローダーで使用できるメソッドは、次のとおりです。
  76. </para>
  77. <itemizedlist>
  78. <listitem><para>
  79. <methodname>setPluginLoader($loader, $type)</methodname>:
  80. $loader はプラグインローダーオブジェクトで、type
  81. は先ほど説明した型のいずれかです。
  82. これは、指定した型のプラグインローダーを
  83. 指定したローダーオブジェクトに設定します。
  84. </para></listitem>
  85. <listitem><para>
  86. <methodname>getPluginLoader($type)</methodname>:
  87. $type に関連づけられたプラグインローダーを取得します。
  88. </para></listitem>
  89. <listitem><para>
  90. <methodname>addPrefixPath($prefix, $path, $type = null)</methodname>:
  91. プレフィックスとパスの関連づけを、$type で指定したローダーに指定します。
  92. $type が <constant>NULL</constant> の場合はそのパスをすべてのローダーに追加します。この場合、
  93. プレフィックスの後に "_Element" および
  94. "_Decorator" を追加し、パスに "Element/" および
  95. "Decorator/" を追加します。追加のフォーム要素クラス群をすべて同じ階層に配置した場合は、
  96. このメソッドを使用すると簡単に基底プレフィックスを設定できます。
  97. </para></listitem>
  98. <listitem><para>
  99. <methodname>addPrefixPaths(array $spec)</methodname>:
  100. 複数のパスを、ひとつあるは複数のプラグインローダーに一度に追加します。
  101. 配列の各要素は、キー 'path'、'prefix' および
  102. 'type' を持つ配列となります。
  103. </para></listitem>
  104. </itemizedlist>
  105. <para>
  106. さらに、<classname>Zend_Form</classname>
  107. のインスタンスで作成したすべての要素や表示グループ用のプレフィックスパスを設定するには
  108. 次のメソッドを使用します。
  109. </para>
  110. <itemizedlist>
  111. <listitem><para>
  112. <methodname>addElementPrefixPath($prefix, $path, $type = null)</methodname>:
  113. <methodname>addPrefixPath()</methodname> と同じですが、
  114. クラスのプレフィックスとパスを指定しなければなりません。
  115. <code>$type</code> を指定する場合は、それは
  116. <classname>Zend_Form_Element</classname>
  117. で指定したプラグインローダーの型のひとつでなければなりません。
  118. <code>$type</code> にどんな値が指定できるのかについては <link
  119. linkend="zend.form.elements.loaders">要素のプラグインのセクション</link>
  120. を参照ください。<code>$type</code> を省略した場合は、
  121. すべての型で用いる全般的なプレフィックスとみなします。
  122. </para></listitem>
  123. <listitem><para>
  124. <methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname>:
  125. <methodname>addPrefixPath()</methodname> と似ていますが、
  126. クラスのプレフィックスとパスを指定しなければなりません。
  127. しかし、表示グループではプラグインとして対応しているのはデコレータだけなので、
  128. <code>$type</code> は不要です。
  129. </para></listitem>
  130. </itemizedlist>
  131. <para>
  132. 独自の要素やデコレータを使用すると、
  133. 複数フォーム間で機能を共有したり
  134. 独自の機能をカプセル化したりできるようになります。
  135. 独自の要素を既存の標準クラスに置き換えて使用する例については、
  136. 要素のドキュメントの中の
  137. <link linkend="zend.form.elements.loaders.customLabel">独自のラベルを作る例</link>
  138. を参照ください。
  139. </para>
  140. </sect2>
  141. <sect2 id="zend.form.forms.elements">
  142. <title>要素</title>
  143. <para>
  144. <classname>Zend_Form</classname> では、フォームに要素を追加したり
  145. フォームから要素を削除したりするためのアクセサを提供しています。
  146. これらは要素オブジェクトのインスタンスを受け取ることもできますし、
  147. ファクトリとして要素オブジェクトのインスタンスを作成させることもできます。
  148. </para>
  149. <para>
  150. 要素を追加するもっとも基本的なメソッドが
  151. <methodname>addElement()</methodname> です。
  152. このメソッドは、 <classname>Zend_Form_Element</classname> 型
  153. (あるいは <classname>Zend_Form_Element</classname> を継承したクラス) のオブジェクト
  154. あるいは新しい要素を作成するための引数
  155. (要素の型や名前、設定オプション) を受け取ります。
  156. </para>
  157. <para>
  158. 例を示します。
  159. </para>
  160. <programlisting language="php"><![CDATA[
  161. // 要素のインスタンスを使用します
  162. $element = new Zend_Form_Element_Text('foo');
  163. $form->addElement($element);
  164. // ファクトリを使用します
  165. //
  166. // Zend_Form_Element_Text 型で
  167. // 'foo' という名前の要素を作成します
  168. $form->addElement('text', 'foo');
  169. // 要素の label オプションを渡します
  170. $form->addElement('text', 'foo', array('label' => 'Foo:'));
  171. ]]></programlisting>
  172. <note>
  173. <title>addElement() における「流れるようなインターフェイス」の実装</title>
  174. <para>
  175. <methodname>addElement()</methodname> は「流れるようなインターフェイス」
  176. を実装しています。つまり、このメソッドは要素ではなく
  177. <classname>Zend_Form</classname> オブジェクトを返すということです。
  178. これによって、複数の addElement() メソッドやその他のフォームメソッド
  179. (<classname>Zend_Form</classname> のすべてのセッターは流れるようなインターフェイスを実装しています)
  180. を連結させることができます。
  181. </para>
  182. <para>
  183. 単に要素を返してほしい場合は、
  184. <methodname>createElement()</methodname> を使いましょう。
  185. 概要は以下で説明します。しかし、
  186. <methodname>createElement()</methodname> は要素をフォームにアタッチしないことに注意しましょう。
  187. </para>
  188. <para>
  189. 内部的には、<methodname>addElement()</methodname> は実際には
  190. <methodname>createElement()</methodname> をコールして作成した要素をフォームにアタッチしています。
  191. </para>
  192. </note>
  193. <para>
  194. 要素をフォームに追加したら、名前を指定してそれを取得できます。
  195. 取得するには、<methodname>getElement()</methodname> メソッドを使用するか
  196. オブジェクトのプロパティとして要素にアクセスします。
  197. property:
  198. </para>
  199. <programlisting language="php"><![CDATA[
  200. // getElement():
  201. $foo = $form->getElement('foo');
  202. // オブジェクトのプロパティとして
  203. $foo = $form->foo;
  204. ]]></programlisting>
  205. <para>
  206. 時には、フォームにアタッチせずに要素を作成したいこともあるでしょう
  207. (フォームに登録されているさまざまなプラグインパスを使いたいけれど、
  208. そのオブジェクトは後でサブフォームにアタッチしたい場合など)。
  209. そんな場合は <methodname>createElement()</methodname> メソッドを使用します。
  210. </para>
  211. <programlisting language="php"><![CDATA[
  212. // $username は Zend_Form_Element_Text オブジェクトとなります
  213. $username = $form->createElement('text', 'username');
  214. ]]></programlisting>
  215. <sect3 id="zend.form.forms.elements.values">
  216. <title>値の設定と取得</title>
  217. <para>
  218. フォームの検証をすませたら、通常は値を取得することになるでしょう。
  219. その値を用いてデータベースの更新なりウェブサービスの操作なりといった作業を行います。
  220. すべての要素の値を取得するには <methodname>getValues()</methodname> を使用します。
  221. <methodname>getValue($name)</methodname> を使用すると、
  222. 要素名を指定して特定の要素の値を取得できます。
  223. </para>
  224. <programlisting language="php"><![CDATA[
  225. // すべての値を取得します
  226. $values = $form->getValues();
  227. // 'foo' 要素の値のみを取得します
  228. $value = $form->getValue('foo');
  229. ]]></programlisting>
  230. <para>
  231. レンダリング前に、フォームに特定の値を設定したいこともあるでしょう。
  232. これを行うには <methodname>setDefaults()</methodname> メソッドあるいは
  233. <methodname>populate()</methodname> メソッドを使用します。
  234. </para>
  235. <programlisting language="php"><![CDATA[
  236. $form->setDefaults($data);
  237. $form->populate($data);
  238. ]]></programlisting>
  239. <para>
  240. 逆に、値を設定したり検証したりした後でフォームをクリアしたいこともあるでしょう。
  241. これを行うには <methodname>reset()</methodname> メソッドを使用します。
  242. </para>
  243. <programlisting language="php"><![CDATA[
  244. $form->reset();
  245. ]]></programlisting>
  246. </sect3>
  247. <sect3 id="zend.form.forms.elements.global">
  248. <title>グローバルな操作</title>
  249. <para>
  250. 時には、すべての要素に対して何らかの操作を行いたくなることもあります。
  251. ありがちな例としては、すべての要素に対してプラグインのプレフィックスを設定したり
  252. デコレータを設定したり、フィルタを設定したりといったものがあります。
  253. 以下に例を示します。
  254. </para>
  255. <example id="zend.form.forms.elements.global.allpaths">
  256. <title>すべての要素に対するプレフィックスパスの設定</title>
  257. <para>
  258. 型を指定してすべての要素に対するプレフィックスパスを設定したり、
  259. グローバルプレフィックスを使用したりする例です。
  260. </para>
  261. <programlisting language="php"><![CDATA[
  262. // グローバルプレフィックスパスの設定:
  263. // プレフィックス My_Foo_Filter、My_Foo_Validate、
  264. // My_Foo_Decorator のパスを作成します。
  265. $form->addElementPrefixPath('My_Foo', 'My/Foo/');
  266. // フィルタのパスのみ:
  267. $form->addElementPrefixPath('My_Foo_Filter',
  268. 'My/Foo/Filter',
  269. 'filter');
  270. // バリデータのパスのみ:
  271. $form->addElementPrefixPath('My_Foo_Validate',
  272. 'My/Foo/Validate',
  273. 'validate');
  274. // デコレータのパスのみ:
  275. $form->addElementPrefixPath('My_Foo_Decorator',
  276. 'My/Foo/Decorator',
  277. 'decorator');
  278. ]]></programlisting>
  279. </example>
  280. <example id="zend.form.forms.elements.global.decorators">
  281. <title>すべての要素に対するデコレータの設定</title>
  282. <para>
  283. すべての要素に対するデコレータを設定できます。
  284. <methodname>setElementDecorators()</methodname> に
  285. <methodname>setDecorators()</methodname>
  286. と同じようなデコレータの配列を渡すと、
  287. 各要素にそれまで設定されていたすべてのデコレータを上書きします。
  288. この例では、単純に ViewHelper デコレータと
  289. Label デコレータを設定します。
  290. </para>
  291. <programlisting language="php"><![CDATA[
  292. $form->setElementDecorators(array(
  293. 'ViewHelper',
  294. 'Label'
  295. ));
  296. ]]></programlisting>
  297. </example>
  298. <example id="zend.form.forms.elements.global.decoratorsFilter">
  299. <title>いくつかの要素に対するデコレータの設定</title>
  300. <para>
  301. 設定したい要素、あるいは設定したくない要素を指定することで、
  302. 一部の要素にだけデコレータを設定することもできます。
  303. <methodname>setElementDecorators()</methodname> の 2 番目の引数に要素名の配列を指定します。
  304. デフォルトでは、ここで指定した要素にのみデコレータが設定されます。
  305. 3 番目の引数を渡すこともできます。
  306. これは、設定「したい」要素なのか設定「したくない」
  307. 要素なのかを表すフラグとなります。
  308. <constant>FALSE</constant> を渡すと、指定した
  309. <emphasis>以外の</emphasis> すべての要素にデコレータを設定します。
  310. このメソッドの標準的な使用法と同様、
  311. 各要素にそれまで設定されていたすべてのデコレータを上書きします。
  312. </para>
  313. <para>
  314. 次の例は、
  315. ViewHelper デコレータと Label デコレータを
  316. 要素 'foo' および 'bar' に対してのみ使用するものです。
  317. </para>
  318. <programlisting language="php"><![CDATA[
  319. $form->setElementDecorators(
  320. array(
  321. 'ViewHelper',
  322. 'Label'
  323. ),
  324. array(
  325. 'foo',
  326. 'bar'
  327. )
  328. );
  329. ]]></programlisting>
  330. <para>
  331. 一方、この例では、ViewHelper デコレータと Label
  332. デコレータを 'foo' と 'bar' <emphasis>以外の</emphasis>
  333. すべての要素に対して使用します。
  334. </para>
  335. <programlisting language="php"><![CDATA[
  336. $form->setElementDecorators(
  337. array(
  338. 'ViewHelper',
  339. 'Label'
  340. ),
  341. array(
  342. 'foo',
  343. 'bar'
  344. ),
  345. false
  346. );
  347. ]]></programlisting>
  348. </example>
  349. <note>
  350. <title>要素によっては不適切なデコレータもある</title>
  351. <para>
  352. <methodname>setElementDecorators()</methodname>
  353. はよい方法に見えますが、時には予期せぬ結果を引き起こすこともあります。
  354. たとえば、ボタン系の要素 (Submit, Button, Reset)
  355. では現在ラベルをボタンの値として使用しています。
  356. そして ViewHelper デコレータや DtDdWrapper デコレータは、
  357. 余計なラベルやエラー、ヒントをレンダリングしないためにのみ用います。
  358. 上の例ではいくつかのラベルが重複してしまいます。
  359. </para>
  360. <para>
  361. 先ほどの例に示したように、設定したい要素/したくない要素
  362. を配列で指定するようにすれば、この問題を回避できます。
  363. </para>
  364. <para>
  365. したがって、このメソッドは注意して使うようにしましょう。
  366. 場合によっては、いくつかの要素を除外するか、
  367. そののデコレータを変更して予期せぬ結果を防ぐ必要があります。
  368. </para>
  369. </note>
  370. <example id="zend.form.forms.elements.global.filters">
  371. <title>すべての要素に対するフィルタの設定</title>
  372. <para>
  373. すべての要素に対して同じフィルタを適用したくなることもよくあります。
  374. たとえば、すべての値を <methodname>trim()</methodname> するなどです。
  375. </para>
  376. <programlisting language="php"><![CDATA[
  377. $form->setElementFilters(array('StringTrim'));
  378. ]]></programlisting>
  379. </example>
  380. </sect3>
  381. <sect3 id="zend.form.forms.elements.methods">
  382. <title>要素を操作するためのメソッド</title>
  383. <para>
  384. 次のメソッドを使用して、要素を操作します。
  385. </para>
  386. <itemizedlist>
  387. <listitem><para>
  388. <methodname>createElement($element, $name = null, $options = null)</methodname>
  389. </para></listitem>
  390. <listitem><para>
  391. <methodname>addElement($element, $name = null, $options = null)</methodname>
  392. </para></listitem>
  393. <listitem><para>
  394. <methodname>addElements(array $elements)</methodname>
  395. </para></listitem>
  396. <listitem><para>
  397. <methodname>setElements(array $elements)</methodname>
  398. </para></listitem>
  399. <listitem><para>
  400. <methodname>getElement($name)</methodname>
  401. </para></listitem>
  402. <listitem><para>
  403. <methodname>getElements()</methodname>
  404. </para></listitem>
  405. <listitem><para>
  406. <methodname>removeElement($name)</methodname>
  407. </para></listitem>
  408. <listitem><para>
  409. <methodname>clearElements()</methodname>
  410. </para></listitem>
  411. <listitem><para>
  412. <methodname>setDefaults(array $defaults)</methodname>
  413. </para></listitem>
  414. <listitem><para>
  415. <methodname>setDefault($name, $value)</methodname>
  416. </para></listitem>
  417. <listitem><para>
  418. <methodname>getValue($name)</methodname>
  419. </para></listitem>
  420. <listitem><para>
  421. <methodname>getValues()</methodname>
  422. </para></listitem>
  423. <listitem><para>
  424. <methodname>getUnfilteredValue($name)</methodname>
  425. </para></listitem>
  426. <listitem><para>
  427. <methodname>getUnfilteredValues()</methodname>
  428. </para></listitem>
  429. <listitem><para>
  430. <methodname>setElementFilters(array $filters)</methodname>
  431. </para></listitem>
  432. <listitem><para>
  433. <methodname>setElementDecorators(array $decorators)</methodname>
  434. </para></listitem>
  435. <listitem><para>
  436. <methodname>addElementPrefixPath($prefix, $path, $type = null)</methodname>
  437. </para></listitem>
  438. <listitem><para>
  439. <methodname>addElementPrefixPaths(array $spec)</methodname>
  440. </para></listitem>
  441. </itemizedlist>
  442. </sect3>
  443. </sect2>
  444. <sect2 id="zend.form.forms.displaygroups">
  445. <title>表示グループ</title>
  446. <para>
  447. 表示グループを使用すると、複数の要素を仮想的にグループ化して
  448. 表示させることができます。
  449. フォーム内の各要素に対しては名前を指定してアクセスできますが、
  450. フォームの要素を順次処理したりレンダリングしたりするときは
  451. 表示グループは一括して扱われます。
  452. 表示グループのもっとも一般的な使用例は、
  453. フィールドセット内の要素をグループ化することです。
  454. </para>
  455. <para>
  456. 表示グループの基底クラスは <classname>Zend_Form_DisplayGroup</classname> です。
  457. このクラスのインスタンスを直接作成することもできますが、
  458. 一般的には <classname>Zend_Form</classname> の
  459. <methodname>addDisplayGroup()</methodname> メソッドでインスタンスを作成することになります。
  460. このメソッドの最初の引数には要素名の配列を渡し、
  461. 表示グループの名前を 2 番目の引数で指定します。
  462. オプションの 3 番目の引数で、設定の配列や
  463. <classname>Zend_Config</classname> オブジェクトを渡すこともできます。
  464. </para>
  465. <para>
  466. 'username' と 'password' という要素がすでにフォームに登録済みである場合に、
  467. 次のコードを使用するとそれらを表示グループ
  468. 'login' にまとめることができます。
  469. </para>
  470. <programlisting language="php"><![CDATA[
  471. $form->addDisplayGroup(array('username', 'password'), 'login');
  472. ]]></programlisting>
  473. <para>
  474. 表示グループにアクセスするには、
  475. <methodname>getDisplayGroup()</methodname> メソッドを使用するか
  476. あるいは表示グループ名を指定します。
  477. </para>
  478. <programlisting language="php"><![CDATA[
  479. // getDisplayGroup() の使用
  480. $login = $form->getDisplayGroup('login');
  481. // オーバーロードの使用
  482. $login = $form->login;
  483. ]]></programlisting>
  484. <note>
  485. <title>読み込み不要なデフォルトのデコレータ</title>
  486. <para>
  487. デフォルトのデコレータは、
  488. オブジェクトの初期化時に読み込まれるようになっています。
  489. この機能を無効にするには、表示グループの作成時にオプション
  490. 'disableLoadDefaultDecorators' を指定します。
  491. </para>
  492. <programlisting language="php"><![CDATA[
  493. $form->addDisplayGroup(
  494. array('foo', 'bar'),
  495. 'foobar',
  496. array('disableLoadDefaultDecorators' => true)
  497. );
  498. ]]></programlisting>
  499. <para>
  500. このオプションは、他のオプションと混用することもできます。
  501. その場合はオプションの配列や <classname>Zend_Config</classname>
  502. オブジェクトを使用します。
  503. </para>
  504. </note>
  505. <sect3 id="zend.form.forms.displaygroups.global">
  506. <title>グローバルな操作</title>
  507. <para>
  508. 要素と同様、表示グループに対しても全体に共通の操作があるでしょう。
  509. デコレータを設定したり、デコレータを探すプラグインパスを設定したりといったものです。
  510. </para>
  511. <example id="zend.form.forms.displaygroups.global.paths">
  512. <title>すべての表示グループに対するデコレータプレフィックスパスの設定</title>
  513. <para>
  514. デフォルトでは、表示グループはフォームが使用しているデコレータパスを継承します。
  515. 別の場所を探すようにしたい場合は
  516. <methodname>addDisplayGroupPrefixPath()</methodname> メソッドを使用します。
  517. </para>
  518. <programlisting language="php"><![CDATA[
  519. $form->addDisplayGroupPrefixPath('My_Foo_Decorator', 'My/Foo/Decorator');
  520. ]]></programlisting>
  521. </example>
  522. <example id="zend.form.forms.displaygroups.global.decorators">
  523. <title>すべての表示グループに対するデコレータの設定</title>
  524. <para>
  525. すべての表示グループに対するデコレータを設定できます。
  526. <methodname>setDisplayGroupDecorators()</methodname> に
  527. <methodname>setDecorators()</methodname>
  528. と同じようなデコレータの配列を渡すと、
  529. 各表示グループにそれまで設定されていたすべてのデコレータを上書きします。
  530. この例では、単純に fieldset デコレータ
  531. (FormElements デコレータは、要素を順次処理するために必須です)
  532. を設定します。
  533. </para>
  534. <programlisting language="php"><![CDATA[
  535. $form->setDisplayGroupDecorators(array(
  536. 'FormElements',
  537. 'Fieldset'
  538. ));
  539. ]]></programlisting>
  540. </example>
  541. </sect3>
  542. <sect3 id="zend.form.forms.displaygroups.customClasses">
  543. <title>独自の表示グループクラスの使用</title>
  544. <para>
  545. デフォルトでは、<classname>Zend_Form</classname> は
  546. 表示グループ用に <classname>Zend_Form_DisplayGroup</classname>
  547. クラスを使用します。
  548. このクラスを継承すれば、独自の機能を持つクラスを作成できます。
  549. <methodname>addDisplayGroup()</methodname>
  550. には具象インスタンスを渡すことはできませんが、
  551. どのクラスを使用するのかをオプションで指定できます。
  552. このときに使用するキーは 'displayGroupClass' です。
  553. </para>
  554. <programlisting language="php"><![CDATA[
  555. // 'My_DisplayGroup' クラスを使用します
  556. $form->addDisplayGroup(
  557. array('username', 'password'),
  558. 'user',
  559. array('displayGroupClass' => 'My_DisplayGroup')
  560. );
  561. ]]></programlisting>
  562. <para>
  563. そのクラスがまだ読み込まれていない場合は、<classname>Zend_Form</classname>
  564. は <classname>Zend_Loader</classname> を使ってクラスを読み込みます。
  565. </para>
  566. <para>
  567. デフォルトの表示グループクラスを指定し、
  568. そのフォームオブジェクトで作成するすべての表示グループで
  569. 同じクラスを使わせることもできます。
  570. </para>
  571. <programlisting language="php"><![CDATA[
  572. // すべての表示グループで 'My_DisplayGroup' クラスを使用します
  573. $form->setDefaultDisplayGroupClass('My_DisplayGroup');
  574. ]]></programlisting>
  575. <para>
  576. これは、設定項目
  577. 'defaultDisplayGroupClass' で指定することもできます。
  578. そうすれば、事前にこのクラスが読み込まれて
  579. すべての表示グループでこのクラスを使うようになります。
  580. </para>
  581. </sect3>
  582. <sect3 id="zend.form.forms.displaygroups.interactionmethods">
  583. <title>表示グループを操作するためのメソッド</title>
  584. <para>
  585. 次のメソッドを使用して、表示グループを操作します。
  586. </para>
  587. <itemizedlist>
  588. <listitem><para>
  589. <methodname>addDisplayGroup(array $elements, $name, $options = null)</methodname>
  590. </para></listitem>
  591. <listitem><para>
  592. <methodname>addDisplayGroups(array $groups)</methodname>
  593. </para></listitem>
  594. <listitem><para>
  595. <methodname>setDisplayGroups(array $groups)</methodname>
  596. </para></listitem>
  597. <listitem><para>
  598. <methodname>getDisplayGroup($name)</methodname>
  599. </para></listitem>
  600. <listitem><para>
  601. <methodname>getDisplayGroups()</methodname>
  602. </para></listitem>
  603. <listitem><para>
  604. <methodname>removeDisplayGroup($name)</methodname>
  605. </para></listitem>
  606. <listitem><para>
  607. <methodname>clearDisplayGroups()</methodname>
  608. </para></listitem>
  609. <listitem><para>
  610. <methodname>setDisplayGroupDecorators(array $decorators)</methodname>
  611. </para></listitem>
  612. <listitem><para>
  613. <methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname>
  614. </para></listitem>
  615. <listitem><para>
  616. <methodname>setDefaultDisplayGroupClass($class)</methodname>
  617. </para></listitem>
  618. <listitem><para>
  619. <methodname>getDefaultDisplayGroupClass($class)</methodname>
  620. </para></listitem>
  621. </itemizedlist>
  622. </sect3>
  623. <sect3 id="zend.form.forms.displaygroups.methods">
  624. <title>Zend_Form_DisplayGroup のメソッド</title>
  625. <para>
  626. <classname>Zend_Form_DisplayGroup</classname> には次のようなメソッドがあります。
  627. タイプ別にまとめます。
  628. </para>
  629. <itemizedlist>
  630. <listitem><para>設定</para>
  631. <itemizedlist>
  632. <listitem><para><methodname>setOptions(array $options)</methodname></para></listitem>
  633. <listitem><para><methodname>setConfig(Zend_Config $config)</methodname></para></listitem>
  634. </itemizedlist>
  635. </listitem>
  636. <listitem><para>メタデータ</para>
  637. <itemizedlist>
  638. <listitem><para><methodname>setAttrib($key, $value)</methodname></para></listitem>
  639. <listitem><para><methodname>addAttribs(array $attribs)</methodname></para></listitem>
  640. <listitem><para><methodname>setAttribs(array $attribs)</methodname></para></listitem>
  641. <listitem><para><methodname>getAttrib($key)</methodname></para></listitem>
  642. <listitem><para><methodname>getAttribs()</methodname></para></listitem>
  643. <listitem><para><methodname>removeAttrib($key)</methodname></para></listitem>
  644. <listitem><para><methodname>clearAttribs()</methodname></para></listitem>
  645. <listitem><para><methodname>setName($name)</methodname></para></listitem>
  646. <listitem><para><methodname>getName()</methodname></para></listitem>
  647. <listitem><para><methodname>setDescription($value)</methodname></para></listitem>
  648. <listitem><para><methodname>getDescription()</methodname></para></listitem>
  649. <listitem><para><methodname>setLegend($legend)</methodname></para></listitem>
  650. <listitem><para><methodname>getLegend()</methodname></para></listitem>
  651. <listitem><para><methodname>setOrder($order)</methodname></para></listitem>
  652. <listitem><para><methodname>getOrder()</methodname></para></listitem>
  653. </itemizedlist>
  654. </listitem>
  655. <listitem><para>要素</para>
  656. <itemizedlist>
  657. <listitem><para><methodname>createElement($type, $name, array $options = array())</methodname></para></listitem>
  658. <listitem><para><methodname>addElement($typeOrElement, $name, array $options = array())</methodname></para></listitem>
  659. <listitem><para><methodname>addElements(array $elements)</methodname></para></listitem>
  660. <listitem><para><methodname>setElements(array $elements)</methodname></para></listitem>
  661. <listitem><para><methodname>getElement($name)</methodname></para></listitem>
  662. <listitem><para><methodname>getElements()</methodname></para></listitem>
  663. <listitem><para><methodname>removeElement($name)</methodname></para></listitem>
  664. <listitem><para><methodname>clearElements()</methodname></para></listitem>
  665. </itemizedlist>
  666. </listitem>
  667. <listitem><para>プラグインローダー</para>
  668. <itemizedlist>
  669. <listitem><para><methodname>setPluginLoader(Zend_Loader_PluginLoader $loader)</methodname></para></listitem>
  670. <listitem><para><methodname>getPluginLoader()</methodname></para></listitem>
  671. <listitem><para><methodname>addPrefixPath($prefix, $path)</methodname></para></listitem>
  672. <listitem><para><methodname>addPrefixPaths(array $spec)</methodname></para></listitem>
  673. </itemizedlist>
  674. </listitem>
  675. <listitem><para>デコレータ</para>
  676. <itemizedlist>
  677. <listitem><para><methodname>addDecorator($decorator, $options = null)</methodname></para></listitem>
  678. <listitem><para><methodname>addDecorators(array $decorators)</methodname></para></listitem>
  679. <listitem><para><methodname>setDecorators(array $decorators)</methodname></para></listitem>
  680. <listitem><para><methodname>getDecorator($name)</methodname></para></listitem>
  681. <listitem><para><methodname>getDecorators()</methodname></para></listitem>
  682. <listitem><para><methodname>removeDecorator($name)</methodname></para></listitem>
  683. <listitem><para><methodname>clearDecorators()</methodname></para></listitem>
  684. </itemizedlist>
  685. </listitem>
  686. <listitem><para>レンダリング</para>
  687. <itemizedlist>
  688. <listitem><para><methodname>setView(Zend_View_Interface $view = null)</methodname></para></listitem>
  689. <listitem><para><methodname>getView()</methodname></para></listitem>
  690. <listitem><para><methodname>render(Zend_View_Interface $view = null)</methodname></para></listitem>
  691. </itemizedlist>
  692. </listitem>
  693. <listitem><para>I18n</para>
  694. <itemizedlist>
  695. <listitem><para><methodname>setTranslator(Zend_Translate_Adapter $translator = null)</methodname></para></listitem>
  696. <listitem><para><methodname>getTranslator()</methodname></para></listitem>
  697. <listitem><para><methodname>setDisableTranslator($flag)</methodname></para></listitem>
  698. <listitem><para><methodname>translatorIsDisabled()</methodname></para></listitem>
  699. </itemizedlist>
  700. </listitem>
  701. </itemizedlist>
  702. </sect3>
  703. </sect2>
  704. <sect2 id="zend.form.forms.subforms">
  705. <title>サブフォーム</title>
  706. <para>
  707. サブフォームの役割は、次のようなものです。
  708. </para>
  709. <itemizedlist>
  710. <listitem><para>
  711. 論理的な要素グループの作成。
  712. サブフォームは単なるフォームであり、
  713. サブフォームを独立したエンティティとして検証できます。
  714. </para></listitem>
  715. <listitem><para>
  716. 複数ページにまたがるフォームの作成。
  717. サブフォームは単なるフォームなので、
  718. ページごとに別々のサブフォームを表示させて複数ページのフォームを作成し、
  719. 各ページで独自のバリデーションを行うことができます。
  720. すべてのサブフォームの検証に成功した時点ではじめて
  721. フォームの検証が完了したことになります。
  722. </para></listitem>
  723. <listitem><para>
  724. 表示のグループ化。
  725. 表示グループと同様、サブフォームについても
  726. フォーム内でのレンダリングの際にはひとつの要素として扱われます。
  727. しかし注意しましょう。
  728. 親フォームからはサブフォームの要素にはアクセスできません。
  729. </para></listitem>
  730. </itemizedlist>
  731. <para>
  732. サブフォームは <classname>Zend_Form</classname> のオブジェクトです。一般的には
  733. <classname>Zend_Form_SubForm</classname> のオブジェクトとなります。
  734. 後者のクラスには、大きなフォームに含めるのに適したデコレータが含まれています
  735. (<acronym>HTML</acronym> の form タグをレンダリングせず、要素をグループ化するものです)。
  736. サブフォームをアタッチするには、単にそれを要素に追加して名前を指定するだけです。
  737. </para>
  738. <programlisting language="php"><![CDATA[
  739. $form->addSubForm($subForm, 'subform');
  740. ]]></programlisting>
  741. <para>
  742. サブフォームを取得するには、
  743. <methodname>getSubForm($name)</methodname> を使用するかあるいはサブフォームの名前を使用します。
  744. </para>
  745. <programlisting language="php"><![CDATA[
  746. // getSubForm() の使用
  747. $subForm = $form->getSubForm('subform');
  748. // オーバーロードの使用
  749. $subForm = $form->subform;
  750. ]]></programlisting>
  751. <para>
  752. サブフォームは、親フォームの要素のひとつとして順次処理できます。
  753. サブフォーム内の要素は処理できません。
  754. </para>
  755. <sect3 id="zend.form.forms.subforms.global">
  756. <title>グローバルな操作</title>
  757. <para>
  758. 要素や表示グループと同様、サブフォームに対しても全体に共通の操作があるでしょう。
  759. しかし、表示グループや要素とは異なり、
  760. サブフォームは親フォームのほとんどの機能を継承しています。
  761. グローバルな操作として本当に必要なのは、サブフォーム用のデコレータの設定くらいです。
  762. そんな場合には <methodname>setSubFormDecorators()</methodname>
  763. メソッドを使用します。次の例では、すべてのサブフォームに対して
  764. 単純に fieldset デコレータを設定します
  765. (FormElements デコレータは、要素を順次処理するために必須です)。
  766. </para>
  767. <programlisting language="php"><![CDATA[
  768. $form->setSubFormDecorators(array(
  769. 'FormElements',
  770. 'Fieldset'
  771. ));
  772. ]]></programlisting>
  773. </sect3>
  774. <sect3 id="zend.form.forms.subforms.methods">
  775. <title>サブフォームを操作するためのメソッド</title>
  776. <para>
  777. 次のメソッドを使用して、サブフォームを操作します。
  778. </para>
  779. <itemizedlist>
  780. <listitem><para>
  781. <methodname>addSubForm(Zend_Form $form, $name, $order = null)</methodname>
  782. </para></listitem>
  783. <listitem><para>
  784. <methodname>addSubForms(array $subForms)</methodname>
  785. </para></listitem>
  786. <listitem><para>
  787. <methodname>setSubForms(array $subForms)</methodname>
  788. </para></listitem>
  789. <listitem><para>
  790. <methodname>getSubForm($name)</methodname>
  791. </para></listitem>
  792. <listitem><para>
  793. <methodname>getSubForms()</methodname>
  794. </para></listitem>
  795. <listitem><para>
  796. <methodname>removeSubForm($name)</methodname>
  797. </para></listitem>
  798. <listitem><para>
  799. <methodname>clearSubForms()</methodname>
  800. </para></listitem>
  801. <listitem><para>
  802. <methodname>setSubFormDecorators(array $decorators)</methodname>
  803. </para></listitem>
  804. </itemizedlist>
  805. </sect3>
  806. </sect2>
  807. <sect2 id="zend.form.forms.metadata">
  808. <title>メタデータおよび属性</title>
  809. <para>
  810. フォームで一番大切なのはフォームが含む要素ですが、
  811. フォームにはそれ以外のメタデータも含まれます。たとえばフォームの名前
  812. (この名前は、<acronym>HTML</acronym> のマークアップ時に ID として用いられます)
  813. やアクション、そしてメソッドなどです。
  814. 要素や表示グループ、サブフォームの数も含まれます。
  815. それ以外にも任意のメタデータを含めることができます
  816. (通常は、form タグで指定する <acronym>HTML</acronym> 属性などをここに含めます)。
  817. </para>
  818. <para>
  819. フォームの名前を設定したり取得したりするには name アクセサを使用します。
  820. </para>
  821. <programlisting language="php"><![CDATA[
  822. // 名前を設定します
  823. $form->setName('registration');
  824. // 名前を取得します
  825. $name = $form->getName();
  826. ]]></programlisting>
  827. <para>
  828. アクション (フォームを送信したときに進む URL)
  829. やメソッド (送信する方法。たとえば 'POST' あるいは '<constant>GET</constant>')
  830. を設定するには、それぞれ action アクセサおよび
  831. method アクセサを使用します。
  832. </para>
  833. <programlisting language="php"><![CDATA[
  834. // アクションとメソッドを設定します
  835. $form->setAction('/user/login')
  836. ->setMethod('post');
  837. ]]></programlisting>
  838. <para>
  839. フォームのエンコード形式を設定するには
  840. enctype アクセサを使用します。
  841. <classname>Zend_Form</classname> では 2 つの定数
  842. <constant>Zend_Form::ENCTYPE_URLENCODED</constant> と
  843. <constant>Zend_Form::ENCTYPE_MULTIPART</constant> を定義しており、
  844. これらはそれぞれ 'application/x-www-form-urlencoded' と
  845. 'multipart/form-data' に対応します。
  846. しかし、これ以外にも任意のエンコード形式を指定できます。
  847. </para>
  848. <programlisting language="php"><![CDATA[
  849. // アクションとメソッド、そして enctype を設定します
  850. $form->setAction('/user/login')
  851. ->setMethod('post')
  852. ->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
  853. ]]></programlisting>
  854. <note>
  855. <para>
  856. メソッドやアクション、enctype はレンダリングの際に内部的に使われるものであり、
  857. バリデーション用のものではありません。
  858. </para>
  859. </note>
  860. <para>
  861. <classname>Zend_Form</classname> は <classname>Countable</classname>
  862. インターフェイスを実装しており、count 関数の引数として使用できます。
  863. </para>
  864. <programlisting language="php"><![CDATA[
  865. $numItems = count($form);
  866. ]]></programlisting>
  867. <para>
  868. 任意のメタデータを設定するには attribs アクセサを使用します。
  869. <classname>Zend_Form</classname> ではオーバーロードを使用して
  870. 要素や表示グループそしてサブフォームにアクセスしているので、
  871. これがメタデータにアクセスするための唯一の方法となります。
  872. </para>
  873. <programlisting language="php"><![CDATA[
  874. // 属性を設定します
  875. $form->setAttrib('class', 'zend-form')
  876. ->addAttribs(array(
  877. 'id' => 'registration',
  878. 'onSubmit' => 'validate(this)',
  879. ));
  880. // 属性を取得します
  881. $class = $form->getAttrib('class');
  882. $attribs = $form->getAttribs();
  883. // 属性を削除します
  884. $form->removeAttrib('onSubmit');
  885. // すべての属性を削除します
  886. $form->clearAttribs();
  887. ]]></programlisting>
  888. </sect2>
  889. <sect2 id="zend.form.forms.decorators">
  890. <title>デコレータ</title>
  891. <para>
  892. フォームのマークアップを作成するのは手間のかかる作業です。
  893. 特に、検証エラーの表示や値の送信などの決まりきった処理を何度も行うのは大変なことです。
  894. この問題に対する <classname>Zend_Form</classname>
  895. からの回答が <emphasis>デコレータ</emphasis> です。
  896. </para>
  897. <para>
  898. <classname>Zend_Form</classname> オブジェクトのデコレータを使用して、
  899. フォームをレンダリングします。
  900. FormElements デコレータは、フォームのすべての項目
  901. (要素、表示グループ、サブフォーム)
  902. を順次処理し、それらをレンダリングした結果を返します。
  903. それ以外のデコレータを使用して、コンテンツをラップしたり
  904. 前後に何かを追加したりできます。
  905. </para>
  906. <para>
  907. <classname>Zend_Form</classname> のデフォルトのデコレータは、FormElements と
  908. HtmlTag (定義リストによるラッピングをします)、そして Form です。
  909. 以下のコードと同様です。
  910. </para>
  911. <programlisting language="php"><![CDATA[
  912. $form->setDecorators(array(
  913. 'FormElements',
  914. array('HtmlTag', array('tag' => 'dl')),
  915. 'Form'
  916. ));
  917. ]]></programlisting>
  918. <para>
  919. これは、次のような出力をします。
  920. </para>
  921. <programlisting language="html"><![CDATA[
  922. <form action="/form/action" method="post">
  923. <dl>
  924. ...
  925. </dl>
  926. </form>
  927. ]]></programlisting>
  928. <para>
  929. フォームオブジェクトに設定した任意の属性は、
  930. <acronym>HTML</acronym> の <emphasis>&lt;form&gt;</emphasis> タグの属性となります。
  931. </para>
  932. <note>
  933. <title>読み込み不要なデフォルトのデコレータ</title>
  934. <para>
  935. デフォルトのデコレータは、
  936. オブジェクトの初期化時に読み込まれるようになっています。
  937. この機能を無効にするには、コンストラクタでオプション
  938. 'disableLoadDefaultDecorators' を指定します。
  939. </para>
  940. <programlisting language="php"><![CDATA[
  941. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  942. ]]></programlisting>
  943. <para>
  944. このオプションは、他のオプションと混用することもできます。
  945. その場合はオプションの配列や <classname>Zend_Config</classname>
  946. オブジェクトを使用します。
  947. </para>
  948. </note>
  949. <note>
  950. <title>同じ型の複数のデコレータの使用法</title>
  951. <para>
  952. 内部的には、<classname>Zend_Form</classname>
  953. はデコレータのクラスをもとにデコレータを取得しています。
  954. その結果、同じ型のデコレータを複数使うことができなくなります。
  955. 同じ型のデコレータを複数指定すると、
  956. 後から指定したものが先に指定したものを上書きします。
  957. </para>
  958. <para>
  959. これを回避するにはエイリアスを使用します。
  960. デコレータやデコレータ名を <methodname>addDecorator()</methodname>
  961. の最初の引数として渡すのではなく、ひとつの要素からなる配列を渡します。
  962. この配列には、デコレータオブジェクトあるいはデコレータ名を指すエイリアスを指定します。
  963. </para>
  964. <programlisting language="php"><![CDATA[
  965. // 'FooBar' へのエイリアス
  966. $form->addDecorator(array('FooBar' => 'HtmlTag'), array('tag' => 'div'));
  967. // 後で、このように取得できます
  968. $form = $element->getDecorator('FooBar');
  969. ]]></programlisting>
  970. <para>
  971. <methodname>addDecorators()</methodname> メソッドおよび
  972. <methodname>setDecorators()</methodname> メソッドでは、
  973. デコレータを表す配列を 'decorator' オプションに渡す必要があります。
  974. </para>
  975. <programlisting language="php"><![CDATA[
  976. // ふたつの 'HtmlTag' デコレータを使用するため、片方に 'FooBar' というエイリアスを指定します
  977. $form->addDecorators(
  978. array('HtmlTag', array('tag' => 'div')),
  979. array(
  980. 'decorator' => array('FooBar' => 'HtmlTag'),
  981. 'options' => array('tag' => 'dd')
  982. ),
  983. );
  984. // 後で、このように取得できます
  985. $htmlTag = $form->getDecorator('HtmlTag');
  986. $fooBar = $form->getDecorator('FooBar');
  987. ]]></programlisting>
  988. </note>
  989. <para>
  990. 独自のデコレータを使用してフォームを作成することもできます。
  991. 一般的な使用例としては、作成したい <acronym>HTML</acronym> が厳密に決まっている場合などがあります。
  992. デコレータでそれとまったく同じ <acronym>HTML</acronym> を作成し、単純にそれを返せばいいのです。
  993. 個々の要素や表示グループに対してもそれぞれデコレータを使用できます。
  994. </para>
  995. <para>
  996. デコレータ関連のメソッドを以下にまとめます。
  997. </para>
  998. <itemizedlist>
  999. <listitem><para>
  1000. <methodname>addDecorator($decorator, $options = null)</methodname>
  1001. </para></listitem>
  1002. <listitem><para>
  1003. <methodname>addDecorators(array $decorators)</methodname>
  1004. </para></listitem>
  1005. <listitem><para>
  1006. <methodname>setDecorators(array $decorators)</methodname>
  1007. </para></listitem>
  1008. <listitem><para>
  1009. <methodname>getDecorator($name)</methodname>
  1010. </para></listitem>
  1011. <listitem><para>
  1012. <methodname>getDecorators()</methodname>
  1013. </para></listitem>
  1014. <listitem><para>
  1015. <methodname>removeDecorator($name)</methodname>
  1016. </para></listitem>
  1017. <listitem><para>
  1018. <methodname>clearDecorators()</methodname>
  1019. </para></listitem>
  1020. </itemizedlist>
  1021. <para>
  1022. <classname>Zend_Form</classname> は、
  1023. オーバーロードを使用して特定のデコレータをレンダリングすることもできます。
  1024. 'render' で始まる名前のメソッドを <methodname>__call()</methodname>
  1025. で捕捉し、メソッド名の残りの部分にもとづいてデコレータを探します。
  1026. 見つかった場合は、そのデコレータ
  1027. <emphasis>だけ</emphasis> をレンダリングします。
  1028. 引数を渡すと、それがデコレータの
  1029. <methodname>render()</methodname> メソッドにコンテンツとして渡されます。次の例を参照ください。
  1030. </para>
  1031. <programlisting language="php"><![CDATA[
  1032. // FormElements デコレータのみをレンダリングします
  1033. echo $form->renderFormElements();
  1034. // fieldset デコレータのみ、コンテンツを渡してレンダリングします
  1035. echo $form->renderFieldset("<p>This is fieldset content</p>");
  1036. ]]></programlisting>
  1037. <para>
  1038. デコレータが存在しない場合は、例外が発生します。
  1039. </para>
  1040. </sect2>
  1041. <sect2 id="zend.form.forms.validation">
  1042. <title>バリデーション</title>
  1043. <para>
  1044. フォームの主な使用法は、送信されたデータを検証することです。
  1045. <classname>Zend_Form</classname> は、フォーム全体あるいはその一部を一度に検証したり、
  1046. XmlHttpRequests (AJAX) のレスポンスを自動的に検証したりできます。
  1047. 送信されたデータが無効な場合は、
  1048. 要素やサブフォームについて
  1049. 検証エラーのコードやメッセージを取得するメソッドが用意されています。
  1050. </para>
  1051. <para>
  1052. フォーム全体のバリデーションを行うには <methodname>isValid()</methodname> メソッドを使用します。
  1053. </para>
  1054. <programlisting language="php"><![CDATA[
  1055. if (!$form->isValid($_POST)) {
  1056. // 検証に失敗しました
  1057. }
  1058. ]]></programlisting>
  1059. <para>
  1060. <methodname>isValid()</methodname> はすべての必須要素の検証を行います。
  1061. また非必須要素の中で値が送信された要素についても検証します。
  1062. </para>
  1063. <para>
  1064. 時には一部のデータだけを検証したいこともあるでしょう。そんな場合には
  1065. <methodname>isValidPartial($data)</methodname> を使用します。
  1066. </para>
  1067. <programlisting language="php"><![CDATA[
  1068. if (!$form->isValidPartial($data)) {
  1069. // 検証に失敗しました
  1070. }
  1071. ]]></programlisting>
  1072. <para>
  1073. <methodname>isValidPartial()</methodname> は、
  1074. data の項目にマッチする要素についてのみ検証を行います。
  1075. マッチする要素がなかった場合は処理をスキップします。
  1076. </para>
  1077. <para>
  1078. <acronym>AJAX</acronym> リクエストの要素や要素グループを検証する際は、
  1079. 通常はフォームの一部を検証してその結果を <acronym>JSON</acronym> で返すことになります。
  1080. <methodname>processAjax()</methodname> が、まさにその作業を行うメソッドです。
  1081. </para>
  1082. <programlisting language="php"><![CDATA[
  1083. $json = $form->processAjax($data);
  1084. ]]></programlisting>
  1085. <para>
  1086. こうすれば、単純に <acronym>JSON</acronym> レスポンスをクライアントに返すことができます。
  1087. フォームの検証に成功した場合は、この結果は <constant>TRUE</constant> となります。
  1088. 失敗した場合は、キーとメッセージのペアを含む javascript
  1089. オブジェクトを返します。個々の 'message'
  1090. が検証エラーメッセージの配列となります。
  1091. </para>
  1092. <para>
  1093. フォームの検証に失敗した場合は、エラーコードとエラーメッセージをそれぞれ
  1094. <methodname>getErrors()</methodname> と
  1095. <methodname>getMessages()</methodname> で取得できます。
  1096. </para>
  1097. <programlisting language="php"><![CDATA[
  1098. $codes = $form->getErrors();
  1099. $messages = $form->getMessages();
  1100. ]]></programlisting>
  1101. <note>
  1102. <para>
  1103. <methodname>getMessages()</methodname> が返すメッセージは エラーコード/エラーメッセージ
  1104. の配列なので、<methodname>getErrors()</methodname> は通常は不要です。
  1105. </para>
  1106. </note>
  1107. <para>
  1108. 個々の要素のコードとエラーメッセージを取得するには、
  1109. それぞれのメソッドに要素名を渡します。
  1110. </para>
  1111. <programlisting language="php"><![CDATA[
  1112. $codes = $form->getErrors('username');
  1113. $messages = $form->getMessages('username');
  1114. ]]></programlisting>
  1115. <note>
  1116. <para>
  1117. 注意: 要素を検証する際、<classname>Zend_Form</classname> は各要素の
  1118. <methodname>isValid()</methodname> メソッドに 2 番目の引数を渡します。
  1119. これは検証対象のデータの配列です。
  1120. これを使用すると、他の要素の入力内容をもとにした検証を
  1121. 各要素で行うことができます。
  1122. 例としては、「パスワード」欄と「パスワード(確認)」
  1123. があるユーザ登録フォームがあります。「パスワード」
  1124. 要素の検証には「パスワード(確認)」の入力内容を使用することになるでしょう。
  1125. </para>
  1126. </note>
  1127. <sect3 id="zend.form.forms.validation.errors">
  1128. <title>独自のエラーメッセージ</title>
  1129. <para>
  1130. 時には、要素にアタッチされたバリデータが生成するエラーメッセージではなく
  1131. 独自のエラーメッセージを指定したくなることもあるでしょう。
  1132. さらに、時には自分自身でフォームを無効だとマークしたいこともあるでしょう。
  1133. 次のメソッドでこの機能を使用できます。
  1134. </para>
  1135. <itemizedlist>
  1136. <listitem><para>
  1137. <methodname>addErrorMessage($message)</methodname>:
  1138. フォームの検証エラーの際に表示するエラーメッセージを追加します。
  1139. 複数回コールすると、新しいメッセージはスタックに追加されます。
  1140. </para></listitem>
  1141. <listitem><para>
  1142. <methodname>addErrorMessages(array $messages)</methodname>:
  1143. フォームの検証エラーの際に表示する複数のエラーメッセージを追加します。
  1144. </para></listitem>
  1145. <listitem><para>
  1146. <methodname>setErrorMessages(array $messages)</methodname>:
  1147. フォームの検証エラーの際に表示する複数のエラーメッセージを追加します。
  1148. それまでに設定されていたすべてのメッセージを上書きします。
  1149. </para></listitem>
  1150. <listitem><para>
  1151. <methodname>getErrorMessages()</methodname>:
  1152. 定義済みのカスタムエラーメッセージの一覧を取得します。
  1153. </para></listitem>
  1154. <listitem><para>
  1155. <methodname>clearErrorMessages()</methodname>:
  1156. 定義済みのカスタムエラーメッセージをすべて削除します。
  1157. </para></listitem>
  1158. <listitem><para>
  1159. <methodname>markAsError()</methodname>:
  1160. 検証に失敗したものとしてフォームにマークします。
  1161. </para></listitem>
  1162. <listitem><para>
  1163. <methodname>addError($message)</methodname>:
  1164. エラーメッセージをカスタムエラーメッセージスタックに追加し、
  1165. フォームを無効とマークします。
  1166. </para></listitem>
  1167. <listitem><para>
  1168. <methodname>addErrors(array $messages)</methodname>:
  1169. 複数のエラーメッセージをカスタムエラーメッセージスタックに追加し、
  1170. フォームを無効とマークします。
  1171. </para></listitem>
  1172. <listitem><para>
  1173. <methodname>setErrors(array $messages)</methodname>:
  1174. 指定したメッセージでカスタムエラーメッセージスタックを上書きし、
  1175. フォームを無効とマークします。
  1176. </para></listitem>
  1177. </itemizedlist>
  1178. <para>
  1179. この方式で設定したすべてのエラーは翻訳されることになります。
  1180. </para>
  1181. </sect3>
  1182. <sect3 id="zend.form.forms.validation.values">
  1183. <title>正しい値だけを取得</title>
  1184. <!-- TODO : to be translated -->
  1185. <para>
  1186. There are scenarios when you want to allow your user to work on a valid form
  1187. in several steps. Meanwhile you allow the user to save the form with any
  1188. set of values inbetween. Then if all the data is specified you can transfer
  1189. the model from the building or prototying stage to a valid stage.
  1190. </para>
  1191. <para>
  1192. You can retrieve all the valid values that match the submitted data by calling:
  1193. </para>
  1194. <programlisting language="php"><![CDATA[
  1195. $validValues = $form->getValidValues($_POST);
  1196. ]]></programlisting>
  1197. </sect3>
  1198. </sect2>
  1199. <sect2 id="zend.form.forms.methods">
  1200. <title>メソッド</title>
  1201. <para>
  1202. 以下の一覧は、<classname>Zend_Form</classname> のメソッドをタイプ別にまとめたものです。
  1203. </para>
  1204. <itemizedlist>
  1205. <listitem><para>設定</para>
  1206. <itemizedlist>
  1207. <listitem><para><methodname>setOptions(array $options)</methodname></para></listitem>
  1208. <listitem><para><methodname>setConfig(Zend_Config $config)</methodname></para></listitem>
  1209. </itemizedlist>
  1210. </listitem>
  1211. <listitem><para>プラグインローダーおよびパス</para>
  1212. <itemizedlist>
  1213. <listitem><para><methodname>setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)</methodname></para></listitem>
  1214. <listitem><para><methodname>getPluginLoader($type = null)</methodname></para></listitem>
  1215. <listitem><para><methodname>addPrefixPath($prefix, $path, $type = null) </methodname></para></listitem>
  1216. <listitem><para><methodname>addPrefixPaths(array $spec)</methodname></para></listitem>
  1217. <listitem><para><methodname>addElementPrefixPath($prefix, $path, $type = null)</methodname></para></listitem>
  1218. <listitem><para><methodname>addElementPrefixPaths(array $spec)</methodname></para></listitem>
  1219. <listitem><para><methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname></para></listitem>
  1220. </itemizedlist>
  1221. </listitem>
  1222. <listitem><para>メタデータ</para>
  1223. <itemizedlist>
  1224. <listitem><para><methodname>setAttrib($key, $value)</methodname></para></listitem>
  1225. <listitem><para><methodname>addAttribs(array $attribs)</methodname></para></listitem>
  1226. <listitem><para><methodname>setAttribs(array $attribs)</methodname></para></listitem>
  1227. <listitem><para><methodname>getAttrib($key)</methodname></para></listitem>
  1228. <listitem><para><methodname>getAttribs()</methodname></para></listitem>
  1229. <listitem><para><methodname>removeAttrib($key)</methodname></para></listitem>
  1230. <listitem><para><methodname>clearAttribs()</methodname></para></listitem>
  1231. <listitem><para><methodname>setAction($action)</methodname></para></listitem>
  1232. <listitem><para><methodname>getAction()</methodname></para></listitem>
  1233. <listitem><para><methodname>setMethod($method)</methodname></para></listitem>
  1234. <listitem><para><methodname>getMethod()</methodname></para></listitem>
  1235. <listitem><para><methodname>setName($name)</methodname></para></listitem>
  1236. <listitem><para><methodname>getName()</methodname></para></listitem>
  1237. </itemizedlist>
  1238. </listitem>
  1239. <listitem><para>要素</para>
  1240. <itemizedlist>
  1241. <listitem><para><methodname>addElement($element, $name = null, $options = null)</methodname></para></listitem>
  1242. <listitem><para><methodname>addElements(array $elements)</methodname></para></listitem>
  1243. <listitem><para><methodname>setElements(array $elements)</methodname></para></listitem>
  1244. <listitem><para><methodname>getElement($name)</methodname></para></listitem>
  1245. <listitem><para><methodname>getElements()</methodname></para></listitem>
  1246. <listitem><para><methodname>removeElement($name)</methodname></para></listitem>
  1247. <listitem><para><methodname>clearElements()</methodname></para></listitem>
  1248. <listitem><para><methodname>setDefaults(array $defaults)</methodname></para></listitem>
  1249. <listitem><para><methodname>setDefault($name, $value)</methodname></para></listitem>
  1250. <listitem><para><methodname>getValue($name)</methodname></para></listitem>
  1251. <listitem><para><methodname>getValues()</methodname></para></listitem>
  1252. <listitem><para><methodname>getUnfilteredValue($name)</methodname></para></listitem>
  1253. <listitem><para><methodname>getUnfilteredValues()</methodname></para></listitem>
  1254. <listitem><para><methodname>setElementFilters(array $filters)</methodname></para></listitem>
  1255. <listitem><para><methodname>setElementDecorators(array $decorators)</methodname></para></listitem>
  1256. </itemizedlist>
  1257. </listitem>
  1258. <listitem><para>サブフォーム</para>
  1259. <itemizedlist>
  1260. <listitem><para><methodname>addSubForm(Zend_Form $form, $name, $order = null)</methodname></para></listitem>
  1261. <listitem><para><methodname>addSubForms(array $subForms)</methodname></para></listitem>
  1262. <listitem><para><methodname>setSubForms(array $subForms)</methodname></para></listitem>
  1263. <listitem><para><methodname>getSubForm($name)</methodname></para></listitem>
  1264. <listitem><para><methodname>getSubForms()</methodname></para></listitem>
  1265. <listitem><para><methodname>removeSubForm($name)</methodname></para></listitem>
  1266. <listitem><para><methodname>clearSubForms()</methodname></para></listitem>
  1267. <listitem><para><methodname>setSubFormDecorators(array $decorators)</methodname></para></listitem>
  1268. </itemizedlist>
  1269. </listitem>
  1270. <listitem><para>表示グループ</para>
  1271. <itemizedlist>
  1272. <listitem><para><methodname>addDisplayGroup(array $elements, $name, $options = null)</methodname></para></listitem>
  1273. <listitem><para><methodname>addDisplayGroups(array $groups)</methodname></para></listitem>
  1274. <listitem><para><methodname>setDisplayGroups(array $groups)</methodname></para></listitem>
  1275. <listitem><para><methodname>getDisplayGroup($name)</methodname></para></listitem>
  1276. <listitem><para><methodname>getDisplayGroups()</methodname></para></listitem>
  1277. <listitem><para><methodname>removeDisplayGroup($name)</methodname></para></listitem>
  1278. <listitem><para><methodname>clearDisplayGroups()</methodname></para></listitem>
  1279. <listitem><para><methodname>setDisplayGroupDecorators(array $decorators)</methodname></para></listitem>
  1280. </itemizedlist>
  1281. </listitem>
  1282. <listitem><para>検証</para>
  1283. <itemizedlist>
  1284. <listitem><para><methodname>populate(array $values)</methodname></para></listitem>
  1285. <listitem><para><methodname>isValid(array $data)</methodname></para></listitem>
  1286. <listitem><para><methodname>isValidPartial(array $data)</methodname></para></listitem>
  1287. <listitem><para><methodname>processAjax(array $data)</methodname></para></listitem>
  1288. <listitem><para><methodname>persistData()</methodname></para></listitem>
  1289. <listitem><para><methodname>getErrors($name = null)</methodname></para></listitem>
  1290. <listitem><para><methodname>getMessages($name = null)</methodname></para></listitem>
  1291. </itemizedlist>
  1292. </listitem>
  1293. <listitem><para>レンダリング</para>
  1294. <itemizedlist>
  1295. <listitem><para><methodname>setView(Zend_View_Interface $view = null)</methodname></para></listitem>
  1296. <listitem><para><methodname>getView()</methodname></para></listitem>
  1297. <listitem><para><methodname>addDecorator($decorator, $options = null)</methodname></para></listitem>
  1298. <listitem><para><methodname>addDecorators(array $decorators)</methodname></para></listitem>
  1299. <listitem><para><methodname>setDecorators(array $decorators)</methodname></para></listitem>
  1300. <listitem><para><methodname>getDecorator($name)</methodname></para></listitem>
  1301. <listitem><para><methodname>getDecorators()</methodname></para></listitem>
  1302. <listitem><para><methodname>removeDecorator($name)</methodname></para></listitem>
  1303. <listitem><para><methodname>clearDecorators()</methodname></para></listitem>
  1304. <listitem><para><methodname>render(Zend_View_Interface $view = null)</methodname></para></listitem>
  1305. </itemizedlist>
  1306. </listitem>
  1307. <listitem><para>I18n</para>
  1308. <itemizedlist>
  1309. <listitem><para><methodname>setTranslator(Zend_Translate_Adapter $translator = null)</methodname></para></listitem>
  1310. <listitem><para><methodname>getTranslator()</methodname></para></listitem>
  1311. <listitem><para><methodname>setDisableTranslator($flag)</methodname></para></listitem>
  1312. <listitem><para><methodname>translatorIsDisabled()</methodname></para></listitem>
  1313. </itemizedlist>
  1314. </listitem>
  1315. </itemizedlist>
  1316. </sect2>
  1317. <sect2 id="zend.form.forms.config">
  1318. <title>設定</title>
  1319. <para>
  1320. <classname>Zend_Form</classname> は、
  1321. <methodname>setOptions()</methodname> や <methodname>setConfig()</methodname> を用いて
  1322. (あるいはコンストラクタでオプションや
  1323. <classname>Zend_Config</classname> オブジェクトを渡して)
  1324. さまざまに設定できます。
  1325. これらのメソッドを使用して、フォームの要素や表示グループ、デコレータ、
  1326. そしてメタデータを設定できます。
  1327. </para>
  1328. <para>
  1329. 全般的なルールとして、もし 'set' + オプションのキー
  1330. という名前のメソッドが <classname>Zend_Form</classname>
  1331. にあった場合は、そのメソッドを使用するとオプションを設定できます。
  1332. アクセサが存在しない場合は
  1333. キーが属性への参照とみなされ、
  1334. <methodname>setAttrib()</methodname> に渡されます。
  1335. </para>
  1336. <para>
  1337. このルールには、次のような例外があります。
  1338. </para>
  1339. <itemizedlist>
  1340. <listitem><para>
  1341. <property>prefixPath</property> は
  1342. <methodname>addPrefixPaths()</methodname> に渡されます。
  1343. </para></listitem>
  1344. <listitem><para>
  1345. <property>elementPrefixPath</property> は
  1346. <methodname>addElementPrefixPaths()</methodname> に渡されます。
  1347. </para></listitem>
  1348. <listitem><para>
  1349. <property>displayGroupPrefixPath</property> は
  1350. <methodname>addDisplayGroupPrefixPaths()</methodname> に渡されます。
  1351. </para></listitem>
  1352. <listitem>
  1353. <para>以下のセッターはこの方式では設定できません。</para>
  1354. <itemizedlist>
  1355. <listitem><para><property>setAttrib (ただし、setAttribs は動作します)</property></para></listitem>
  1356. <listitem><para><property>setConfig</property></para></listitem>
  1357. <listitem><para><property>setDefault</property></para></listitem>
  1358. <listitem><para><property>setOptions</property></para></listitem>
  1359. <listitem><para><property>setPluginLoader</property></para></listitem>
  1360. <listitem><para><property>setSubForms</property></para></listitem>
  1361. <listitem><para><property>setTranslator</property></para></listitem>
  1362. <listitem><para><property>setView</property></para></listitem>
  1363. </itemizedlist>
  1364. </listitem>
  1365. </itemizedlist>
  1366. <para>
  1367. 例として、すべての型の設定データを渡すファイルを見てみましょう。
  1368. </para>
  1369. <programlisting language="ini"><![CDATA[
  1370. [element]
  1371. name = "registration"
  1372. action = "/user/register"
  1373. method = "post"
  1374. attribs.class = "zend_form"
  1375. attribs.onclick = "validate(this)"
  1376. disableTranslator = 0
  1377. prefixPath.element.prefix = "My_Element"
  1378. prefixPath.element.path = "My/Element/"
  1379. elementPrefixPath.validate.prefix = "My_Validate"
  1380. elementPrefixPath.validate.path = "My/Validate/"
  1381. displayGroupPrefixPath.prefix = "My_Group"
  1382. displayGroupPrefixPath.path = "My/Group/"
  1383. elements.username.type = "text"
  1384. elements.username.options.label = "Username"
  1385. elements.username.options.validators.alpha.validator = "Alpha"
  1386. elements.username.options.filters.lcase = "StringToLower"
  1387. ; more elements, of course...
  1388. elementFilters.trim = "StringTrim"
  1389. ;elementDecorators.trim = "StringTrim"
  1390. displayGroups.login.elements.username = "username"
  1391. displayGroups.login.elements.password = "password"
  1392. displayGroupDecorators.elements.decorator = "FormElements"
  1393. displayGroupDecorators.fieldset.decorator = "Fieldset"
  1394. decorators.elements.decorator = "FormElements"
  1395. decorators.fieldset.decorator = "FieldSet"
  1396. decorators.fieldset.decorator.options.class = "zend_form"
  1397. decorators.form.decorator = "Form"
  1398. ]]></programlisting>
  1399. <para>
  1400. これは、<acronym>XML</acronym> や <acronym>PHP</acronym>
  1401. の配列形式の設定ファイルにも簡単に置き換えることができます。
  1402. </para>
  1403. </sect2>
  1404. <sect2 id="zend.form.forms.custom">
  1405. <title>カスタムフォーム</title>
  1406. <para>
  1407. 独自の設定のフォームを使用するもうひとつの方法は、
  1408. <classname>Zend_Form</classname> のサブクラスを作成することです。
  1409. これには次のような利点があります。
  1410. </para>
  1411. <itemizedlist>
  1412. <listitem><para>
  1413. フォームのユニットテストを容易に行うことができ、
  1414. 検証やレンダリングが期待通りのものかを確認しやすくなる。
  1415. </para></listitem>
  1416. <listitem><para>
  1417. 個々の要素に対する決め細やかな制御ができる。
  1418. </para></listitem>
  1419. <listitem><para>
  1420. フォームオブジェクトの再利用がしやすくなり、移植性が高まる
  1421. (設定ファイルの内容を気にする必要がなくなる)。
  1422. </para></listitem>
  1423. <listitem><para>
  1424. 独自の機能を実装できる。
  1425. </para></listitem>
  1426. </itemizedlist>
  1427. <para>
  1428. 一般的な使用法としては、<methodname>init()</methodname>
  1429. メソッドでフォーム要素などの設定を行います。
  1430. </para>
  1431. <programlisting language="php"><![CDATA[
  1432. class My_Form_Login extends Zend_Form
  1433. {
  1434. public function init()
  1435. {
  1436. $username = new Zend_Form_Element_Text('username');
  1437. $username->class = 'formtext';
  1438. $username->setLabel('Username:')
  1439. ->setDecorators(array(
  1440. array('ViewHelper',
  1441. array('helper' => 'formText')),
  1442. array('Label',
  1443. array('class' => 'label'))
  1444. ));
  1445. $password = new Zend_Form_Element_Password('password');
  1446. $password->class = 'formtext';
  1447. $password->setLabel('Password:')
  1448. ->setDecorators(array(
  1449. array('ViewHelper',
  1450. array('helper' => 'formPassword')),
  1451. array('Label',
  1452. array('class' => 'label'))
  1453. ));
  1454. $submit = new Zend_Form_Element_Submit('login');
  1455. $submit->class = 'formsubmit';
  1456. $submit->setValue('Login')
  1457. ->setDecorators(array(
  1458. array('ViewHelper',
  1459. array('helper' => 'formSubmit'))
  1460. ));
  1461. $this->addElements(array(
  1462. $username,
  1463. $password,
  1464. $submit
  1465. ));
  1466. $this->setDecorators(array(
  1467. 'FormElements',
  1468. 'Fieldset',
  1469. 'Form'
  1470. ));
  1471. }
  1472. }
  1473. ]]></programlisting>
  1474. <para>
  1475. このフォームのインスタンスを作成するのは、次のように簡単です。
  1476. </para>
  1477. <programlisting language="php"><![CDATA[
  1478. $form = new My_Form_Login();
  1479. ]]></programlisting>
  1480. <para>
  1481. そして、すべての機能はすでに準備済みの状態で、
  1482. 設定ファイルは不要です (この例はかなり単純化していることに注意しましょう。
  1483. バリデーションや要素のフィルタなどは省略しています)。
  1484. </para>
  1485. <para>
  1486. 独自のフォームを作成する理由としてもうひとつよくあるのが、
  1487. デフォルトのデコレータを定義したいというものです。
  1488. この場合は <methodname>loadDefaultDecorators()</methodname> メソッドを上書きします。
  1489. </para>
  1490. <programlisting language="php"><![CDATA[
  1491. class My_Form_Login extends Zend_Form
  1492. {
  1493. public function loadDefaultDecorators()
  1494. {
  1495. $this->setDecorators(array(
  1496. 'FormElements',
  1497. 'Fieldset',
  1498. 'Form'
  1499. ));
  1500. }
  1501. }
  1502. ]]></programlisting>
  1503. </sect2>
  1504. </sect1>
  1505. <!--
  1506. vim:se ts=4 sw=4 et:
  1507. -->