Zend_Form-Elements.xml 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. <sect1 id="zend.form.elements">
  2. <title>Tworzenie elementów formularza za pomocą klasy Zend_Form_Element</title>
  3. <para>
  4. Formularz składa się z elementów, które zazwyczaj odpowiadają elementom
  5. formularzy HTML. Klasa Zend_Form_Element obsługuje pojedyncze elementy
  6. formularza w takich zakresach:
  7. </para>
  8. <itemizedlist>
  9. <listitem>
  10. <para>
  11. weryfikacja (czy wysłane dane są poprawne?)
  12. </para>
  13. <itemizedlist>
  14. <listitem><para>przechowywanie kodów i komunikatów o błędach jakie
  15. wystąpiły podczas weryfikacji</para></listitem>
  16. </itemizedlist>
  17. </listitem>
  18. <listitem><para>
  19. filtrowanie (w jaki sposób element jest przygotowany do weryfikacji i wyświetlenia?)
  20. </para></listitem>
  21. <listitem><para>
  22. renderowanie (jak element jest wyświetlany?)
  23. </para></listitem>
  24. <listitem><para>
  25. dane meta i atrybuty (jakie dodatkowe informacje opisują element?)
  26. </para></listitem>
  27. </itemizedlist>
  28. <para>
  29. Klasa bazowa <code>Zend_Form_Element</code> jest domyślnie skonfigurowana
  30. dla wielu przypadków użycia, jednak najlepiej jest rozszerzyć tę klasę
  31. aby utworzyć najczęściej używane elementy. Dodatkowo Zend Framework
  32. zawiera pewną ilość standardowych elementów XHTML; możesz o nich przeczytać <link
  33. linkend="zend.form.standardElements">w rozdziale Standardowe Elementy</link>.
  34. </para>
  35. <sect2 id="zend.form.elements.loaders">
  36. <title>Ładowanie wtyczek</title>
  37. <para>
  38. Klasa <code>Zend_Form_Element</code> używa klasy <link
  39. linkend="zend.loader.pluginloader">Zend_Loader_PluginLoader</link>
  40. aby umożliwić programistom określenie ścieżek, w których znajdują
  41. się alternatywne weryfikatory, filtry i dekoratory. Każda z tych wtyczek
  42. ma przypisaną własną klasę ładującą je, a do ich modyfikacji
  43. używany jest zestaw metod dostępowych.
  44. </para>
  45. <para>
  46. W metodach obsługujących ładowanie wtyczek używane są następujące
  47. typy: 'validate', 'filter' oraz 'decorator'. Wielkość liter w nazwach
  48. typów nie jest istotna.
  49. </para>
  50. <para>
  51. Metody używane do obsługi ładowania wtyczek to:
  52. </para>
  53. <itemizedlist>
  54. <listitem><para>
  55. <code>setPluginLoader($loader, $type)</code>:
  56. <code>$loader</code> jest obiektem klasy ładującej wtyczki, a
  57. <code>$type</code> jest jednym z typów określonych wyżej. Metoda
  58. ustawia obiekt ładujący wtyczki dla danego typu.
  59. </para></listitem>
  60. <listitem><para>
  61. <code>getPluginLoader($type)</code>: zwraca klasę ładującą wtyczkę
  62. powiązaną z typem <code>$type</code>.
  63. </para></listitem>
  64. <listitem><para>
  65. <code>addPrefixPath($prefix, $path, $type = null)</code>: adds
  66. a prefix/path association to the loader specified by
  67. <code>$type</code>. If <code>$type</code> is null, it will
  68. attempt to add the path to all loaders, by appending the prefix
  69. with each of "_Validate", "_Filter", and "_Decorator"; and
  70. appending the path with "Validate/", "Filter/", and
  71. "Decorator/". If you have all your extra form element classes
  72. under a common hierarchy, this is a convenience method for
  73. setting the base prefix for them.
  74. </para></listitem>
  75. <listitem><para>
  76. <code>addPrefixPaths(array $spec)</code>: allows you to add
  77. many paths at once to one or more plugin loaders. It expects
  78. each array item to be an array with the keys 'path', 'prefix',
  79. and 'type'.
  80. </para></listitem>
  81. </itemizedlist>
  82. <para>
  83. Własne weryfikatory, filtry i dekoratory są łatwym sposobem na
  84. użycie pewnej własnej funkcjonalności w wielu formularzach.
  85. </para>
  86. <example id="zend.form.elements.loaders.customLabel">
  87. <title>Własna etykieta</title>
  88. <para>
  89. One common use case for plugins is to provide replacements for
  90. standard classes. For instance, if you want to provide a
  91. different implementation of the 'Label' decorator -- for
  92. instance, to always append a colon -- you could create your own
  93. 'Label' decorator with your own class prefix, and then add it to
  94. your prefix path.
  95. </para>
  96. <para>
  97. Spróbujmy stworzyć własny dekorator dla etykiety. Damy jego klasie
  98. przedrostek "My_Decorator", a sama klasa będzie znajdować się
  99. w pliku "My/Decorator/Label.php".
  100. </para>
  101. <programlisting role="php"><![CDATA[
  102. class My_Decorator_Label extends Zend_Form_Decorator_Abstract
  103. {
  104. protected $_placement = 'PREPEND';
  105. public function render($content)
  106. {
  107. if (null === ($element = $this->getElement())) {
  108. return $content;
  109. }
  110. if (!method_exists($element, 'getLabel')) {
  111. return $content;
  112. }
  113. $label = $element->getLabel() . ':';
  114. if (null === ($view = $element->getView())) {
  115. return $this->renderLabel($content, $label);
  116. }
  117. $label = $view->formLabel($element->getName(), $label);
  118. return $this->renderLabel($content, $label);
  119. }
  120. public function renderLabel($content, $label)
  121. {
  122. $placement = $this->getPlacement();
  123. $separator = $this->getSeparator();
  124. switch ($placement) {
  125. case 'APPEND':
  126. return $content . $separator . $label;
  127. case 'PREPEND':
  128. default:
  129. return $label . $separator . $content;
  130. }
  131. }
  132. }
  133. ]]>
  134. </programlisting>
  135. <para>
  136. Now we can tell the element to use this plugin path when looking
  137. for decorators:
  138. </para>
  139. <programlisting role="php"><![CDATA[
  140. $element->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
  141. ]]>
  142. </programlisting>
  143. <para>
  144. Alternately, we can do that at the form level to ensure all
  145. decorators use this path:
  146. </para>
  147. <programlisting role="php"><![CDATA[
  148. $form->addElementPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
  149. ]]>
  150. </programlisting>
  151. <para>
  152. With this path added, when you add a decorator, the
  153. 'My/Decorator/' path will be searched first to see if the
  154. decorator exists there. As a result, 'My_Decorator_Label' will
  155. now be used when the 'Label' decorator is requested.
  156. </para>
  157. </example>
  158. </sect2>
  159. <sect2 id="zend.form.elements.filters">
  160. <title>Filtry</title>
  161. <para>
  162. It's often useful and/or necessary to perform some normalization on
  163. input prior to validation – for instance, you may want to strip out
  164. all HTML, but run your validations on what remains to ensure the
  165. submission is valid. Or you may want to trim empty space surrounding
  166. input so that a StringLength validator will not return a false
  167. positive. These operations may be performed using
  168. <code>Zend_Filter</code>, and <code>Zend_Form_Element</code> has
  169. support for filter chains, allowing you to specify multiple,
  170. sequential filters to utilize. Filtering happens both during
  171. validation and when you retrieve the element value via
  172. <code>getValue()</code>:
  173. </para>
  174. <programlisting role="php"><![CDATA[
  175. $filtered = $element->getValue();
  176. ]]>
  177. </programlisting>
  178. <para>
  179. Filtry mogą być dodane na dwa sposoby:
  180. </para>
  181. <itemizedlist>
  182. <listitem><para>
  183. przekazanie konkretnego egzemplarza obiektu filtra
  184. </para></listitem>
  185. <listitem><para>
  186. przekazanie nazwy filtra – krótkiej lub pełnej nazwy
  187. </para></listitem>
  188. </itemizedlist>
  189. <para>
  190. Zobaczmy kilka przykładów:
  191. </para>
  192. <programlisting role="php"><![CDATA[
  193. // Konkretny egzemplarz obiektu filtra:
  194. $element->addFilter(new Zend_Filter_Alnum());
  195. // Pełna nazwa filtra:
  196. $element->addFilter('Zend_Filter_Alnum');
  197. // Krótka nazwa filtra:
  198. $element->addFilter('Alnum');
  199. $element->addFilter('alnum');
  200. ]]>
  201. </programlisting>
  202. <para>
  203. Krótkie nazwy są zazwyczaj nazwą klasy filtra pozbawioną
  204. przedrostka. W domyślnym przypadku, będzie to oznaczało
  205. że pomijamy przedrostek 'Zend_Filter_'. Nie jest też konieczne
  206. aby pierwsza litera była wielka.
  207. </para>
  208. <note>
  209. <title>Użycie własnych klas filtrów</title>
  210. <para>
  211. Jeśli posiadasz własny zestaw klas filtrów, możesz przekazać
  212. klasie <code>Zend_Form_Element</code> informacje o tym za pomocą
  213. metody <code>addPrefixPath()</code>. Na przykład jeśli posiadasz
  214. filtry z przedostkiem 'My_Filter' możesz przekazać do klasy
  215. <code>Zend_Form_Element</code> informację w taki sposób:
  216. </para>
  217. <programlisting role="php"><![CDATA[
  218. $element->addPrefixPath('My_Filter', 'My/Filter/', 'filter');
  219. ]]>
  220. </programlisting>
  221. <para>
  222. (Zauważ że trzeci argument oznacza typ wtyczek dla którego
  223. określamy przedrostek)
  224. </para>
  225. </note>
  226. <para>
  227. Jęśli w potrzebujesz niefiltrowaną wartość użyj metody
  228. <code>getUnfilteredValue()</code>:
  229. </para>
  230. <programlisting role="php"><![CDATA[
  231. $unfiltered = $element->getUnfilteredValue();
  232. ]]>
  233. </programlisting>
  234. <para>
  235. Aby uzyskać więcej informacji o filtrach zobacz <link
  236. linkend="zend.filter.introduction">dokumentację klasy Zend_Filter</link>.
  237. </para>
  238. <para>
  239. Metody powiązane z filtrami to:
  240. </para>
  241. <itemizedlist>
  242. <listitem><para>
  243. <code>addFilter($nameOfFilter, array $options = null)</code>
  244. </para></listitem>
  245. <listitem><para>
  246. <code>addFilters(array $filters)</code>
  247. </para></listitem>
  248. <listitem><para>
  249. <code>setFilters(array $filters)</code> (nadpisuje wszystkie filtry)
  250. </para></listitem>
  251. <listitem><para>
  252. <code>getFilter($name)</code> (pobiera obiekt filtra)
  253. </para></listitem>
  254. <listitem><para>
  255. <code>getFilters()</code> (pobiera wszystkie filtry)
  256. </para></listitem>
  257. <listitem><para>
  258. <code>removeFilter($name)</code> (usuwa filtr)
  259. </para></listitem>
  260. <listitem><para>
  261. <code>clearFilters()</code> (usuwa wszystkie filtry)
  262. </para></listitem>
  263. </itemizedlist>
  264. </sect2>
  265. <sect2 id="zend.form.elements.validators">
  266. <title>Weryfikatory</title>
  267. <para>
  268. If you subscribe to the security mantra of "filter input, escape
  269. output," you'll want to validate ("filter input") your form input.
  270. In <code>Zend_Form</code>, each element includes its own validator
  271. chain, consisting of <code>Zend_Validate_*</code> validators.
  272. </para>
  273. <para>
  274. Weryfikatory mogą być dodane na dwa sposoby:
  275. </para>
  276. <itemizedlist>
  277. <listitem><para>
  278. przekazanie konkretnego egzemplarza obiektu weryfikatora
  279. </para></listitem>
  280. <listitem><para>
  281. przekazanie nazwy weryfikatora – krótkiej lub pełnej nazwy
  282. </para></listitem>
  283. </itemizedlist>
  284. <para>
  285. Zobaczmy kilka przykładów:
  286. </para>
  287. <programlisting role="php"><![CDATA[
  288. // Konkretny egzemplarz obiektu weryfikatora:
  289. $element->addValidator(new Zend_Validate_Alnum());
  290. // Pełna nazwa klasy:
  291. $element->addValidator('Zend_Validate_Alnum');
  292. // Krótka nazwa weryfikatora:
  293. $element->addValidator('Alnum');
  294. $element->addValidator('alnum');
  295. ]]>
  296. </programlisting>
  297. <para>
  298. Krótkie nazwy są zazwyczaj nazwą klasy weryfikatora pozbawioną
  299. przedrostka. W domyślnym przypadku, będzie to oznaczało
  300. że pomijamy przedrostek 'Zend_Validate_'. Nie jest też konieczne
  301. aby pierwsza litera była wielka.
  302. </para>
  303. <note>
  304. <title>Użycie własnych klas weryfikatorów</title>
  305. <para>
  306. Jeśli posiadasz własny zestaw klas weryfikatorów, możesz przekazać
  307. klasie <code>Zend_Form_Element</code> informacje o tym za pomocą
  308. metody <code>addPrefixPath()</code>. Na przykład jeśli posiadasz
  309. weryfikatory z przedostkiem 'My_Validator' możesz przekazać do klasy
  310. <code>Zend_Form_Element</code> informację w taki sposób:
  311. </para>
  312. <programlisting role="php"><![CDATA[
  313. $element->addPrefixPath('My_Validator', 'My/Validator/', 'validate');
  314. ]]>
  315. </programlisting>
  316. <para>
  317. (Zauważ że trzeci argument oznacza typ wtyczek dla którego
  318. określamy przedrostek)
  319. </para>
  320. </note>
  321. <para>
  322. If failing a particular validation should prevent later validators
  323. from firing, pass boolean <code>true</code> as the second parameter:
  324. </para>
  325. <programlisting role="php"><![CDATA[
  326. $element->addValidator('alnum', true);
  327. ]]>
  328. </programlisting>
  329. <para>
  330. If you are using a string name to add a validator, and the
  331. validator class accepts arguments to the constructor, you may pass
  332. these to the third parameter of <code>addValidator()</code> as an
  333. array:
  334. </para>
  335. <programlisting role="php"><![CDATA[
  336. $element->addValidator('StringLength', false, array(6, 20));
  337. ]]>
  338. </programlisting>
  339. <para>
  340. Arguments passed in this way should be in the order in which they
  341. are defined in the constructor. The above example will instantiate
  342. the <code>Zend_Validate_StringLenth</code> class with its
  343. <code>$min</code> and <code>$max</code> parameters:
  344. </para>
  345. <programlisting role="php"><![CDATA[
  346. $validator = new Zend_Validate_StringLength(6, 20);
  347. ]]>
  348. </programlisting>
  349. <note>
  350. <title>Określanie własnych komunikatów o błędach</title>
  351. <para>
  352. Some developers may wish to provide custom error messages for a
  353. validator. <code>Zend_Form_Element::addValidator()</code>'s
  354. <code>$options</code> argument allows you to do so by providing
  355. the key 'messages' and setting it to an array of key/value pairs
  356. for setting the message templates. You will need to know the
  357. error codes of the various validation error types for the
  358. particular validator.
  359. </para>
  360. <para>
  361. A better option is to use a <code>Zend_Translate_Adapter</code>
  362. with your form. Error codes are automatically passed to the
  363. adapter by the default Errors decorator; you can then specify
  364. your own error message strings by setting up translations for
  365. the various error codes of your validators.
  366. </para>
  367. </note>
  368. <para>
  369. Możesz także ustawić wiele weryfikatorów na raz, używając metody
  370. <code>addValidators()</code>. Podstawowym sposobem użycia jest
  371. przekazanie tablicy tablic, gdzie każda z tablic posiada od 1 do 3
  372. wartości, zgodnych z wywołaniem metody <code>addValidator()</code>:
  373. </para>
  374. <programlisting role="php"><![CDATA[
  375. $element->addValidators(array(
  376. array('NotEmpty', true),
  377. array('alnum'),
  378. array('stringLength', false, array(6, 20)),
  379. ));
  380. ]]>
  381. </programlisting>
  382. <para>
  383. If you want to be more verbose or explicit, you can use the array
  384. keys 'validator', 'breakChainOnFailure', and 'options':
  385. </para>
  386. <programlisting role="php"><![CDATA[
  387. $element->addValidators(array(
  388. array(
  389. 'validator' => 'NotEmpty',
  390. 'breakChainOnFailure' => true),
  391. array('validator' => 'alnum'),
  392. array(
  393. 'validator' => 'stringLength',
  394. 'options' => array(6, 20)),
  395. ));
  396. ]]>
  397. </programlisting>
  398. <para>
  399. Ten przykład pokazuje w jaki sposób możesz skonfigurować
  400. weryfikatory w pliku konfiguracyjnym:
  401. </para>
  402. <programlisting role="ini"><![CDATA[
  403. element.validators.notempty.validator = "NotEmpty"
  404. element.validators.notempty.breakChainOnFailure = true
  405. element.validators.alnum.validator = "Alnum"
  406. element.validators.strlen.validator = "StringLength"
  407. element.validators.strlen.options.min = 6
  408. element.validators.strlen.options.max = 20
  409. ]]>
  410. </programlisting>
  411. <para>
  412. Notice that every item has a key, whether or not it needs one; this
  413. is a limitation of using configuration files -- but it also helps
  414. make explicit what the arguments are for. Just remember that any
  415. validator options must be specified in order.
  416. </para>
  417. <para>
  418. Aby sprawdzić poprawność elementu przekaż wartość do metody:
  419. <code>isValid()</code>:
  420. </para>
  421. <programlisting role="php"><![CDATA[
  422. if ($element->isValid($value)) {
  423. // prawidłowy
  424. } else {
  425. // nieprawidłowy
  426. }
  427. ]]>
  428. </programlisting>
  429. <note>
  430. <title>Weryfikowane są przefiltrowane wartości</title>
  431. <para>
  432. <code>Zend_Form_Element::isValid()</code> filtruje wartości
  433. za pomocą ustawionych filtrów zanim zostanie przeprowadzona
  434. weryfikacja. Zobacz <link
  435. linkend="zend.form.elements.filters">rozdział Filtry</link>
  436. aby uzyskać więcej informacji.
  437. </para>
  438. </note>
  439. <note>
  440. <title>Weryfikacja w kontekście</title>
  441. <para>
  442. <code>Zend_Form_Element::isValid()</code> supports an
  443. additional argument, <code>$context</code>.
  444. <code>Zend_Form::isValid()</code> passes the entire array of
  445. data being processed to <code>$context</code> when validating a
  446. form, and <code>Zend_Form_Element::isValid()</code>, in turn,
  447. passes it to each validator. This means you can write
  448. validators that are aware of data passed to other form
  449. elements. As an example, consider a standard registration form
  450. that has fields for both password and a password confirmation;
  451. one validation would be that the two fields match. Such a
  452. validator might look like the following:
  453. </para>
  454. <programlisting role="php"><![CDATA[
  455. class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
  456. {
  457. const NOT_MATCH = 'notMatch';
  458. protected $_messageTemplates = array(
  459. self::NOT_MATCH => 'Password confirmation does not match'
  460. );
  461. public function isValid($value, $context = null)
  462. {
  463. $value = (string) $value;
  464. $this->_setValue($value);
  465. if (is_array($context)) {
  466. if (isset($context['password_confirm'])
  467. && ($value == $context['password_confirm']))
  468. {
  469. return true;
  470. }
  471. } elseif (is_string($context) && ($value == $context)) {
  472. return true;
  473. }
  474. $this->_error(self::NOT_MATCH);
  475. return false;
  476. }
  477. }
  478. ]]>
  479. </programlisting>
  480. </note>
  481. <para>
  482. Validators are processed in order. Each validator is processed,
  483. unless a validator created with a true
  484. <code>breakChainOnFailure</code> value fails its validation. Be
  485. sure to specify your validators in a reasonable order.
  486. </para>
  487. <para>
  488. Po nieudanej weryfikacji możesz pobrać kody i komunikaty błędów:
  489. </para>
  490. <programlisting role="php"><![CDATA[
  491. $errors = $element->getErrors();
  492. $messages = $element->getMessages();
  493. ]]>
  494. </programlisting>
  495. <para>
  496. (Uwaga: komunikaty o błędach są zwracane jako asocjacyjna
  497. tablica w postaci par kod / komunikat.)
  498. </para>
  499. <para>
  500. In addition to validators, you can specify that an element is
  501. required, using <code>setRequired(true)</code>. By default, this
  502. flag is false, meaning that your validator chain will be skipped if
  503. no value is passed to <code>isValid()</code>. You can modify this
  504. behavior in a number of ways:
  505. </para>
  506. <itemizedlist>
  507. <listitem>
  508. <para>
  509. By default, when an element is required, a flag,
  510. 'allowEmpty', is also true. This means that if a value
  511. evaluating to empty is passed to <code>isValid()</code>, the
  512. validators will be skipped. You can toggle this flag using
  513. the accessor <code>setAllowEmpty($flag)</code>; when the
  514. flag is false, then if a value is passed, the validators
  515. will still run.
  516. </para>
  517. </listitem>
  518. <listitem>
  519. <para>
  520. By default, if an element is required, but does not contain
  521. a 'NotEmpty' validator, <code>isValid()</code> will add one
  522. to the top of the stack, with the
  523. <code>breakChainOnFailure</code> flag set. This makes the
  524. required flag have semantic meaning: if no value is passed,
  525. we immediately invalidate the submission and notify the
  526. user, and prevent other validators from running on what we
  527. already know is invalid data.
  528. </para>
  529. <para>
  530. If you do not want this behavior, you can turn it off by
  531. passing a false value to
  532. <code>setAutoInsertNotEmptyValidator($flag)</code>; this
  533. will prevent <code>isValid()</code> from placing the
  534. 'NotEmpty' validator in the validator chain.
  535. </para>
  536. </listitem>
  537. </itemizedlist>
  538. <para>
  539. Aby uzyskać więcej informacji o weryfikatorach, zobacz <link
  540. linkend="zend.validate.introduction">dokumentację klasy Zend_Validate</link>.
  541. </para>
  542. <note>
  543. <title>Użycie klasy Zend_Form_Elements jako weryfikatora</title>
  544. <para>
  545. Klasa <code>Zend_Form_Element</code> implementuje interfejs
  546. <code>Zend_Validate_Interface</code>, co oznacza, że element może
  547. być także użyty jako weryfikator, w zastosowaniu nie związanym
  548. z formularzami.
  549. </para>
  550. </note>
  551. <para>
  552. Metody powiązane z weryfikatorami to:
  553. </para>
  554. <itemizedlist>
  555. <listitem><para>
  556. <code>setRequired($flag)</code> and
  557. <code>isRequired()</code> allow you to set and retrieve the
  558. status of the 'required' flag. When set to boolean <code>true</code>, this
  559. flag requires that the element be in the data processed by
  560. <code>Zend_Form</code>.
  561. </para></listitem>
  562. <listitem><para>
  563. <code>setAllowEmpty($flag)</code> and
  564. <code>getAllowEmpty()</code> allow you to modify the
  565. behaviour of optional elements (i.e., elements where the
  566. required flag is false). When the 'allow empty' flag is
  567. true, empty values will not be passed to the validator
  568. chain.
  569. </para></listitem>
  570. <listitem><para>
  571. <code>setAutoInsertNotEmptyValidator($flag)</code> allows
  572. you to specify whether or not a 'NotEmpty' validator will be
  573. prepended to the validator chain when the element is
  574. required. By default, this flag is true.
  575. </para></listitem>
  576. <listitem><para>
  577. <code>addValidator($nameOrValidator, $breakChainOnFailure = false, array $options = null)</code>
  578. </para></listitem>
  579. <listitem><para>
  580. <code>addValidators(array $validators)</code>
  581. </para></listitem>
  582. <listitem><para>
  583. <code>setValidators(array $validators)</code> (nadpisuje wszystkie weryfikatory)
  584. </para></listitem>
  585. <listitem><para>
  586. <code>getValidator($name)</code> (pobiera obiekt weryfikatora)
  587. </para></listitem>
  588. <listitem><para>
  589. <code>getValidators()</code> (pobiera wszystkie obiekty weryfikatorów)
  590. </para></listitem>
  591. <listitem><para>
  592. <code>removeValidator($name)</code> (usuwa obiekt weryfikatora)
  593. </para></listitem>
  594. <listitem><para>
  595. <code>clearValidators()</code> (usuwa wszystkie obiekty weryfikatorów)
  596. </para></listitem>
  597. </itemizedlist>
  598. <sect3 id="zend.form.elements.validators.errors">
  599. <title>Custom Error Messages</title>
  600. <para>
  601. At times, you may want to specify one or more specific error
  602. messages to use instead of the error messages generated by the
  603. validators attached to your element. Additionally, at times you
  604. may want to mark the element invalid yourself. As of 1.6.0, this
  605. functionality is possible via the following methods.
  606. </para>
  607. <itemizedlist>
  608. <listitem><para>
  609. <code>addErrorMessage($message)</code>: add an error message
  610. to display on form validation errors. You may call this more
  611. than once, and new messages are appended to the stack.
  612. </para></listitem>
  613. <listitem><para>
  614. <code>addErrorMessages(array $messages)</code>: add multiple
  615. error messages to display on form validation errors.
  616. </para></listitem>
  617. <listitem><para>
  618. <code>setErrorMessages(array $messages)</code>: add multiple
  619. error messages to display on form validation errors,
  620. overwriting all previously set error messages.
  621. </para></listitem>
  622. <listitem><para>
  623. <code>getErrorMessages()</code>: retrieve the list of
  624. custom error messages that have been defined.
  625. </para></listitem>
  626. <listitem><para>
  627. <code>clearErrorMessages()</code>: remove all custom error
  628. messages that have been defined.
  629. </para></listitem>
  630. <listitem><para>
  631. <code>markAsError()</code>: mark the element as having
  632. failed validation.
  633. </para></listitem>
  634. <listitem><para>
  635. <code>hasErrors()</code>: determine whether the element has
  636. either failed validation or been marked as invalid.
  637. </para></listitem>
  638. <listitem><para>
  639. <code>addError($message)</code>: add a message to the custom
  640. error messages stack and flag the element as invalid.
  641. </para></listitem>
  642. <listitem><para>
  643. <code>addErrors(array $messages)</code>: add several
  644. messages to the custom error messages stack and flag the
  645. element as invalid.
  646. </para></listitem>
  647. <listitem><para>
  648. <code>setErrors(array $messages)</code>: overwrite the
  649. custom error messages stack with the provided messages and
  650. flag the element as invalid.
  651. </para></listitem>
  652. </itemizedlist>
  653. <para>
  654. All errors set in this fashion may be translated. Additionally,
  655. you may insert the placeholder "%value%" to represent the
  656. element value; this current element value will be substituted
  657. when the error messages are retrieved.
  658. </para>
  659. </sect3>
  660. </sect2>
  661. <sect2 id="zend.form.elements.decorators">
  662. <title>Dekoratory</title>
  663. <para>
  664. One particular pain point for many web developers is the creation
  665. of the XHTML forms themselves. For each element, the developer
  666. needs to create markup for the element itself, typically a label,
  667. and, if they're being nice to their users, markup for displaying
  668. validation error messages. The more elements on the page, the less
  669. trivial this task becomes.
  670. </para>
  671. <para>
  672. <code>Zend_Form_Element</code> tries to solve this issue through
  673. the use of "decorators". Decorators are simply classes that have
  674. access to the element and a method for rendering content. For more
  675. information on how decorators work, please see the section on <link
  676. linkend="zend.form.decorators">Zend_Form_Decorator</link>.
  677. </para>
  678. <para>
  679. Domyśle dekoratory używane przez klasę <code>Zend_Form_Element</code> to:
  680. </para>
  681. <itemizedlist>
  682. <listitem><para>
  683. <emphasis>ViewHelper</emphasis>: określą klasę pomocniczą widoku,
  684. która ma być użyta do renderowania określonego elementu. Atrybut
  685. 'helper' może być użyty aby określić która klasa pomocnicza ma być
  686. użyta. Domyślnie klasa <code>Zend_Form_Element</code> określa
  687. domyślną klasę pomocniczą jako 'formText', jednak klasy
  688. rozszerzające określają inne klasy pomocnicze.
  689. </para></listitem>
  690. <listitem><para>
  691. <emphasis>Errors</emphasis>: dołączą komunikaty błędów do elementu
  692. używając klasy <code>Zend_View_Helper_FormErrors</code>. Jeśli błędów
  693. nie ma nic nie zostaje dołączone.
  694. </para></listitem>
  695. <listitem><para>
  696. <emphasis>HtmlTag</emphasis>: otacza element i błędy znacznikiem
  697. HTML &lt;dd&gt;.
  698. </para></listitem>
  699. <listitem><para>
  700. <emphasis>Label</emphasis>: prepends a label to the element
  701. using <code>Zend_View_Helper_FormLabel</code>, and wraps it in a &lt;dt&gt;
  702. tag. If no label is provided, just the definition term tag is
  703. rendered.
  704. </para></listitem>
  705. </itemizedlist>
  706. <note>
  707. <title>Domyślne dekoratory nie muszą być ładowane</title>
  708. <para>
  709. Domyślny zestaw dekoratorów jest ładowany podczas inicjowania
  710. obiektu. Możesz to zablokować określając opcję
  711. 'disableLoadDefaultDecorators' konstruktora:
  712. </para>
  713. <programlisting role="php"><![CDATA[
  714. $element = new Zend_Form_Element('foo',
  715. array('disableLoadDefaultDecorators' =>
  716. true)
  717. );
  718. ]]>
  719. </programlisting>
  720. <para>
  721. Ta opcja może być użyta równolegle wraz z dowolnymi innymi
  722. opcjami jakie przekażesz, zarówno w postaci tablicy opcji jak
  723. i obiektu <code>Zend_Config</code>.
  724. </para>
  725. </note>
  726. <para>
  727. Z tego względu, że kolejność w jakiej rejestrowane są dekoratory ma
  728. znaczenie -- dekoratory są uruchamiane w takiej kolejności w jakiej
  729. zostały zarejestrowane -- musisz się upewnić, że rejestrujesz
  730. je w odpowiedniej kolejności lub użyć opcji pozwalającej na
  731. zarejestrowanie dekoratora w konkretnej pozycji. Poniżej jako
  732. przykład został zamieszczony przykładowy kod, który rejestruje
  733. domyślne dekoratory:
  734. </para>
  735. <programlisting role="php"><![CDATA[
  736. $this->addDecorators(array(
  737. array('ViewHelper'),
  738. array('Errors'),
  739. array('HtmlTag', array('tag' => 'dd')),
  740. array('Label', array('tag' => 'dt')),
  741. ));
  742. ]]>
  743. </programlisting>
  744. <para>
  745. The initial content is created by the 'ViewHelper' decorator, which
  746. creates the form element itself. Next, the 'Errors' decorator
  747. fetches error messages from the element, and, if any are present,
  748. passes them to the 'FormErrors' view helper to render. The next
  749. decorator, 'HtmlTag', wraps the element and errors in an HTML
  750. &lt;dd&gt; tag. Finally, the last decorator, 'label', retrieves
  751. the element's label and passes it to the 'FormLabel' view helper,
  752. wrapping it in an HTML &lt;dt&gt; tag; the value is prepended to
  753. the content by default. The resulting output looks basically like
  754. this:
  755. </para>
  756. <programlisting role="html"><![CDATA[
  757. <dt><label for="foo" class="optional">Foo</label></dt>
  758. <dd>
  759. <input type="text" name="foo" id="foo" value="123" />
  760. <ul class="errors">
  761. <li>"123" is not an alphanumeric value</li>
  762. </ul>
  763. </dd>
  764. ]]>
  765. </programlisting>
  766. <para>
  767. Aby uzyskać więcej informacji o dekoratorach, zobacz <link
  768. linkend="zend.form.decorators">dokumentację klasy Zend_Form_Decorator</link>.
  769. </para>
  770. <note>
  771. <title>Użycie wielu dekoratorów tego samego typu</title>
  772. <para>
  773. Internally, <code>Zend_Form_Element</code> uses a decorator's
  774. class as the lookup mechanism when retrieving decorators. As a
  775. result, you cannot register multiple decorators of the same
  776. type; subsequent decorators will simply overwrite those that
  777. existed before.
  778. </para>
  779. <para>
  780. To get around this, you can use <emphasis>aliases</emphasis>.
  781. Instead of passing a decorator or decorator name as the first
  782. argument to <code>addDecorator()</code>, pass an array with a
  783. single element, with the alias pointing to the decorator object
  784. or name:
  785. </para>
  786. <programlisting role="php"><![CDATA[
  787. // Alias dla 'FooBar':
  788. $element->addDecorator(array('FooBar' => 'HtmlTag'),
  789. array('tag' => 'div'));
  790. // Pobieramy dekorator:
  791. $decorator = $element->getDecorator('FooBar');
  792. ]]>
  793. </programlisting>
  794. <para>
  795. Do metod <code>addDecorators()</code> oraz
  796. <code>setDecorators()</code> musisz przekazać opcję
  797. 'decorator' znajdującą się w tablicy reprezentującej dekorator.
  798. </para>
  799. <programlisting role="php"><![CDATA[
  800. // Dodanie dwóch dekoratorów 'HtmlTag', ustawiając nazwę jednego z nich na 'FooBar':
  801. $element->addDecorators(
  802. array('HtmlTag', array('tag' => 'div')),
  803. array(
  804. 'decorator' => array('FooBar' => 'HtmlTag'),
  805. 'options' => array('tag' => 'dd')
  806. ),
  807. );
  808. // I pobranie ich póżniej:
  809. $htmlTag = $element->getDecorator('HtmlTag');
  810. $fooBar = $element->getDecorator('FooBar');
  811. ]]>
  812. </programlisting>
  813. </note>
  814. <para>
  815. Metody powiązane z dekoratorami to:
  816. </para>
  817. <itemizedlist>
  818. <listitem><para>
  819. <code>addDecorator($nameOrDecorator, array $options = null)</code>
  820. </para></listitem>
  821. <listitem><para>
  822. <code>addDecorators(array $decorators)</code>
  823. </para></listitem>
  824. <listitem><para>
  825. <code>setDecorators(array $decorators)</code> (nadpisuje wszystkie dekoratory)
  826. </para></listitem>
  827. <listitem><para>
  828. <code>getDecorator($name)</code> (pobiera obiekt dekoratora)
  829. </para></listitem>
  830. <listitem><para>
  831. <code>getDecorators()</code> (pobiera wszystkie dekoratory)
  832. </para></listitem>
  833. <listitem><para>
  834. <code>removeDecorator($name)</code> (usuwa dekorator)
  835. </para></listitem>
  836. <listitem><para>
  837. <code>clearDecorators()</code> (usuwa wszystkie dekoratory)
  838. </para></listitem>
  839. </itemizedlist>
  840. </sect2>
  841. <sect2 id="zend.form.elements.metadata">
  842. <title>Dane meta i atrybuty</title>
  843. <para>
  844. <code>Zend_Form_Element</code> obsługuje wiele atrybutów i danych
  845. meta dla elementów. Te atrybuty to:
  846. </para>
  847. <itemizedlist>
  848. <listitem><para>
  849. <emphasis>name</emphasis>: nazwa elementu. Używa metod dostępowych
  850. <code>setName()</code> oraz <code>getName()</code>.
  851. </para></listitem>
  852. <listitem><para>
  853. <emphasis>label</emphasis>: etykieta elementu. Używa metod dostępowych
  854. <code>setLabel()</code> oraz <code>getLabel()</code>.
  855. </para></listitem>
  856. <listitem><para>
  857. <emphasis>order</emphasis>: pozycja w której element ma być wstawiony
  858. w formularzu. Używa metod dostępowych <code>setOrder()</code> oraz
  859. <code>getOrder()</code>.
  860. </para></listitem>
  861. <listitem><para>
  862. <emphasis>value</emphasis>: obecna wartość elementu. Używa metod
  863. dostępowych <code>setValue()</code> oraz <code>getValue()</code>.
  864. </para></listitem>
  865. <listitem><para>
  866. <emphasis>description</emphasis>: opis elementu; zazwyczaj używane
  867. do utworzenia
  868. often used to provide tooltip or javascript contextual hinting
  869. describing the purpose of the element. Używa metod dostępowych
  870. <code>setDescription()</code> oraz <code>getDescription()</code>.
  871. </para></listitem>
  872. <listitem><para>
  873. <emphasis>required</emphasis>: flag indicating whether or not
  874. the element is required when performing form validation. Uses
  875. the <code>setRequired()</code> and <code>isRequired()</code>
  876. accessors. This flag is false by default.
  877. </para></listitem>
  878. <listitem><para>
  879. <emphasis>allowEmpty</emphasis>: flag indicating whether or not
  880. a non-required (optional) element should attempt to validate
  881. empty values. When true, and the required flag is false, empty
  882. values are not passed to the validator chain, and presumed true.
  883. Uses the <code>setAllowEmpty()</code> and <code>getAllowEmpty()</code>
  884. accessors. This flag is true by default.
  885. </para></listitem>
  886. <listitem><para>
  887. <emphasis>autoInsertNotEmptyValidator</emphasis>: flag
  888. indicating whether or not to insert a 'NotEmpty' validator when
  889. the element is required. By default, this flag is true. Set the
  890. flag with <code>setAutoInsertNotEmptyValidator($flag)</code> and
  891. determine the value with
  892. <code>autoInsertNotEmptyValidator()</code>.
  893. </para></listitem>
  894. </itemizedlist>
  895. <para>
  896. Elementy formularzy mogą wymagać dodatkowych danych meta. Przykładowo
  897. dla elementów formularzy XHTML możesz chcieć określić takie atrybuty
  898. jak 'class' czy 'id'. Do obsługi tego istnieje kilka metod dostępowych:
  899. </para>
  900. <itemizedlist>
  901. <listitem><para>
  902. <emphasis>setAttrib($name, $value)</emphasis>: dodaje atrybut
  903. </para></listitem>
  904. <listitem><para>
  905. <emphasis>setAttribs(array $attribs)</emphasis>: tak jak metoda
  906. addAttribs(), ale nadpisuje atrybuty
  907. </para></listitem>
  908. <listitem><para>
  909. <emphasis>getAttrib($name)</emphasis>: pobiera wartość jednego atrybutu
  910. </para></listitem>
  911. <listitem><para>
  912. <emphasis>getAttribs()</emphasis>: pobiera wszystkie atrybuty w
  913. postaci par klucz/wartość
  914. </para></listitem>
  915. </itemizedlist>
  916. <para>
  917. Most of the time, however, you can simply access them as object
  918. properties, as <code>Zend_Form_Element</code> utilizes overloading
  919. to facilitate access to them:
  920. </para>
  921. <programlisting role="php"><![CDATA[
  922. // Odpowiednik metody $element->setAttrib('class', 'text'):
  923. $element->class = 'text;
  924. ]]>
  925. </programlisting>
  926. <para>
  927. By default, all attributes are passed to the view helper used by
  928. the element during rendering, and rendered as HTML attributes of
  929. the element tag.
  930. </para>
  931. </sect2>
  932. <sect2 id="zend.form.elements.standard">
  933. <title>Standardowe elementy</title>
  934. <para>
  935. Komponent <code>Zend_Form</code> posiada duży zestaw standardowych
  936. elementów; przeczytaj rozdział
  937. <link linkend="zend.form.standardElements">Standardowe Elementy</link>
  938. aby poznać więcej szczegółów.
  939. </para>
  940. </sect2>
  941. <sect2 id="zend.form.elements.methods">
  942. <title>Metody klasy Zend_Form_Element</title>
  943. <para>
  944. Klasa <code>Zend_Form_Element</code> posiada bardzo dużo metod. Poniżej
  945. zamieszczono podsumowanie ich sygnatur, pogrupowanych na podstawie typu:
  946. </para>
  947. <itemizedlist>
  948. <listitem><para>Konfiguracja:</para>
  949. <itemizedlist>
  950. <listitem><para><code>setOptions(array $options)</code></para></listitem>
  951. <listitem><para><code>setConfig(Zend_Config $config)</code></para></listitem>
  952. </itemizedlist>
  953. </listitem>
  954. <listitem><para>I18N:</para>
  955. <itemizedlist>
  956. <listitem><para><code>setTranslator(Zend_Translate_Adapter $translator = null)</code></para></listitem>
  957. <listitem><para><code>getTranslator()</code></para></listitem>
  958. <listitem><para><code>setDisableTranslator($flag)</code></para></listitem>
  959. <listitem><para><code>translatorIsDisabled()</code></para></listitem>
  960. </itemizedlist>
  961. </listitem>
  962. <listitem><para>Właściwości:</para>
  963. <itemizedlist>
  964. <listitem><para><code>setName($name)</code></para></listitem>
  965. <listitem><para><code>getName()</code></para></listitem>
  966. <listitem><para><code>setValue($value)</code></para></listitem>
  967. <listitem><para><code>getValue()</code></para></listitem>
  968. <listitem><para><code>getUnfilteredValue()</code></para></listitem>
  969. <listitem><para><code>setLabel($label)</code></para></listitem>
  970. <listitem><para><code>getLabel()</code></para></listitem>
  971. <listitem><para><code>setDescription($description)</code></para></listitem>
  972. <listitem><para><code>getDescription()</code></para></listitem>
  973. <listitem><para><code>setOrder($order)</code></para></listitem>
  974. <listitem><para><code>getOrder()</code></para></listitem>
  975. <listitem><para><code>setRequired($flag)</code></para></listitem>
  976. <listitem><para><code>isRequired()</code></para></listitem>
  977. <listitem><para><code>setAllowEmpty($flag)</code></para></listitem>
  978. <listitem><para><code>getAllowEmpty()</code></para></listitem>
  979. <listitem><para><code>setAutoInsertNotEmptyValidator($flag)</code></para></listitem>
  980. <listitem><para><code>autoInsertNotEmptyValidator()</code></para></listitem>
  981. <listitem><para><code>setIgnore($flag)</code></para></listitem>
  982. <listitem><para><code>getIgnore()</code></para></listitem>
  983. <listitem><para><code>getType()</code></para></listitem>
  984. <listitem><para><code>setAttrib($name, $value)</code></para></listitem>
  985. <listitem><para><code>setAttribs(array $attribs)</code></para></listitem>
  986. <listitem><para><code>getAttrib($name)</code></para></listitem>
  987. <listitem><para><code>getAttribs()</code></para></listitem>
  988. </itemizedlist>
  989. </listitem>
  990. <listitem><para>Ładowanie wtyczek i ścieżki:</para>
  991. <itemizedlist>
  992. <listitem><para><code>setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type)</code></para></listitem>
  993. <listitem><para><code>getPluginLoader($type)</code></para></listitem>
  994. <listitem><para><code>addPrefixPath($prefix, $path, $type = null)</code></para></listitem>
  995. <listitem><para><code>addPrefixPaths(array $spec)</code></para></listitem>
  996. </itemizedlist>
  997. </listitem>
  998. <listitem><para>Weryfikacja:</para>
  999. <itemizedlist>
  1000. <listitem><para><code>addValidator($validator, $breakChainOnFailure = false, $options = array())</code></para></listitem>
  1001. <listitem><para><code>addValidators(array $validators)</code></para></listitem>
  1002. <listitem><para><code>setValidators(array $validators)</code></para></listitem>
  1003. <listitem><para><code>getValidator($name)</code></para></listitem>
  1004. <listitem><para><code>getValidators()</code></para></listitem>
  1005. <listitem><para><code>removeValidator($name)</code></para></listitem>
  1006. <listitem><para><code>clearValidators()</code></para></listitem>
  1007. <listitem><para><code>isValid($value, $context = null)</code></para></listitem>
  1008. <listitem><para><code>getErrors()</code></para></listitem>
  1009. <listitem><para><code>getMessages()</code></para></listitem>
  1010. </itemizedlist>
  1011. </listitem>
  1012. <listitem><para>Filtrowanie:</para>
  1013. <itemizedlist>
  1014. <listitem><para><code>addFilter($filter, $options = array())</code></para></listitem>
  1015. <listitem><para><code>addFilters(array $filters)</code></para></listitem>
  1016. <listitem><para><code>setFilters(array $filters)</code></para></listitem>
  1017. <listitem><para><code>getFilter($name)</code></para></listitem>
  1018. <listitem><para><code>getFilters()</code></para></listitem>
  1019. <listitem><para><code>removeFilter($name)</code></para></listitem>
  1020. <listitem><para><code>clearFilters()</code></para></listitem>
  1021. </itemizedlist>
  1022. </listitem>
  1023. <listitem><para>Renderowanie:</para>
  1024. <itemizedlist>
  1025. <listitem><para><code>setView(Zend_View_Interface $view = null)</code></para></listitem>
  1026. <listitem><para><code>getView()</code></para></listitem>
  1027. <listitem><para><code>addDecorator($decorator, $options = null)</code></para></listitem>
  1028. <listitem><para><code>addDecorators(array $decorators)</code></para></listitem>
  1029. <listitem><para><code>setDecorators(array $decorators)</code></para></listitem>
  1030. <listitem><para><code>getDecorator($name)</code></para></listitem>
  1031. <listitem><para><code>getDecorators()</code></para></listitem>
  1032. <listitem><para><code>removeDecorator($name)</code></para></listitem>
  1033. <listitem><para><code>clearDecorators()</code></para></listitem>
  1034. <listitem><para><code>render(Zend_View_Interface $view = null)</code></para></listitem>
  1035. </itemizedlist>
  1036. </listitem>
  1037. </itemizedlist>
  1038. </sect2>
  1039. <sect2 id="zend.form.elements.config">
  1040. <title>Konfiguracja</title>
  1041. <para>
  1042. Konstruktor klasy <code>Zend_Form_Element</code>przyjmuje w
  1043. parametrze tablicę opcji lub obiekt <code>Zend_Config</code>
  1044. zawierający pcje. Klasa może być także skonfigurowana za pomocą
  1045. metod <code>setOptions()</code> oraz <code>setConfig()</code>.
  1046. Generalnie klucze nazwane są w taki sposób:
  1047. </para>
  1048. <itemizedlist>
  1049. <listitem><para>
  1050. If 'set' + key refers to a <code>Zend_Form_Element</code>
  1051. method, then the value provided will be passed to that method.
  1052. </para></listitem>
  1053. <listitem><para>
  1054. Otherwise, the value will be used to set an attribute.
  1055. </para></listitem>
  1056. </itemizedlist>
  1057. <para>
  1058. Oto wyjątki od tej zasady:
  1059. </para>
  1060. <itemizedlist>
  1061. <listitem><para>
  1062. <code>prefixPath</code> will be passed to
  1063. <code>addPrefixPaths()</code>
  1064. </para></listitem>
  1065. <listitem>
  1066. <para>
  1067. The following setters cannot be set in this way:
  1068. </para>
  1069. <itemizedlist>
  1070. <listitem><para>
  1071. <code>setAttrib</code> (though
  1072. <code>setAttribs</code> <emphasis>will</emphasis>
  1073. work)
  1074. </para></listitem>
  1075. <listitem><para><code>setConfig</code></para></listitem>
  1076. <listitem><para><code>setOptions</code></para></listitem>
  1077. <listitem><para><code>setPluginLoader</code></para></listitem>
  1078. <listitem><para><code>setTranslator</code></para></listitem>
  1079. <listitem><para><code>setView</code></para></listitem>
  1080. </itemizedlist>
  1081. </listitem>
  1082. </itemizedlist>
  1083. <para>
  1084. As an example, here is a config file that passes configuration for
  1085. every type of configurable data:
  1086. </para>
  1087. <programlisting role="ini"><![CDATA[
  1088. [element]
  1089. name = "foo"
  1090. value = "foobar"
  1091. label = "Foo:"
  1092. order = 10
  1093. required = true
  1094. allowEmpty = false
  1095. autoInsertNotEmptyValidator = true
  1096. description = "Foo elements are for examples"
  1097. ignore = false
  1098. attribs.id = "foo"
  1099. attribs.class = "element"
  1100. ; ustawia atrybut 'onclick'
  1101. onclick = "autoComplete(this, '/form/autocomplete/element')"
  1102. prefixPaths.decorator.prefix = "My_Decorator"
  1103. prefixPaths.decorator.path = "My/Decorator/"
  1104. disableTranslator = 0
  1105. validators.required.validator = "NotEmpty"
  1106. validators.required.breakChainOnFailure = true
  1107. validators.alpha.validator = "alpha"
  1108. validators.regex.validator = "regex"
  1109. validators.regex.options.pattern = "/^[A-F].*/$"
  1110. filters.ucase.filter = "StringToUpper"
  1111. decorators.element.decorator = "ViewHelper"
  1112. decorators.element.options.helper = "FormText"
  1113. decorators.label.decorator = "Label"
  1114. ]]>
  1115. </programlisting>
  1116. </sect2>
  1117. <sect2 id="zend.form.elements.custom">
  1118. <title>Własne elementy</title>
  1119. <para>
  1120. Możesz tworzyć własne elementy po prostu rozszerzając klasę
  1121. <code>Zend_Form_Element</code>. Powodami aby to zrobić mogą być:
  1122. </para>
  1123. <itemizedlist>
  1124. <listitem><para>
  1125. Elements that share common validators and/or filters
  1126. </para></listitem>
  1127. <listitem><para>
  1128. Elements that have custom decorator functionality
  1129. </para></listitem>
  1130. </itemizedlist>
  1131. <para>
  1132. There are two methods typically used to extend an element:
  1133. <code>init()</code>, which can be used to add custom initialization
  1134. logic to your element, and <code>loadDefaultDecorators()</code>,
  1135. which can be used to set a list of default decorators used by your
  1136. element.
  1137. </para>
  1138. <para>
  1139. As an example, let's say that all text elements in a form you are
  1140. creating need to be filtered with <code>StringTrim</code>,
  1141. validated with a common regular expression, and that you want to
  1142. use a custom decorator you've created for displaying them,
  1143. 'My_Decorator_TextItem'; additionally, you have a number of standard
  1144. attributes, including 'size', 'maxLength', and 'class' you wish to
  1145. specify. You could define such an element as follows:
  1146. </para>
  1147. <programlisting role="php"><![CDATA[
  1148. class My_Element_Text extends Zend_Form_Element
  1149. {
  1150. public function init()
  1151. {
  1152. $this->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator')
  1153. ->addFilters('StringTrim')
  1154. ->addValidator('Regex', false, array('/^[a-z0-9]{6,}$/i'))
  1155. ->addDecorator('TextItem')
  1156. ->setAttrib('size', 30)
  1157. ->setAttrib('maxLength', 45)
  1158. ->setAttrib('class', 'text');
  1159. }
  1160. }
  1161. ]]>
  1162. </programlisting>
  1163. <para>
  1164. You could then inform your form object about the prefix path for
  1165. such elements, and start creating elements:
  1166. </para>
  1167. <programlisting role="php"><![CDATA[<?php
  1168. $form->addPrefixPath('My_Element', 'My/Element/', 'element')
  1169. ->addElement('foo', 'text');
  1170. ]]>
  1171. </programlisting>
  1172. <para>
  1173. The 'foo' element will now be of type <code>My_Element_Text</code>,
  1174. and exhibit the behaviour you've outlined.
  1175. </para>
  1176. <para>
  1177. Another method you may want to override when extending
  1178. <code>Zend_Form_Element</code> is the
  1179. <code>loadDefaultDecorators()</code> method. This method
  1180. conditionally loads a set of default decorators for your element;
  1181. you may wish to substitute your own decorators in your extending
  1182. class:
  1183. </para>
  1184. <programlisting role="php"><![CDATA[
  1185. class My_Element_Text extends Zend_Form_Element
  1186. {
  1187. public function loadDefaultDecorators()
  1188. {
  1189. $this->addDecorator('ViewHelper')
  1190. ->addDecorator('DisplayError')
  1191. ->addDecorator('Label')
  1192. ->addDecorator('HtmlTag',
  1193. array('tag' => 'div', 'class' => 'element'));
  1194. }
  1195. }
  1196. ]]>
  1197. </programlisting>
  1198. <para>
  1199. There are many ways to customize elements; be sure to read the API
  1200. documentation of <code>Zend_Form_Element</code> to know all the
  1201. methods available.
  1202. </para>
  1203. </sect2>
  1204. </sect1>
  1205. <!--
  1206. vim:se ts=4 sw=4 tw=80 et:
  1207. -->