2
0

Zend_Form-Forms.xml 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 17618 -->
  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 が null の場合はそのパスをすべてのローダーに追加します。この場合、
  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. false を渡すと、指定した
  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. (HTML の 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. (この名前は、HTML のマークアップ時に ID として用いられます)
  813. やアクション、そしてメソッドなどです。
  814. 要素や表示グループ、サブフォームの数も含まれます。
  815. それ以外にも任意のメタデータを含めることができます
  816. (通常は、form タグで指定する HTML 属性などをここに含めます)。
  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' あるいは 'GET')
  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> は <code>Countable</code>
  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. HTML の <code>&lt;form&gt;</code> タグの属性となります。
  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. 一般的な使用例としては、作成したい HTML が厳密に決まっている場合などがあります。
  992. デコレータでそれとまったく同じ HTML を作成し、単純にそれを返せばいいのです。
  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. フォームの検証に成功した場合は、この結果は true となります。
  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. </sect2>
  1183. <sect2 id="zend.form.forms.methods">
  1184. <title>メソッド</title>
  1185. <para>
  1186. 以下の一覧は、<classname>Zend_Form</classname> のメソッドをタイプ別にまとめたものです。
  1187. </para>
  1188. <itemizedlist>
  1189. <listitem><para>設定</para>
  1190. <itemizedlist>
  1191. <listitem><para><methodname>setOptions(array $options)</methodname></para></listitem>
  1192. <listitem><para><methodname>setConfig(Zend_Config $config)</methodname></para></listitem>
  1193. </itemizedlist>
  1194. </listitem>
  1195. <listitem><para>プラグインローダーおよびパス</para>
  1196. <itemizedlist>
  1197. <listitem><para><methodname>setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)</methodname></para></listitem>
  1198. <listitem><para><methodname>getPluginLoader($type = null)</methodname></para></listitem>
  1199. <listitem><para><methodname>addPrefixPath($prefix, $path, $type = null) </methodname></para></listitem>
  1200. <listitem><para><methodname>addPrefixPaths(array $spec)</methodname></para></listitem>
  1201. <listitem><para><methodname>addElementPrefixPath($prefix, $path, $type = null)</methodname></para></listitem>
  1202. <listitem><para><methodname>addElementPrefixPaths(array $spec)</methodname></para></listitem>
  1203. <listitem><para><methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname></para></listitem>
  1204. </itemizedlist>
  1205. </listitem>
  1206. <listitem><para>メタデータ</para>
  1207. <itemizedlist>
  1208. <listitem><para><methodname>setAttrib($key, $value)</methodname></para></listitem>
  1209. <listitem><para><methodname>addAttribs(array $attribs)</methodname></para></listitem>
  1210. <listitem><para><methodname>setAttribs(array $attribs)</methodname></para></listitem>
  1211. <listitem><para><methodname>getAttrib($key)</methodname></para></listitem>
  1212. <listitem><para><methodname>getAttribs()</methodname></para></listitem>
  1213. <listitem><para><methodname>removeAttrib($key)</methodname></para></listitem>
  1214. <listitem><para><methodname>clearAttribs()</methodname></para></listitem>
  1215. <listitem><para><methodname>setAction($action)</methodname></para></listitem>
  1216. <listitem><para><methodname>getAction()</methodname></para></listitem>
  1217. <listitem><para><methodname>setMethod($method)</methodname></para></listitem>
  1218. <listitem><para><methodname>getMethod()</methodname></para></listitem>
  1219. <listitem><para><methodname>setName($name)</methodname></para></listitem>
  1220. <listitem><para><methodname>getName()</methodname></para></listitem>
  1221. </itemizedlist>
  1222. </listitem>
  1223. <listitem><para>要素</para>
  1224. <itemizedlist>
  1225. <listitem><para><methodname>addElement($element, $name = null, $options = null)</methodname></para></listitem>
  1226. <listitem><para><methodname>addElements(array $elements)</methodname></para></listitem>
  1227. <listitem><para><methodname>setElements(array $elements)</methodname></para></listitem>
  1228. <listitem><para><methodname>getElement($name)</methodname></para></listitem>
  1229. <listitem><para><methodname>getElements()</methodname></para></listitem>
  1230. <listitem><para><methodname>removeElement($name)</methodname></para></listitem>
  1231. <listitem><para><methodname>clearElements()</methodname></para></listitem>
  1232. <listitem><para><methodname>setDefaults(array $defaults)</methodname></para></listitem>
  1233. <listitem><para><methodname>setDefault($name, $value)</methodname></para></listitem>
  1234. <listitem><para><methodname>getValue($name)</methodname></para></listitem>
  1235. <listitem><para><methodname>getValues()</methodname></para></listitem>
  1236. <listitem><para><methodname>getUnfilteredValue($name)</methodname></para></listitem>
  1237. <listitem><para><methodname>getUnfilteredValues()</methodname></para></listitem>
  1238. <listitem><para><methodname>setElementFilters(array $filters)</methodname></para></listitem>
  1239. <listitem><para><methodname>setElementDecorators(array $decorators)</methodname></para></listitem>
  1240. </itemizedlist>
  1241. </listitem>
  1242. <listitem><para>サブフォーム</para>
  1243. <itemizedlist>
  1244. <listitem><para><methodname>addSubForm(Zend_Form $form, $name, $order = null)</methodname></para></listitem>
  1245. <listitem><para><methodname>addSubForms(array $subForms)</methodname></para></listitem>
  1246. <listitem><para><methodname>setSubForms(array $subForms)</methodname></para></listitem>
  1247. <listitem><para><methodname>getSubForm($name)</methodname></para></listitem>
  1248. <listitem><para><methodname>getSubForms()</methodname></para></listitem>
  1249. <listitem><para><methodname>removeSubForm($name)</methodname></para></listitem>
  1250. <listitem><para><methodname>clearSubForms()</methodname></para></listitem>
  1251. <listitem><para><methodname>setSubFormDecorators(array $decorators)</methodname></para></listitem>
  1252. </itemizedlist>
  1253. </listitem>
  1254. <listitem><para>表示グループ</para>
  1255. <itemizedlist>
  1256. <listitem><para><methodname>addDisplayGroup(array $elements, $name, $options = null)</methodname></para></listitem>
  1257. <listitem><para><methodname>addDisplayGroups(array $groups)</methodname></para></listitem>
  1258. <listitem><para><methodname>setDisplayGroups(array $groups)</methodname></para></listitem>
  1259. <listitem><para><methodname>getDisplayGroup($name)</methodname></para></listitem>
  1260. <listitem><para><methodname>getDisplayGroups()</methodname></para></listitem>
  1261. <listitem><para><methodname>removeDisplayGroup($name)</methodname></para></listitem>
  1262. <listitem><para><methodname>clearDisplayGroups()</methodname></para></listitem>
  1263. <listitem><para><methodname>setDisplayGroupDecorators(array $decorators)</methodname></para></listitem>
  1264. </itemizedlist>
  1265. </listitem>
  1266. <listitem><para>検証</para>
  1267. <itemizedlist>
  1268. <listitem><para><methodname>populate(array $values)</methodname></para></listitem>
  1269. <listitem><para><methodname>isValid(array $data)</methodname></para></listitem>
  1270. <listitem><para><methodname>isValidPartial(array $data)</methodname></para></listitem>
  1271. <listitem><para><methodname>processAjax(array $data)</methodname></para></listitem>
  1272. <listitem><para><methodname>persistData()</methodname></para></listitem>
  1273. <listitem><para><methodname>getErrors($name = null)</methodname></para></listitem>
  1274. <listitem><para><methodname>getMessages($name = null)</methodname></para></listitem>
  1275. </itemizedlist>
  1276. </listitem>
  1277. <listitem><para>レンダリング</para>
  1278. <itemizedlist>
  1279. <listitem><para><methodname>setView(Zend_View_Interface $view = null)</methodname></para></listitem>
  1280. <listitem><para><methodname>getView()</methodname></para></listitem>
  1281. <listitem><para><methodname>addDecorator($decorator, $options = null)</methodname></para></listitem>
  1282. <listitem><para><methodname>addDecorators(array $decorators)</methodname></para></listitem>
  1283. <listitem><para><methodname>setDecorators(array $decorators)</methodname></para></listitem>
  1284. <listitem><para><methodname>getDecorator($name)</methodname></para></listitem>
  1285. <listitem><para><methodname>getDecorators()</methodname></para></listitem>
  1286. <listitem><para><methodname>removeDecorator($name)</methodname></para></listitem>
  1287. <listitem><para><methodname>clearDecorators()</methodname></para></listitem>
  1288. <listitem><para><methodname>render(Zend_View_Interface $view = null)</methodname></para></listitem>
  1289. </itemizedlist>
  1290. </listitem>
  1291. <listitem><para>I18n</para>
  1292. <itemizedlist>
  1293. <listitem><para><methodname>setTranslator(Zend_Translate_Adapter $translator = null)</methodname></para></listitem>
  1294. <listitem><para><methodname>getTranslator()</methodname></para></listitem>
  1295. <listitem><para><methodname>setDisableTranslator($flag)</methodname></para></listitem>
  1296. <listitem><para><methodname>translatorIsDisabled()</methodname></para></listitem>
  1297. </itemizedlist>
  1298. </listitem>
  1299. </itemizedlist>
  1300. </sect2>
  1301. <sect2 id="zend.form.forms.config">
  1302. <title>設定</title>
  1303. <para>
  1304. <classname>Zend_Form</classname> は、
  1305. <methodname>setOptions()</methodname> や <methodname>setConfig()</methodname> を用いて
  1306. (あるいはコンストラクタでオプションや
  1307. <classname>Zend_Config</classname> オブジェクトを渡して)
  1308. さまざまに設定することができます。
  1309. これらのメソッドを使用して、フォームの要素や表示グループ、デコレータ、
  1310. そしてメタデータを設定することができます。
  1311. </para>
  1312. <para>
  1313. 全般的なルールとして、もし 'set' + オプションのキー
  1314. という名前のメソッドが <classname>Zend_Form</classname>
  1315. にあった場合は、そのメソッドを使用するとオプションを設定することができます。
  1316. アクセサが存在しない場合は
  1317. キーが属性への参照とみなされ、
  1318. <methodname>setAttrib()</methodname> に渡されます。
  1319. </para>
  1320. <para>
  1321. このルールには、次のような例外があります。
  1322. </para>
  1323. <itemizedlist>
  1324. <listitem><para>
  1325. <code>prefixPaths</code> は
  1326. <methodname>addPrefixPaths()</methodname> に渡されます。
  1327. </para></listitem>
  1328. <listitem><para>
  1329. <code>elementPrefixPaths</code> は
  1330. <methodname>addElementPrefixPaths()</methodname> に渡されます。
  1331. </para></listitem>
  1332. <listitem><para>
  1333. <code>displayGroupPrefixPaths</code> は
  1334. <methodname>addDisplayGroupPrefixPaths()</methodname> に渡されます。
  1335. </para></listitem>
  1336. <listitem>
  1337. <para>以下のセッターはこの方式では設定できません。</para>
  1338. <itemizedlist>
  1339. <listitem><para><code>setAttrib (ただし、setAttribs は動作します)</code></para></listitem>
  1340. <listitem><para><code>setConfig</code></para></listitem>
  1341. <listitem><para><code>setDefault</code></para></listitem>
  1342. <listitem><para><code>setOptions</code></para></listitem>
  1343. <listitem><para><code>setPluginLoader</code></para></listitem>
  1344. <listitem><para><code>setSubForms</code></para></listitem>
  1345. <listitem><para><code>setTranslator</code></para></listitem>
  1346. <listitem><para><code>setView</code></para></listitem>
  1347. </itemizedlist>
  1348. </listitem>
  1349. </itemizedlist>
  1350. <para>
  1351. 例として、すべての型の設定データを渡すファイルを見てみましょう。
  1352. </para>
  1353. <programlisting language="ini"><![CDATA[
  1354. [element]
  1355. name = "registration"
  1356. action = "/user/register"
  1357. method = "post"
  1358. attribs.class = "zend_form"
  1359. attribs.onclick = "validate(this)"
  1360. disableTranslator = 0
  1361. prefixPath.element.prefix = "My_Element"
  1362. prefixPath.element.path = "My/Element/"
  1363. elementPrefixPath.validate.prefix = "My_Validate"
  1364. elementPrefixPath.validate.path = "My/Validate/"
  1365. displayGroupPrefixPath.prefix = "My_Group"
  1366. displayGroupPrefixPath.path = "My/Group/"
  1367. elements.username.type = "text"
  1368. elements.username.options.label = "Username"
  1369. elements.username.options.validators.alpha.validator = "Alpha"
  1370. elements.username.options.filters.lcase = "StringToLower"
  1371. ; more elements, of course...
  1372. elementFilters.trim = "StringTrim"
  1373. ;elementDecorators.trim = "StringTrim"
  1374. displayGroups.login.elements.username = "username"
  1375. displayGroups.login.elements.password = "password"
  1376. displayGroupDecorators.elements.decorator = "FormElements"
  1377. displayGroupDecorators.fieldset.decorator = "Fieldset"
  1378. decorators.elements.decorator = "FormElements"
  1379. decorators.fieldset.decorator = "FieldSet"
  1380. decorators.fieldset.decorator.options.class = "zend_form"
  1381. decorators.form.decorator = "Form"
  1382. ]]></programlisting>
  1383. <para>
  1384. これは、<acronym>XML</acronym> や <acronym>PHP</acronym>
  1385. の配列形式の設定ファイルにも簡単に置き換えることができます。
  1386. </para>
  1387. </sect2>
  1388. <sect2 id="zend.form.forms.custom">
  1389. <title>カスタムフォーム</title>
  1390. <para>
  1391. 独自の設定のフォームを使用するもうひとつの方法は、
  1392. <classname>Zend_Form</classname> のサブクラスを作成することです。
  1393. これには次のような利点があります。
  1394. </para>
  1395. <itemizedlist>
  1396. <listitem><para>
  1397. フォームのユニットテストを容易に行うことができ、
  1398. 検証やレンダリングが期待通りのものかを確認しやすくなる。
  1399. </para></listitem>
  1400. <listitem><para>
  1401. 個々の要素に対する決め細やかな制御ができる。
  1402. </para></listitem>
  1403. <listitem><para>
  1404. フォームオブジェクトの再利用がしやすくなり、移植性が高まる
  1405. (設定ファイルの内容を気にする必要がなくなる)。
  1406. </para></listitem>
  1407. <listitem><para>
  1408. 独自の機能を実装できる。
  1409. </para></listitem>
  1410. </itemizedlist>
  1411. <para>
  1412. 一般的な使用法としては、<methodname>init()</methodname>
  1413. メソッドでフォーム要素などの設定を行います。
  1414. </para>
  1415. <programlisting language="php"><![CDATA[
  1416. class My_Form_Login extends Zend_Form
  1417. {
  1418. public function init()
  1419. {
  1420. $username = new Zend_Form_Element_Text('username');
  1421. $username->class = 'formtext';
  1422. $username->setLabel('Username:')
  1423. ->setDecorators(array(
  1424. array('ViewHelper',
  1425. array('helper' => 'formText')),
  1426. array('Label',
  1427. array('class' => 'label'))
  1428. ));
  1429. $password = new Zend_Form_Element_Password('password');
  1430. $password->class = 'formtext';
  1431. $password->setLabel('Username:')
  1432. ->setDecorators(array(
  1433. array('ViewHelper',
  1434. array('helper' => 'formPassword')),
  1435. array('Label',
  1436. array('class' => 'label'))
  1437. ));
  1438. $submit = new Zend_Form_Element_Submit('login');
  1439. $submit->class = 'formsubmit';
  1440. $submit->setValue('Login')
  1441. ->setDecorators(array(
  1442. array('ViewHelper',
  1443. array('helper' => 'formSubmit'))
  1444. ));
  1445. $this->addElements(array(
  1446. $username,
  1447. $password,
  1448. $submit
  1449. ));
  1450. $this->setDecorators(array(
  1451. 'FormElements',
  1452. 'Fieldset',
  1453. 'Form'
  1454. ));
  1455. }
  1456. }
  1457. ]]></programlisting>
  1458. <para>
  1459. このフォームのインスタンスを作成するのは、次のように簡単です。
  1460. </para>
  1461. <programlisting language="php"><![CDATA[
  1462. $form = new My_Form_Login();
  1463. ]]></programlisting>
  1464. <para>
  1465. そして、すべての機能はすでに準備済みの状態で、
  1466. 設定ファイルは不要です (この例はかなり単純化していることに注意しましょう。
  1467. バリデーションや要素のフィルタなどは省略しています)。
  1468. </para>
  1469. <para>
  1470. 独自のフォームを作成する理由としてもうひとつよくあるのが、
  1471. デフォルトのデコレータを定義したいというものです。
  1472. この場合は <methodname>loadDefaultDecorators()</methodname> メソッドを上書きします。
  1473. </para>
  1474. <programlisting language="php"><![CDATA[
  1475. class My_Form_Login extends Zend_Form
  1476. {
  1477. public function loadDefaultDecorators()
  1478. {
  1479. $this->setDecorators(array(
  1480. 'FormElements',
  1481. 'Fieldset',
  1482. 'Form'
  1483. ));
  1484. }
  1485. }
  1486. ]]></programlisting>
  1487. </sect2>
  1488. </sect1>
  1489. <!--
  1490. vim:se ts=4 sw=4 et:
  1491. -->