1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.form.forms">
<title>Creating Forms Using Zend_Form</title>
<para>
The <classname>Zend_Form</classname> class is used to aggregate form elements,
display groups, and subforms. It can then perform the following actions
on those items:
</para>
<itemizedlist>
<listitem>
<para>
Validation, including retrieving error codes and messages
</para>
</listitem>
<listitem>
<para>
Value aggregation, including populating items and retrieving both
filtered and unfiltered values from all items
</para>
</listitem>
<listitem>
<para>
Iteration over all items, in the order in which they are entered or
based on the order hints retrieved from each item
</para>
</listitem>
<listitem>
<para>
Rendering of the entire form, either via a single decorator that
performs custom rendering or by iterating over each item in the form
</para>
</listitem>
</itemizedlist>
<para>
While forms created with <classname>Zend_Form</classname> may be complex, probably
the best use case is for simple forms; its best use is for Rapid
Application Development (RAD) and prototyping.
</para>
<para>
At its most basic, you simply instantiate a form object:
</para>
<programlisting language="php"><![CDATA[
// Generic form object:
$form = new Zend_Form();
// Custom form object:
$form = new My_Form()
]]></programlisting>
<para>
You can optionally pass in a instance of <classname>Zend_Config</classname> or an array,
which will be used to set object state and potentially create new elements:
</para>
<programlisting language="php"><![CDATA[
// Passing in configuration options:
$form = new Zend_Form($config);
]]></programlisting>
<para>
<classname>Zend_Form</classname> is iterable, and will iterate over elements,
display groups, and subforms, using the order they were registered and
any order index each may have. This is useful in cases where you wish to
render the elements manually in the appropriate order.
</para>
<para>
<classname>Zend_Form</classname>'s magic lies in its ability to serve as a factory
for elements and display groups, as well as the ability to render itself
through decorators.
</para>
<sect2 id="zend.form.forms.plugins">
<title>Plugin Loaders</title>
<para>
<classname>Zend_Form</classname> makes use of
<classname>Zend_Loader_PluginLoader</classname> to allow developers to specify
the locations of alternate elements and decorators. Each has its own
plugin loader associated with it, and general accessors are used to
retrieve and modify each.
</para>
<para>
The following loader types are used with the various plugin loader
methods: 'element' and 'decorator'. The type names are case
insensitive.
</para>
<para>
The methods used to interact with plugin loaders are as follows:
</para>
<itemizedlist>
<listitem>
<para>
<methodname>setPluginLoader($loader, $type)</methodname>: $loader is the
plugin loader object itself, while type is one of the types
specified above. This sets the plugin loader for the given type
to the newly specified loader object.
</para>
</listitem>
<listitem>
<para>
<methodname>getPluginLoader($type)</methodname>: retrieves the plugin loader
associated with $type.
</para>
</listitem>
<listitem>
<para>
<methodname>addPrefixPath($prefix, $path, $type = null)</methodname>: adds a
prefix/path association to the loader specified by $type. If
$type is <constant>NULL</constant>, it will attempt to add the path to all
loaders, by appending the prefix with each of "_Element" and
"_Decorator"; and appending the path with "Element/" and
"Decorator/". If you have all your extra form element classes
under a common hierarchy, this is a convenience method for
setting the base prefix for them.
</para>
</listitem>
<listitem>
<para>
<methodname>addPrefixPaths(array $spec)</methodname>: allows you to add many
paths at once to one or more plugin loaders. It expects each
array item to be an array with the keys 'path', 'prefix', and
'type'.
</para>
</listitem>
</itemizedlist>
<para>
Additionally, you can specify prefix paths for all elements and
display groups created through a <classname>Zend_Form</classname> instance
using the following methods:
</para>
<itemizedlist>
<listitem>
<para>
<methodname>addElementPrefixPath($prefix, $path, $type = null)</methodname>:
Just like <methodname>addPrefixPath()</methodname>, you must specify a class
prefix and a path. <varname>$type</varname>, when specified, must be
one of the plugin loader types specified by
<classname>Zend_Form_Element</classname>; see the <link
linkend="zend.form.elements.loaders">element plugins
section</link> for more information on valid
<varname>$type</varname> values. If no <varname>$type</varname> is
specified, the method will assume it is a general prefix for all
types.
</para>
</listitem>
<listitem>
<para>
<methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname>:
Just like <methodname>addPrefixPath()</methodname>, you must specify a class
prefix and a path; however, since display groups only support
decorators as plugins, no <varname>$type</varname> is necessary.
</para>
</listitem>
</itemizedlist>
<para>
Custom elements and decorators are an easy way to share
functionality between forms and encapsulate custom functionality.
See the <link
linkend="zend.form.elements.loaders.customLabel">Custom Label
example</link> in the elements documentation for an example of
how custom elements can be used as replacements for standard
classes.
</para>
</sect2>
<sect2 id="zend.form.forms.elements">
<title>Elements</title>
<para>
<classname>Zend_Form</classname> provides several accessors for adding and
removing form elements from a form. These can take element object
instances or serve as factories for instantiating the element
objects themselves.
</para>
<para>
The most basic method for adding an element is
<methodname>addElement()</methodname>. This method can take either an object of
type <classname>Zend_Form_Element</classname> (or of a class extending
<classname>Zend_Form_Element</classname>), or arguments for building a new
element -- including the element type, name, and any configuration
options.
</para>
<para>
Some examples:
</para>
<programlisting language="php"><![CDATA[
// Using an element instance:
$element = new Zend_Form_Element_Text('foo');
$form->addElement($element);
// Using a factory
//
// Creates an element of type Zend_Form_Element_Text with the
// name of 'foo':
$form->addElement('text', 'foo');
// Pass label option to the element:
$form->addElement('text', 'foo', array('label' => 'Foo:'));
]]></programlisting>
<note>
<title>addElement() Implements Fluent Interface</title>
<para>
<methodname>addElement()</methodname> implements a fluent interface; that is
to say, it returns the <classname>Zend_Form</classname> object, and not
the element. This is done to allow you to chain together
multiple addElement() methods or other form methods that
implement the fluent interface (all setters in <classname>Zend_Form</classname>
implement the pattern).
</para>
<para>
If you wish to return the element instead, use
<methodname>createElement()</methodname>, which is outlined below. Be aware,
however, that <methodname>createElement()</methodname> does not attach the
element to the form.
</para>
<para>
Internally, <methodname>addElement()</methodname> actually uses
<methodname>createElement()</methodname> to create the element before
attaching it to the form.
</para>
</note>
<para>
Once an element has been added to the form, you can retrieve it by
name. This can be done either by using the <methodname>getElement()</methodname>
method or by using overloading to access the element as an object
property:
</para>
<programlisting language="php"><![CDATA[
// getElement():
$foo = $form->getElement('foo');
// As object property:
$foo = $form->foo;
]]></programlisting>
<para>
Occasionally, you may want to create an element without attaching it
to the form (for instance, if you wish to make use of the various
plugin paths registered with the form, but wish to later attach the
object to a sub form). The <methodname>createElement()</methodname> method
allows you to do so:
</para>
<programlisting language="php"><![CDATA[
// $username becomes a Zend_Form_Element_Text object:
$username = $form->createElement('text', 'username');
]]></programlisting>
<sect3 id="zend.form.forms.elements.values">
<title>Populating and Retrieving Values</title>
<para>
After validating a form, you will typically need to retrieve the
values so you can perform other operations, such as updating a
database or notifying a web service. You can retrieve all values
for all elements using <methodname>getValues()</methodname>;
<methodname>getValue($name)</methodname> allows you to retrieve a single
element's value by element name:
</para>
<programlisting language="php"><![CDATA[
// Get all values:
$values = $form->getValues();
// Get only 'foo' element's value:
$value = $form->getValue('foo');
]]></programlisting>
<para>
Sometimes you'll want to populate the form with specified values
prior to rendering. This can be done with either the
<methodname>setDefaults()</methodname> or <methodname>populate()</methodname>
methods:
</para>
<programlisting language="php"><![CDATA[
$form->setDefaults($data);
$form->populate($data);
]]></programlisting>
<para>
On the flip side, you may want to clear a form after populating
or validating it; this can be done using the
<methodname>reset()</methodname> method:
</para>
<programlisting language="php"><![CDATA[
$form->reset();
]]></programlisting>
</sect3>
<sect3 id="zend.form.forms.elements.global">
<title>Global Operations</title>
<para>
Occasionally you will want certain operations to affect all
elements. Common scenarios include needing to set plugin prefix
paths for all elements, setting decorators for all elements, and
setting filters for all elements. As examples:
</para>
<example id="zend.form.forms.elements.global.allpaths">
<title>Setting Prefix Paths for All Elements</title>
<para>
You can set prefix paths for all elements by type, or using
a global prefix. Some examples:
</para>
<programlisting language="php"><![CDATA[
// Set global prefix path:
// Creates paths for prefixes My_Foo_Filter, My_Foo_Validate,
// and My_Foo_Decorator
$form->addElementPrefixPath('My_Foo', 'My/Foo/');
// Just filter paths:
$form->addElementPrefixPath('My_Foo_Filter',
'My/Foo/Filter',
'filter');
// Just validator paths:
$form->addElementPrefixPath('My_Foo_Validate',
'My/Foo/Validate',
'validate');
// Just decorator paths:
$form->addElementPrefixPath('My_Foo_Decorator',
'My/Foo/Decorator',
'decorator');
]]></programlisting>
</example>
<example id="zend.form.forms.elements.global.decorators">
<title>Setting Decorators for All Elements</title>
<para>
You can set decorators for all elements.
<methodname>setElementDecorators()</methodname> accepts an array of
decorators, just like <methodname>setDecorators()</methodname>, and will
overwrite any previously set decorators in each element. In
this example, we set the decorators to simply a ViewHelper
and a Label:
</para>
<programlisting language="php"><![CDATA[
$form->setElementDecorators(array(
'ViewHelper',
'Label'
));
]]></programlisting>
</example>
<example id="zend.form.forms.elements.global.decoratorsFilter">
<title>Setting Decorators for Some Elements</title>
<para>
You can also set decorators for a subset of elements,
either by inclusion or exclusion. The second argument to
<methodname>setElementDecorators()</methodname> may be an array of
element names; by default, specifying such an array will
set the specified decorators on those elements only. You
may also pass a third argument, a flag indicating whether
this list of elements is for inclusion or exclusion
purposes. If the flag is <constant>FALSE</constant>, it will decorate all
elements <emphasis>except</emphasis> those in the passed list.
As with standard usage of the method, any decorators passed
will overwrite any previously set decorators in each
element.
</para>
<para>
In the following snippet, we indicate that we want only the
ViewHelper and Label decorators for the 'foo' and 'bar'
elements:
</para>
<programlisting language="php"><![CDATA[
$form->setElementDecorators(
array(
'ViewHelper',
'Label'
),
array(
'foo',
'bar'
)
);
]]></programlisting>
<para>
On the flip side, with this snippet, we'll now indicate
that we want to use only the ViewHelper and Label
decorators for every element <emphasis>except</emphasis>
the 'foo' and 'bar' elements:
</para>
<programlisting language="php"><![CDATA[
$form->setElementDecorators(
array(
'ViewHelper',
'Label'
),
array(
'foo',
'bar'
),
false
);
]]></programlisting>
</example>
<note>
<title>Some Decorators are Inappropriate for Some Elements</title>
<para>
While <methodname>setElementDecorators()</methodname> may seem like
a good solution, there are some cases where it may
actually end up with unexpected results. For example,
the various button elements (Submit, Button, Reset)
currently use the label as the value of the button,
and only use ViewHelper and DtDdWrapper decorators --
preventing an additional labels, errors, and hints from
being rendered. The example above would duplicate some
content (the label) for button elements.
</para>
<para>
You can use the inclusion/exclusion array to overcome
this issue as noted in the previous example.
</para>
<para>
So, use this method wisely, and realize that you may
need to exclude some elements or manually change some elements' decorators
to prevent unwanted output.
</para>
</note>
<example id="zend.form.forms.elements.global.filters">
<title>Setting Filters for All Elements</title>
<para>
In some cases, you may want to apply the same filter to all
elements; a common case is to <methodname>trim()</methodname> all values:
</para>
<programlisting language="php"><![CDATA[
$form->setElementFilters(array('StringTrim'));
]]></programlisting>
</example>
</sect3>
<sect3 id="zend.form.forms.elements.methods">
<title>Methods For Interacting With Elements</title>
<para>
The following methods may be used to interact with elements:
</para>
<itemizedlist>
<listitem>
<para>
<methodname>createElement($element, $name = null, $options =
null)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addElement($element, $name = null, $options = null)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addElements(array $elements)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setElements(array $elements)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getElement($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getElements()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>removeElement($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>clearElements()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setDefaults(array $defaults)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setDefault($name, $value)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getValue($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getValues()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getUnfilteredValue($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getUnfilteredValues()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setElementFilters(array $filters)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setElementDecorators(array $decorators)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addElementPrefixPath($prefix, $path, $type = null)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addElementPrefixPaths(array $spec)</methodname>
</para>
</listitem>
</itemizedlist>
</sect3>
</sect2>
<sect2 id="zend.form.forms.displaygroups">
<title>Display Groups</title>
<para>
Display groups are a way to create virtual groupings of elements for
display purposes. All elements remain accessible by name in the
form, but when iterating over the form or rendering, any elements in
a display group are rendered together. The most common use case for
this is for grouping elements in fieldsets.
</para>
<para>
The base class for display groups is
<classname>Zend_Form_DisplayGroup</classname>. While it can be instantiated
directly, it is usually best to use <classname>Zend_Form</classname>'s
<methodname>addDisplayGroup()</methodname> method to do so. This method takes an
array of element names as its first argument, and a name for the display
group as its second argument. You may optionally pass in an array of
options or a <classname>Zend_Config</classname> object as the third argument.
</para>
<para>
Assuming that the elements 'username' and 'password' are already set
in the form, the following code would group these elements in a
'login' display group:
</para>
<programlisting language="php"><![CDATA[
$form->addDisplayGroup(array('username', 'password'), 'login');
]]></programlisting>
<para>
You can access display groups using the
<methodname>getDisplayGroup()</methodname> method, or via overloading using the
display group's name:
</para>
<programlisting language="php"><![CDATA[
// Using getDisplayGroup():
$login = $form->getDisplayGroup('login');
// Using overloading:
$login = $form->login;
]]></programlisting>
<note>
<title>Default Decorators Do Not Need to Be Loaded</title>
<para>
By default, the default decorators are loaded during object
initialization. You can disable this by passing the
'disableLoadDefaultDecorators' option when creating a display
group:
</para>
<programlisting language="php"><![CDATA[
$form->addDisplayGroup(
array('foo', 'bar'),
'foobar',
array('disableLoadDefaultDecorators' => true)
);
]]></programlisting>
<para>
This option may be mixed with any other options you pass,
both as array options or in a <classname>Zend_Config</classname> object.
</para>
</note>
<sect3 id="zend.form.forms.displaygroups.global">
<title>Global Operations</title>
<para>
Just as with elements, there are some operations which might
affect all display groups; these include setting decorators and
setting the plugin path in which to look for decorators.
</para>
<example id="zend.form.forms.displaygroups.global.paths">
<title>Setting Decorator Prefix Path for All Display Groups</title>
<para>
By default, display groups inherit whichever decorator paths
the form uses; however, if they should look in alternate
locations, you can use the
<methodname>addDisplayGroupPrefixPath()</methodname> method.
</para>
<programlisting language="php"><![CDATA[
$form->addDisplayGroupPrefixPath('My_Foo_Decorator', 'My/Foo/Decorator');
]]></programlisting>
</example>
<example id="zend.form.forms.displaygroups.global.decorators">
<title>Setting Decorators for All Display Groups</title>
<para>
You can set decorators for all display groups.
<methodname>setDisplayGroupDecorators()</methodname> accepts an array of
decorators, just like <methodname>setDecorators()</methodname>, and will
overwrite any previously set decorators in each display
group. In this example, we set the decorators to simply a
fieldset (the FormElements decorator is necessary to ensure
that the elements are iterated):
</para>
<programlisting language="php"><![CDATA[
$form->setDisplayGroupDecorators(array(
'FormElements',
'Fieldset'
));
]]></programlisting>
</example>
</sect3>
<sect3 id="zend.form.forms.displaygroups.customClasses">
<title>Using Custom Display Group Classes</title>
<para>
By default, <classname>Zend_Form</classname> uses the
<classname>Zend_Form_DisplayGroup</classname> class for display groups.
You may find you need to extend this class in order to provided
custom functionality. <methodname>addDisplayGroup()</methodname> does not
allow passing in a concrete instance, but does allow specifying
the class to use as one of its options, using the
'displayGroupClass' key:
</para>
<programlisting language="php"><![CDATA[
// Use the 'My_DisplayGroup' class
$form->addDisplayGroup(
array('username', 'password'),
'user',
array('displayGroupClass' => 'My_DisplayGroup')
);
]]></programlisting>
<para>
If the class has not yet been loaded, <classname>Zend_Form</classname>
will attempt to do so using <classname>Zend_Loader</classname>.
</para>
<para>
You can also specify a default display group class to use with
the form such that all display groups created with the form
object will use that class:
</para>
<programlisting language="php"><![CDATA[
// Use the 'My_DisplayGroup' class for all display groups:
$form->setDefaultDisplayGroupClass('My_DisplayGroup');
]]></programlisting>
<para>
This setting may be specified in configurations as
'defaultDisplayGroupClass', and will be loaded early to ensure
all display groups use that class.
</para>
</sect3>
<sect3 id="zend.form.forms.displaygroups.interactionmethods">
<title>Methods for Interacting With Display Groups</title>
<para>
The following methods may be used to interact with display
groups:
</para>
<itemizedlist>
<listitem>
<para>
<methodname>addDisplayGroup(array $elements, $name, $options
= null)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addDisplayGroups(array $groups)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setDisplayGroups(array $groups)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getDisplayGroup($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getDisplayGroups()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>removeDisplayGroup($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>clearDisplayGroups()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setDisplayGroupDecorators(array $decorators)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setDefaultDisplayGroupClass($class)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getDefaultDisplayGroupClass($class)</methodname>
</para>
</listitem>
</itemizedlist>
</sect3>
<sect3 id="zend.form.forms.displaygroups.methods">
<title>Zend_Form_DisplayGroup Methods</title>
<para>
<classname>Zend_Form_DisplayGroup</classname> has the following methods,
grouped by type:
</para>
<itemizedlist>
<listitem>
<para>Configuration:</para>
<itemizedlist>
<listitem>
<para>
<methodname>setOptions(array $options)</methodname>
</para>
</listitem>
<listitem>
<para><methodname>setConfig(Zend_Config $config)</methodname></para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Metadata:</para>
<itemizedlist>
<listitem>
<para><methodname>setAttrib($key, $value)</methodname></para>
</listitem>
<listitem>
<para><methodname>addAttribs(array $attribs)</methodname></para>
</listitem>
<listitem>
<para><methodname>setAttribs(array $attribs)</methodname></para>
</listitem>
<listitem><para><methodname>getAttrib($key)</methodname></para></listitem>
<listitem><para><methodname>getAttribs()</methodname></para></listitem>
<listitem>
<para><methodname>removeAttrib($key)</methodname></para>
</listitem>
<listitem><para><methodname>clearAttribs()</methodname></para></listitem>
<listitem><para><methodname>setName($name)</methodname></para></listitem>
<listitem><para><methodname>getName()</methodname></para></listitem>
<listitem>
<para><methodname>setDescription($value)</methodname></para>
</listitem>
<listitem><para><methodname>getDescription()</methodname></para></listitem>
<listitem>
<para><methodname>setLegend($legend)</methodname></para>
</listitem>
<listitem><para><methodname>getLegend()</methodname></para></listitem>
<listitem><para><methodname>setOrder($order)</methodname></para></listitem>
<listitem><para><methodname>getOrder()</methodname></para></listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Elements:</para>
<itemizedlist>
<listitem>
<para>
<methodname>createElement($type, $name, array $options
= array())</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addElement($typeOrElement, $name, array $options =
array())</methodname>
</para>
</listitem>
<listitem>
<para><methodname>addElements(array $elements)</methodname></para>
</listitem>
<listitem>
<para><methodname>setElements(array $elements)</methodname></para>
</listitem>
<listitem><para><methodname>getElement($name)</methodname></para></listitem>
<listitem><para><methodname>getElements()</methodname></para></listitem>
<listitem>
<para><methodname>removeElement($name)</methodname></para>
</listitem>
<listitem><para><methodname>clearElements()</methodname></para></listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Plugin loaders:</para>
<itemizedlist>
<listitem>
<para>
<methodname>setPluginLoader(Zend_Loader_PluginLoader
$loader)</methodname>
</para>
</listitem>
<listitem><para><methodname>getPluginLoader()</methodname></para></listitem>
<listitem>
<para><methodname>addPrefixPath($prefix, $path)</methodname></para>
</listitem>
<listitem>
<para><methodname>addPrefixPaths(array $spec)</methodname></para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Decorators:</para>
<itemizedlist>
<listitem>
<para>
<methodname>addDecorator($decorator, $options = null)</methodname>
</para>
</listitem>
<listitem>
<para><methodname>addDecorators(array $decorators)</methodname></para>
</listitem>
<listitem>
<para><methodname>setDecorators(array $decorators)</methodname></para>
</listitem>
<listitem>
<para><methodname>getDecorator($name)</methodname></para>
</listitem>
<listitem><para><methodname>getDecorators()</methodname></para></listitem>
<listitem>
<para><methodname>removeDecorator($name)</methodname></para>
</listitem>
<listitem><para><methodname>clearDecorators()</methodname></para></listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Rendering:</para>
<itemizedlist>
<listitem>
<para>
<methodname>setView(Zend_View_Interface $view = null)</methodname>
</para>
</listitem>
<listitem><para><methodname>getView()</methodname></para></listitem>
<listitem>
<para>
<methodname>render(Zend_View_Interface $view = null)</methodname>
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>I18n:</para>
<itemizedlist>
<listitem>
<para>
<methodname>setTranslator(Zend_Translate_Adapter $translator =
null)</methodname>
</para>
</listitem>
<listitem><para><methodname>getTranslator()</methodname></para></listitem>
<listitem>
<para><methodname>setDisableTranslator($flag)</methodname></para>
</listitem>
<listitem>
<para><methodname>translatorIsDisabled()</methodname></para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</sect3>
</sect2>
<sect2 id="zend.form.forms.subforms">
<title>Sub Forms</title>
<para>
Sub forms serve several purposes:
</para>
<itemizedlist>
<listitem>
<para>
Creating logical element groups. Since sub forms are simply
forms, you can validate subforms as individual entities.
</para>
</listitem>
<listitem>
<para>
Creating multi-page forms. Since sub forms are simply forms, you
can display a separate sub form per page, building up multi-page
forms where each form has its own validation logic. Only once
all sub forms validate would the form be considered complete.
</para>
</listitem>
<listitem>
<para>
Display groupings. Like display groups, sub forms, when rendered
as part of a larger form, can be used to group elements. Be
aware, however, that the master form object will have no
awareness of the elements in sub forms.
</para>
</listitem>
</itemizedlist>
<para>
A sub form may be a <classname>Zend_Form</classname> object, or, more
typically, a <classname>Zend_Form_SubForm</classname> object. The latter
contains decorators suitable for inclusion in a larger form (i.e.,
it does not render additional <acronym>HTML</acronym> form tags, but does group
elements). To attach a sub form, simply add it to the form and give
it a name:
</para>
<programlisting language="php"><![CDATA[
$form->addSubForm($subForm, 'subform');
]]></programlisting>
<para>
You can retrieve a sub form using either
<methodname>getSubForm($name)</methodname> or overloading using the sub form
name:
</para>
<programlisting language="php"><![CDATA[
// Using getSubForm():
$subForm = $form->getSubForm('subform');
// Using overloading:
$subForm = $form->subform;
]]></programlisting>
<para>
Sub forms are included in form iteration, although the elements they
contain are not.
</para>
<sect3 id="zend.form.forms.subforms.global">
<title>Global Operations</title>
<para>
Like elements and display groups, there are some operations that
might need to affect all sub forms. Unlike display groups and
elements, however, sub forms inherit most functionality from the
master form object, and the only real operation that may need to
be performed globally is setting decorators for sub forms. For
this purpose, there is the <methodname>setSubFormDecorators()</methodname>
method. In the next example, we'll set the decorator for all
subforms to be simply a fieldset (the FormElements decorator is
needed to ensure its elements are iterated):
</para>
<programlisting language="php"><![CDATA[
$form->setSubFormDecorators(array(
'FormElements',
'Fieldset'
));
]]></programlisting>
</sect3>
<sect3 id="zend.form.forms.subforms.methods">
<title>Methods for Interacting With Sub Forms</title>
<para>
The following methods may be used to interact with sub forms:
</para>
<itemizedlist>
<listitem>
<para>
<methodname>addSubForm(Zend_Form $form, $name, $order = null)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addSubForms(array $subForms)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setSubForms(array $subForms)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getSubForm($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getSubForms()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>removeSubForm($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>clearSubForms()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setSubFormDecorators(array $decorators)</methodname>
</para>
</listitem>
</itemizedlist>
</sect3>
</sect2>
<sect2 id="zend.form.forms.metadata">
<title>Metadata and Attributes</title>
<para>
While a form's usefulness primarily derives from the elements it
contains, it can also contain other metadata, such as a name (often
used as a unique ID in the <acronym>HTML</acronym> markup); the form action and method;
the number of elements, groups, and sub forms it contains; and
arbitrary metadata (usually used to set <acronym>HTML</acronym> attributes for the form
tag itself).
</para>
<para>
You can set and retrieve a form's name using the name accessors:
</para>
<programlisting language="php"><![CDATA[
// Set the name:
$form->setName('registration');
// Retrieve the name:
$name = $form->getName();
]]></programlisting>
<para>
To set the action (url to which the form submits) and method (method
by which it should submit, e.g., 'POST' or '<constant>GET</constant>'), use the action
and method accessors:
</para>
<programlisting language="php"><![CDATA[
// Set the action and method:
$form->setAction('/user/login')
->setMethod('post');
]]></programlisting>
<para>
You may also specify the form encoding type specifically using the
enctype accessors. <classname>Zend_Form</classname> defines two constants,
<constant>Zend_Form::ENCTYPE_URLENCODED</constant> and
<constant>Zend_Form::ENCTYPE_MULTIPART</constant>, corresponding to the
values 'application/x-www-form-urlencoded' and
'multipart/form-data', respectively; however, you can set this to
any arbitrary encoding type.
</para>
<programlisting language="php"><![CDATA[
// Set the action, method, and enctype:
$form->setAction('/user/login')
->setMethod('post')
->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
]]></programlisting>
<note>
<para>
The method, action, and enctype are only used internally for rendering,
and not for any sort of validation.
</para>
</note>
<para>
<classname>Zend_Form</classname> implements the <classname>Countable</classname>
interface, allowing you to pass it as an argument to count:
</para>
<programlisting language="php"><![CDATA[
$numItems = count($form);
]]></programlisting>
<para>
Setting arbitrary metadata is done through the attribs accessors.
Since overloading in <classname>Zend_Form</classname> is used to access
elements, display groups, and sub forms, this is the only method for
accessing metadata.
</para>
<programlisting language="php"><![CDATA[
// Setting attributes:
$form->setAttrib('class', 'zend-form')
->addAttribs(array(
'id' => 'registration',
'onSubmit' => 'validate(this)',
));
// Retrieving attributes:
$class = $form->getAttrib('class');
$attribs = $form->getAttribs();
// Remove an attribute:
$form->removeAttrib('onSubmit');
// Clear all attributes:
$form->clearAttribs();
]]></programlisting>
</sect2>
<sect2 id="zend.form.forms.decorators">
<title>Decorators</title>
<para>
Creating the markup for a form is often a time-consuming task,
particularly if you plan on re-using the same markup to show things
such as validation errors, submitted values, etc.
<classname>Zend_Form</classname>'s answer to this issue is
<emphasis>decorators</emphasis>.
</para>
<para>
Decorators for <classname>Zend_Form</classname> objects can be used to render
a form. The FormElements decorator will iterate through all items in
a form -- elements, display groups, and sub forms -- and render
them, returning the result. Additional decorators may then be used
to wrap this content, or append or prepend it.
</para>
<para>
The default decorators for <classname>Zend_Form</classname> are FormElements,
HtmlTag (wraps in a definition list), and Form; the equivalent code
for creating them is as follows:
</para>
<programlisting language="php"><![CDATA[
$form->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'Form'
));
]]></programlisting>
<para>
This creates output like the following:
</para>
<programlisting language="html"><![CDATA[
<form action="/form/action" method="post">
<dl>
...
</dl>
</form>
]]></programlisting>
<para>
Any attributes set on the form object will be used as <acronym>HTML</acronym>
attributes of the <emphasis><form></emphasis> tag.
</para>
<note>
<title>Default Decorators Do Not Need to Be Loaded</title>
<para>
By default, the default decorators are loaded during object
initialization. You can disable this by passing the
'disableLoadDefaultDecorators' option to the constructor:
</para>
<programlisting language="php"><![CDATA[
$form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
]]></programlisting>
<para>
This option may be mixed with any other options you pass,
both as array options or in a <classname>Zend_Config</classname> object.
</para>
</note>
<note>
<title>Using Multiple Decorators of the Same Type</title>
<para>
Internally, <classname>Zend_Form</classname> uses a decorator's
class as the lookup mechanism when retrieving decorators. As a
result, you cannot register multiple decorators of the same
type; subsequent decorators will simply overwrite those that
existed before.
</para>
<para>
To get around this, you can use aliases. Instead of passing a
decorator or decorator name as the first argument to
<methodname>addDecorator()</methodname>, pass an array with a single
element, with the alias pointing to the decorator object or
name:
</para>
<programlisting language="php"><![CDATA[
// Alias to 'FooBar':
$form->addDecorator(array('FooBar' => 'HtmlTag'), array('tag' => 'div'));
// And retrieve later:
$form = $element->getDecorator('FooBar');
]]></programlisting>
<para>
In the <methodname>addDecorators()</methodname> and
<methodname>setDecorators()</methodname> methods, you will need to pass
the 'decorator' option in the array representing the decorator:
</para>
<programlisting language="php"><![CDATA[
// Add two 'HtmlTag' decorators, aliasing one to 'FooBar':
$form->addDecorators(
array('HtmlTag', array('tag' => 'div')),
array(
'decorator' => array('FooBar' => 'HtmlTag'),
'options' => array('tag' => 'dd')
),
);
// And retrieve later:
$htmlTag = $form->getDecorator('HtmlTag');
$fooBar = $form->getDecorator('FooBar');
]]></programlisting>
</note>
<para>
You may create your own decorators for generating the form. One
common use case is if you know the exact <acronym>HTML</acronym> you wish to use; your
decorator could create the exact <acronym>HTML</acronym> and simply return it,
potentially using the decorators from individual elements or display
groups.
</para>
<para>
The following methods may be used to interact with decorators:
</para>
<itemizedlist>
<listitem>
<para>
<methodname>addDecorator($decorator, $options = null)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>addDecorators(array $decorators)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>setDecorators(array $decorators)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getDecorator($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>getDecorators()</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>removeDecorator($name)</methodname>
</para>
</listitem>
<listitem>
<para>
<methodname>clearDecorators()</methodname>
</para>
</listitem>
</itemizedlist>
<para>
<classname>Zend_Form</classname> also uses overloading to allow rendering
specific decorators. <methodname>__call()</methodname> will intercept methods
that lead with the text 'render' and use the remainder of the method
name to lookup a decorator; if found, it will then render that
<emphasis>single</emphasis> decorator. Any arguments passed to the
method call will be used as content to pass to the decorator's
<methodname>render()</methodname> method. As an example:
</para>
<programlisting language="php"><![CDATA[
// Render only the FormElements decorator:
echo $form->renderFormElements();
// Render only the fieldset decorator, passing in content:
echo $form->renderFieldset("<p>This is fieldset content</p>");
]]></programlisting>
<para>
If the decorator does not exist, an exception is raised.
</para>
</sect2>
<sect2 id="zend.form.forms.validation">
<title>Validation</title>
<para>
A primary use case for forms is validating submitted data.
<classname>Zend_Form</classname> allows you to validate an entire form, a partial form,
or responses for XmlHttpRequests (AJAX). If the submitted data is not valid, it has
methods for retrieving the various error codes and messages for
elements and sub forms.
</para>
<para>
To validate a full form, use the <methodname>isValid()</methodname> method:
</para>
<programlisting language="php"><![CDATA[
if (!$form->isValid($_POST)) {
// failed validation
}
]]></programlisting>
<para>
<methodname>isValid()</methodname> will validate every required element, and any
unrequired element contained in the submitted data.
</para>
<para>
Sometimes you may need to validate only a subset of the data; for
this, use <methodname>isValidPartial($data)</methodname>:
</para>
<programlisting language="php"><![CDATA[
if (!$form->isValidPartial($data)) {
// failed validation
}
]]></programlisting>
<para>
<methodname>isValidPartial()</methodname> only attempts to validate those items
in the data for which there are matching elements; if an element is
not represented in the data, it is skipped.
</para>
<para>
When validating elements or groups of elements for an <acronym>AJAX</acronym> request,
you will typically be validating a subset of the form, and want the
response back in <acronym>JSON</acronym>. <methodname>processAjax()</methodname> does
precisely that:
</para>
<programlisting language="php"><![CDATA[
$json = $form->processAjax($data);
]]></programlisting>
<para>
You can then simply send the <acronym>JSON</acronym> response to the client. If the
form is valid, this will be a boolean <constant>TRUE</constant> response. If not, it
will be a javascript object containing key/message pairs, where each
'message' is an array of validation error messages.
</para>
<para>
For forms that fail validation, you can retrieve both error codes
and error messages, using <methodname>getErrors()</methodname> and
<methodname>getMessages()</methodname>, respectively:
</para>
<programlisting language="php"><![CDATA[
$codes = $form->getErrors();
$messages = $form->getMessages();
]]></programlisting>
<note>
<para>
Since the messages returned by <methodname>getMessages()</methodname> are an
array of error code/message pairs, <methodname>getErrors()</methodname> is
typically not needed.
</para>
</note>
<para>
You can retrieve codes and error messages for individual elements by
simply passing the element name to each:
</para>
<programlisting language="php"><![CDATA[
$codes = $form->getErrors('username');
$messages = $form->getMessages('username');
]]></programlisting>
<note>
<para>
Note: When validating elements, <classname>Zend_Form</classname> sends a
second argument to each element's <methodname>isValid()</methodname> method:
the array of data being validated. This can then be used by
individual validators to allow them to utilize other submitted
values when determining the validity of the data. An example
would be a registration form that requires both a password and
password confirmation; the password element could use the
password confirmation as part of its validation.
</para>
</note>
<sect3 id="zend.form.forms.validation.errors">
<title>Custom Error Messages</title>
<para>
At times, you may want to specify one or more specific error
messages to use instead of the error messages generated by the
validators attached to your elements. Additionally, at times you
may want to mark the form invalid yourself. This
functionality is possible via the following methods.
</para>
<itemizedlist>
<listitem>
<para>
<methodname>addErrorMessage($message)</methodname>: add an error message
to display on form validation errors. You may call this more
than once, and new messages are appended to the stack.
</para>
</listitem>
<listitem>
<para>
<methodname>addErrorMessages(array $messages)</methodname>: add multiple
error messages to display on form validation errors.
</para>
</listitem>
<listitem>
<para>
<methodname>setErrorMessages(array $messages)</methodname>: add multiple
error messages to display on form validation errors,
overwriting all previously set error messages.
</para>
</listitem>
<listitem>
<para>
<methodname>getErrorMessages()</methodname>: retrieve the list of
custom error messages that have been defined.
</para>
</listitem>
<listitem>
<para>
<methodname>clearErrorMessages()</methodname>: remove all custom error
messages that have been defined.
</para>
</listitem>
<listitem>
<para>
<methodname>markAsError()</methodname>: mark the form as having
failed validation.
</para>
</listitem>
<listitem>
<para>
<methodname>addError($message)</methodname>: add a message to the custom
error messages stack and flag the form as invalid.
</para>
</listitem>
<listitem>
<para>
<methodname>addErrors(array $messages)</methodname>: add several
messages to the custom error messages stack and flag the
form as invalid.
</para>
</listitem>
<listitem>
<para>
<methodname>setErrors(array $messages)</methodname>: overwrite the
custom error messages stack with the provided messages and
flag the form as invalid.
</para>
</listitem>
</itemizedlist>
<para>
All errors set in this fashion may be translated.
</para>
</sect3>
<sect3 id="zend.form.forms.validation.values">
<title>Retrieving Valid Values Only</title>
<para>
There are scenarios when you want to allow your user to work on a valid form
in several steps. Meanwhile you allow the user to save the form with any
set of values inbetween. Then if all the data is specified you can transfer
the model from the building or prototying stage to a valid stage.
</para>
<para>
You can retrieve all the valid values that match the submitted data by calling:
</para>
<programlisting language="php"><![CDATA[
$validValues = $form->getValidValues($_POST);
]]></programlisting>
</sect3>
</sect2>
<sect2 id="zend.form.forms.methods">
<title>Methods</title>
<para>
The following is a full list of methods available to
<classname>Zend_Form</classname>, grouped by type:
</para>
<itemizedlist>
<listitem>
<para>Configuration and Options:</para>
<itemizedlist>
<listitem>
<para><methodname>setOptions(array $options)</methodname></para>
</listitem>
<listitem>
<para><methodname>setConfig(Zend_Config $config)</methodname></para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Plugin Loaders and paths:</para>
<itemizedlist>
<listitem>
<para>
<methodname>setPluginLoader(Zend_Loader_PluginLoader_Interface $loader,
$type = null)</methodname>
</para>
</listitem>
<listitem>
<para><methodname>getPluginLoader($type = null)</methodname></para>
</listitem>
<listitem>
<para>
<methodname>addPrefixPath($prefix, $path, $type = null) </methodname>
</para>
</listitem>
<listitem>
<para><methodname>addPrefixPaths(array $spec)</methodname></para>
</listitem>
<listitem>
<para>
<methodname>addElementPrefixPath($prefix, $path, $type
= null)</methodname>
</para>
</listitem>
<listitem>
<para><methodname>addElementPrefixPaths(array $spec)</methodname></para>
</listitem>
<listitem>
<para>
<methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname>
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Metadata:</para>
<itemizedlist>
<listitem>
<para><methodname>setAttrib($key, $value)</methodname></para>
</listitem>
<listitem>
<para><methodname>addAttribs(array $attribs)</methodname></para>
</listitem>
<listitem>
<para><methodname>setAttribs(array $attribs)</methodname></para>
</listitem>
<listitem><para><methodname>getAttrib($key)</methodname></para></listitem>
<listitem><para><methodname>getAttribs()</methodname></para></listitem>
<listitem><para><methodname>removeAttrib($key)</methodname></para></listitem>
<listitem><para><methodname>clearAttribs()</methodname></para></listitem>
<listitem><para><methodname>setAction($action)</methodname></para></listitem>
<listitem><para><methodname>getAction()</methodname></para></listitem>
<listitem><para><methodname>setMethod($method)</methodname></para></listitem>
<listitem><para><methodname>getMethod()</methodname></para></listitem>
<listitem><para><methodname>setName($name)</methodname></para></listitem>
<listitem><para><methodname>getName()</methodname></para></listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Elements:</para>
<itemizedlist>
<listitem>
<para>
<methodname>addElement($element, $name = null, $options
= null)</methodname>
</para>
</listitem>
<listitem>
<para><methodname>addElements(array $elements)</methodname></para>
</listitem>
<listitem>
<para><methodname>setElements(array $elements)</methodname></para>
</listitem>
<listitem><para><methodname>getElement($name)</methodname></para></listitem>
<listitem><para><methodname>getElements()</methodname></para></listitem>
<listitem><para><methodname>removeElement($name)</methodname></para></listitem>
<listitem><para><methodname>clearElements()</methodname></para></listitem>
<listitem>
<para><methodname>setDefaults(array $defaults)</methodname></para>
</listitem>
<listitem>
<para><methodname>setDefault($name, $value)</methodname></para>
</listitem>
<listitem><para><methodname>getValue($name)</methodname></para></listitem>
<listitem><para><methodname>getValues()</methodname></para></listitem>
<listitem>
<para><methodname>getUnfilteredValue($name)</methodname></para>
</listitem>
<listitem><para><methodname>getUnfilteredValues()</methodname></para></listitem>
<listitem>
<para><methodname>setElementFilters(array $filters)</methodname></para>
</listitem>
<listitem>
<para>
<methodname>setElementDecorators(array $decorators)</methodname>
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Sub forms:</para>
<itemizedlist>
<listitem>
<para>
<methodname>addSubForm(Zend_Form $form, $name, $order
= null)</methodname>
</para>
</listitem>
<listitem>
<para><methodname>addSubForms(array $subForms)</methodname></para>
</listitem>
<listitem>
<para><methodname>setSubForms(array $subForms)</methodname></para>
</listitem>
<listitem><para><methodname>getSubForm($name)</methodname></para></listitem>
<listitem><para><methodname>getSubForms()</methodname></para></listitem>
<listitem><para><methodname>removeSubForm($name)</methodname></para></listitem>
<listitem><para><methodname>clearSubForms()</methodname></para></listitem>
<listitem>
<para>
<methodname>setSubFormDecorators(array $decorators)</methodname>
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Display groups:</para>
<itemizedlist>
<listitem>
<para>
<methodname>addDisplayGroup(array $elements, $name, $options
= null)</methodname>
</para>
</listitem>
<listitem>
<para><methodname>addDisplayGroups(array $groups)</methodname></para>
</listitem>
<listitem>
<para><methodname>setDisplayGroups(array $groups)</methodname></para>
</listitem>
<listitem>
<para><methodname>getDisplayGroup($name)</methodname></para>
</listitem>
<listitem><para><methodname>getDisplayGroups()</methodname></para></listitem>
<listitem>
<para><methodname>removeDisplayGroup($name)</methodname></para>
</listitem>
<listitem><para><methodname>clearDisplayGroups()</methodname></para></listitem>
<listitem>
<para>
<methodname>setDisplayGroupDecorators(array $decorators)</methodname>
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Validation</para>
<itemizedlist>
<listitem>
<para><methodname>populate(array $values)</methodname></para>
</listitem>
<listitem><para><methodname>isValid(array $data)</methodname></para></listitem>
<listitem>
<para><methodname>isValidPartial(array $data)</methodname></para>
</listitem>
<listitem>
<para><methodname>processAjax(array $data)</methodname></para>
</listitem>
<listitem><para><methodname>persistData()</methodname></para></listitem>
<listitem>
<para><methodname>getErrors($name = null)</methodname></para>
</listitem>
<listitem>
<para><methodname>getMessages($name = null)</methodname></para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Rendering:</para>
<itemizedlist>
<listitem>
<para>
<methodname>setView(Zend_View_Interface $view = null)</methodname>
</para>
</listitem>
<listitem><para><methodname>getView()</methodname></para></listitem>
<listitem>
<para>
<methodname>addDecorator($decorator, $options = null)</methodname>
</para>
</listitem>
<listitem>
<para><methodname>addDecorators(array $decorators)</methodname></para>
</listitem>
<listitem>
<para><methodname>setDecorators(array $decorators)</methodname></para>
</listitem>
<listitem><para><methodname>getDecorator($name)</methodname></para></listitem>
<listitem><para><methodname>getDecorators()</methodname></para></listitem>
<listitem>
<para><methodname>removeDecorator($name)</methodname></para>
</listitem>
<listitem><para><methodname>clearDecorators()</methodname></para></listitem>
<listitem>
<para>
<methodname>render(Zend_View_Interface $view = null)</methodname>
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>I18n:</para>
<itemizedlist>
<listitem>
<para>
<methodname>setTranslator(Zend_Translate_Adapter $translator
= null)</methodname>
</para>
</listitem>
<listitem><para><methodname>getTranslator()</methodname></para></listitem>
<listitem>
<para><methodname>setDisableTranslator($flag)</methodname></para>
</listitem>
<listitem>
<para><methodname>translatorIsDisabled()</methodname></para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="zend.form.forms.config">
<title>Configuration</title>
<para>
<classname>Zend_Form</classname> is fully configurable via
<methodname>setOptions()</methodname> and <methodname>setConfig()</methodname> (or by
passing options or a <classname>Zend_Config</classname> object to the
constructor). Using these methods, you can specify form elements,
display groups, decorators, and metadata.
</para>
<para>
As a general rule, if 'set' + the option key refers to a
<classname>Zend_Form</classname> method, then the value provided will be
passed to that method. If the accessor does not exist, the key is
assumed to reference an attribute, and will be passed to
<methodname>setAttrib()</methodname>.
</para>
<para>
Exceptions to the rule include the following:
</para>
<itemizedlist>
<listitem>
<para>
<property>prefixPath</property> will be passed to
<methodname>addPrefixPaths()</methodname>
</para>
</listitem>
<listitem>
<para>
<property>elementPrefixPath</property> will be passed to
<methodname>addElementPrefixPaths()</methodname>
</para>
</listitem>
<listitem>
<para>
<property>displayGroupPrefixPath</property> will be passed to
<methodname>addDisplayGroupPrefixPaths()</methodname>
</para>
</listitem>
<listitem>
<para>the following setters cannot be set in this way:</para>
<itemizedlist>
<listitem>
<para><property>setAttrib</property> (though setAttribs *will* work)</para>
</listitem>
<listitem><para><property>setConfig</property></para></listitem>
<listitem><para><property>setDefault</property></para></listitem>
<listitem><para><property>setOptions</property></para></listitem>
<listitem><para><property>setPluginLoader</property></para></listitem>
<listitem><para><property>setSubForms</property></para></listitem>
<listitem><para><property>setTranslator</property></para></listitem>
<listitem><para><property>setView</property></para></listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<para>
As an example, here is a config file that passes configuration for
every type of configurable data:
</para>
<programlisting language="ini"><![CDATA[
[element]
name = "registration"
action = "/user/register"
method = "post"
attribs.class = "zend_form"
attribs.onclick = "validate(this)"
disableTranslator = 0
prefixPath.element.prefix = "My_Element"
prefixPath.element.path = "My/Element/"
elementPrefixPath.validate.prefix = "My_Validate"
elementPrefixPath.validate.path = "My/Validate/"
displayGroupPrefixPath.prefix = "My_Group"
displayGroupPrefixPath.path = "My/Group/"
elements.username.type = "text"
elements.username.options.label = "Username"
elements.username.options.validators.alpha.validator = "Alpha"
elements.username.options.filters.lcase = "StringToLower"
; more elements, of course...
elementFilters.trim = "StringTrim"
;elementDecorators.trim = "StringTrim"
displayGroups.login.elements.username = "username"
displayGroups.login.elements.password = "password"
displayGroupDecorators.elements.decorator = "FormElements"
displayGroupDecorators.fieldset.decorator = "Fieldset"
decorators.elements.decorator = "FormElements"
decorators.fieldset.decorator = "FieldSet"
decorators.fieldset.decorator.options.class = "zend_form"
decorators.form.decorator = "Form"
]]></programlisting>
<para>
The above could easily be abstracted to an <acronym>XML</acronym> or
<acronym>PHP</acronym> array-based configuration file.
</para>
</sect2>
<sect2 id="zend.form.forms.custom">
<title>Custom forms</title>
<para>
An alternative to using configuration-based forms is to subclass
<classname>Zend_Form</classname>. This has several benefits:
</para>
<itemizedlist>
<listitem>
<para>
You can unit test your form easily to ensure validations and
rendering perform as expected.
</para>
</listitem>
<listitem>
<para>
Fine-grained control over individual elements.
</para>
</listitem>
<listitem>
<para>
Re-use of form objects, and greater portability (no need to
track config files).
</para>
</listitem>
<listitem>
<para>
Implementing custom functionality.
</para>
</listitem>
</itemizedlist>
<para>
The most typical use case would be to use the
<methodname>init()</methodname> method to setup specific form elements and
configuration:
</para>
<programlisting language="php"><![CDATA[
class My_Form_Login extends Zend_Form
{
public function init()
{
$username = new Zend_Form_Element_Text('username');
$username->class = 'formtext';
$username->setLabel('Username:')
->setDecorators(array(
array('ViewHelper',
array('helper' => 'formText')),
array('Label',
array('class' => 'label'))
));
$password = new Zend_Form_Element_Password('password');
$password->class = 'formtext';
$password->setLabel('Password:')
->setDecorators(array(
array('ViewHelper',
array('helper' => 'formPassword')),
array('Label',
array('class' => 'label'))
));
$submit = new Zend_Form_Element_Submit('login');
$submit->class = 'formsubmit';
$submit->setValue('Login')
->setDecorators(array(
array('ViewHelper',
array('helper' => 'formSubmit'))
));
$this->addElements(array(
$username,
$password,
$submit
));
$this->setDecorators(array(
'FormElements',
'Fieldset',
'Form'
));
}
}
]]></programlisting>
<para>
This form can then be instantiated with simply:
</para>
<programlisting language="php"><![CDATA[
$form = new My_Form_Login();
]]></programlisting>
<para>
and all functionality is already setup and ready; no config files
needed. (Note that this example is greatly simplified, as it
contains no validators or filters for the elements.)
</para>
<para>
Another common reason for extension is to define a set of
default decorators. You can do this by overriding the
<methodname>loadDefaultDecorators()</methodname> method:
</para>
<programlisting language="php"><![CDATA[
class My_Form_Login extends Zend_Form
{
public function loadDefaultDecorators()
{
$this->setDecorators(array(
'FormElements',
'Fieldset',
'Form'
));
}
}
]]></programlisting>
</sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
|