Form.php 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Validate_Interface */
  21. require_once 'Zend/Validate/Interface.php';
  22. /**
  23. * Zend_Form
  24. *
  25. * @category Zend
  26. * @package Zend_Form
  27. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @version $Id$
  30. */
  31. class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
  32. {
  33. /**#@+
  34. * Plugin loader type constants
  35. */
  36. const DECORATOR = 'DECORATOR';
  37. const ELEMENT = 'ELEMENT';
  38. /**#@-*/
  39. /**#@+
  40. * Method type constants
  41. */
  42. const METHOD_DELETE = 'delete';
  43. const METHOD_GET = 'get';
  44. const METHOD_POST = 'post';
  45. const METHOD_PUT = 'put';
  46. /**#@-*/
  47. /**#@+
  48. * Encoding type constants
  49. */
  50. const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
  51. const ENCTYPE_MULTIPART = 'multipart/form-data';
  52. /**#@-*/
  53. /**
  54. * Form metadata and attributes
  55. * @var array
  56. */
  57. protected $_attribs = array();
  58. /**
  59. * Decorators for rendering
  60. * @var array
  61. */
  62. protected $_decorators = array();
  63. /**
  64. * Default display group class
  65. * @var string
  66. */
  67. protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup';
  68. /**
  69. * Form description
  70. * @var string
  71. */
  72. protected $_description;
  73. /**
  74. * Should we disable loading the default decorators?
  75. * @var bool
  76. */
  77. protected $_disableLoadDefaultDecorators = false;
  78. /**
  79. * Display group prefix paths
  80. * @var array
  81. */
  82. protected $_displayGroupPrefixPaths = array();
  83. /**
  84. * Groups of elements grouped for display purposes
  85. * @var array
  86. */
  87. protected $_displayGroups = array();
  88. /**
  89. * Global decorators to apply to all elements
  90. * @var null|array
  91. */
  92. protected $_elementDecorators;
  93. /**
  94. * Prefix paths to use when creating elements
  95. * @var array
  96. */
  97. protected $_elementPrefixPaths = array();
  98. /**
  99. * Form elements
  100. * @var array
  101. */
  102. protected $_elements = array();
  103. /**
  104. * Array to which elements belong (if any)
  105. * @var string
  106. */
  107. protected $_elementsBelongTo;
  108. /**
  109. * Custom form-level error messages
  110. * @var array
  111. */
  112. protected $_errorMessages = array();
  113. /**
  114. * Are there errors in the form?
  115. * @var bool
  116. */
  117. protected $_errorsExist = false;
  118. /**
  119. * Form order
  120. * @var int|null
  121. */
  122. protected $_formOrder;
  123. /**
  124. * Whether or not form elements are members of an array
  125. * @var bool
  126. */
  127. protected $_isArray = false;
  128. /**
  129. * Form legend
  130. * @var string
  131. */
  132. protected $_legend;
  133. /**
  134. * Plugin loaders
  135. * @var array
  136. */
  137. protected $_loaders = array();
  138. /**
  139. * Allowed form methods
  140. * @var array
  141. */
  142. protected $_methods = array('delete', 'get', 'post', 'put');
  143. /**
  144. * Order in which to display and iterate elements
  145. * @var array
  146. */
  147. protected $_order = array();
  148. /**
  149. * Whether internal order has been updated or not
  150. * @var bool
  151. */
  152. protected $_orderUpdated = false;
  153. /**
  154. * Sub form prefix paths
  155. * @var array
  156. */
  157. protected $_subFormPrefixPaths = array();
  158. /**
  159. * Sub forms
  160. * @var array
  161. */
  162. protected $_subForms = array();
  163. /**
  164. * @var Zend_Translate
  165. */
  166. protected $_translator;
  167. /**
  168. * Global default translation adapter
  169. * @var Zend_Translate
  170. */
  171. protected static $_translatorDefault;
  172. /**
  173. * is the translator disabled?
  174. * @var bool
  175. */
  176. protected $_translatorDisabled = false;
  177. /**
  178. * @var Zend_View_Interface
  179. */
  180. protected $_view;
  181. /**
  182. * Constructor
  183. *
  184. * Registers form view helper as decorator
  185. *
  186. * @param mixed $options
  187. * @return void
  188. */
  189. public function __construct($options = null)
  190. {
  191. if (is_array($options)) {
  192. $this->setOptions($options);
  193. } elseif ($options instanceof Zend_Config) {
  194. $this->setConfig($options);
  195. }
  196. // Extensions...
  197. $this->init();
  198. $this->loadDefaultDecorators();
  199. }
  200. /**
  201. * Clone form object and all children
  202. *
  203. * @return void
  204. */
  205. public function __clone()
  206. {
  207. $elements = array();
  208. foreach ($this->getElements() as $name => $element) {
  209. $elements[] = clone $element;
  210. }
  211. $this->setElements($elements);
  212. $subForms = array();
  213. foreach ($this->getSubForms() as $name => $subForm) {
  214. $subForms[$name] = clone $subForm;
  215. }
  216. $this->setSubForms($subForms);
  217. $displayGroups = array();
  218. foreach ($this->_displayGroups as $group) {
  219. $clone = clone $group;
  220. $elements = array();
  221. foreach ($clone->getElements() as $name => $e) {
  222. $elements[] = $this->getElement($name);
  223. }
  224. $clone->setElements($elements);
  225. $displayGroups[] = $clone;
  226. }
  227. $this->setDisplayGroups($displayGroups);
  228. }
  229. /**
  230. * Reset values of form
  231. *
  232. * @return Zend_Form
  233. */
  234. public function reset()
  235. {
  236. foreach ($this->getElements() as $element) {
  237. $element->setValue(null);
  238. }
  239. foreach ($this->getSubForms() as $subForm) {
  240. $subForm->reset();
  241. }
  242. return $this;
  243. }
  244. /**
  245. * Initialize form (used by extending classes)
  246. *
  247. * @return void
  248. */
  249. public function init()
  250. {
  251. }
  252. /**
  253. * Set form state from options array
  254. *
  255. * @param array $options
  256. * @return Zend_Form
  257. */
  258. public function setOptions(array $options)
  259. {
  260. if (isset($options['prefixPath'])) {
  261. $this->addPrefixPaths($options['prefixPath']);
  262. unset($options['prefixPath']);
  263. }
  264. if (isset($options['elementPrefixPath'])) {
  265. $this->addElementPrefixPaths($options['elementPrefixPath']);
  266. unset($options['elementPrefixPath']);
  267. }
  268. if (isset($options['displayGroupPrefixPath'])) {
  269. $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);
  270. unset($options['displayGroupPrefixPath']);
  271. }
  272. if (isset($options['elementDecorators'])) {
  273. $this->_elementDecorators = $options['elementDecorators'];
  274. unset($options['elementDecorators']);
  275. }
  276. if (isset($options['elements'])) {
  277. $this->setElements($options['elements']);
  278. unset($options['elements']);
  279. }
  280. if (isset($options['defaultDisplayGroupClass'])) {
  281. $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
  282. unset($options['defaultDisplayGroupClass']);
  283. }
  284. if (isset($options['displayGroupDecorators'])) {
  285. $displayGroupDecorators = $options['displayGroupDecorators'];
  286. unset($options['displayGroupDecorators']);
  287. }
  288. if (isset($options['elementsBelongTo'])) {
  289. $elementsBelongTo = $options['elementsBelongTo'];
  290. unset($options['elementsBelongTo']);
  291. }
  292. if (isset($options['attribs'])) {
  293. $this->addAttribs($options['attribs']);
  294. unset($options['attribs']);
  295. }
  296. $forbidden = array(
  297. 'Options', 'Config', 'PluginLoader', 'SubForms', 'View', 'Translator',
  298. 'Attrib', 'Default',
  299. );
  300. foreach ($options as $key => $value) {
  301. $normalized = ucfirst($key);
  302. if (in_array($normalized, $forbidden)) {
  303. continue;
  304. }
  305. $method = 'set' . $normalized;
  306. if (method_exists($this, $method)) {
  307. $this->$method($value);
  308. } else {
  309. $this->setAttrib($key, $value);
  310. }
  311. }
  312. if (isset($displayGroupDecorators)) {
  313. $this->setDisplayGroupDecorators($displayGroupDecorators);
  314. }
  315. if (isset($elementsBelongTo)) {
  316. $this->setElementsBelongTo($elementsBelongTo);
  317. }
  318. return $this;
  319. }
  320. /**
  321. * Set form state from config object
  322. *
  323. * @param Zend_Config $config
  324. * @return Zend_Form
  325. */
  326. public function setConfig(Zend_Config $config)
  327. {
  328. return $this->setOptions($config->toArray());
  329. }
  330. // Loaders
  331. /**
  332. * Set plugin loaders for use with decorators and elements
  333. *
  334. * @param Zend_Loader_PluginLoader_Interface $loader
  335. * @param string $type 'decorator' or 'element'
  336. * @return Zend_Form
  337. * @throws Zend_Form_Exception on invalid type
  338. */
  339. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
  340. {
  341. $type = strtoupper($type);
  342. switch ($type) {
  343. case self::DECORATOR:
  344. case self::ELEMENT:
  345. $this->_loaders[$type] = $loader;
  346. return $this;
  347. default:
  348. require_once 'Zend/Form/Exception.php';
  349. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
  350. }
  351. }
  352. /**
  353. * Retrieve plugin loader for given type
  354. *
  355. * $type may be one of:
  356. * - decorator
  357. * - element
  358. *
  359. * If a plugin loader does not exist for the given type, defaults are
  360. * created.
  361. *
  362. * @param string $type
  363. * @return Zend_Loader_PluginLoader_Interface
  364. */
  365. public function getPluginLoader($type = null)
  366. {
  367. $type = strtoupper($type);
  368. if (!isset($this->_loaders[$type])) {
  369. switch ($type) {
  370. case self::DECORATOR:
  371. $prefixSegment = 'Form_Decorator';
  372. $pathSegment = 'Form/Decorator';
  373. break;
  374. case self::ELEMENT:
  375. $prefixSegment = 'Form_Element';
  376. $pathSegment = 'Form/Element';
  377. break;
  378. default:
  379. require_once 'Zend/Form/Exception.php';
  380. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  381. }
  382. require_once 'Zend/Loader/PluginLoader.php';
  383. $this->_loaders[$type] = new Zend_Loader_PluginLoader(
  384. array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
  385. );
  386. }
  387. return $this->_loaders[$type];
  388. }
  389. /**
  390. * Add prefix path for plugin loader
  391. *
  392. * If no $type specified, assumes it is a base path for both filters and
  393. * validators, and sets each according to the following rules:
  394. * - decorators: $prefix = $prefix . '_Decorator'
  395. * - elements: $prefix = $prefix . '_Element'
  396. *
  397. * Otherwise, the path prefix is set on the appropriate plugin loader.
  398. *
  399. * If $type is 'decorators', sets the path in the decorator plugin loader
  400. * for all elements. Additionally, if no $type is provided,
  401. * {@link Zend_Form_Element::addPrefixPath()} is called on each element.
  402. *
  403. * @param string $path
  404. * @return Zend_Form
  405. * @throws Zend_Form_Exception for invalid type
  406. */
  407. public function addPrefixPath($prefix, $path, $type = null)
  408. {
  409. $type = strtoupper($type);
  410. switch ($type) {
  411. case self::DECORATOR:
  412. case self::ELEMENT:
  413. $loader = $this->getPluginLoader($type);
  414. $loader->addPrefixPath($prefix, $path);
  415. return $this;
  416. case null:
  417. $prefix = rtrim($prefix, '_');
  418. $path = rtrim($path, DIRECTORY_SEPARATOR);
  419. foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
  420. $cType = ucfirst(strtolower($type));
  421. $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
  422. $pluginPrefix = $prefix . '_' . $cType;
  423. $loader = $this->getPluginLoader($type);
  424. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  425. }
  426. return $this;
  427. default:
  428. require_once 'Zend/Form/Exception.php';
  429. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  430. }
  431. }
  432. /**
  433. * Add many prefix paths at once
  434. *
  435. * @param array $spec
  436. * @return Zend_Form
  437. */
  438. public function addPrefixPaths(array $spec)
  439. {
  440. if (isset($spec['prefix']) && isset($spec['path'])) {
  441. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  442. }
  443. foreach ($spec as $type => $paths) {
  444. if (is_numeric($type) && is_array($paths)) {
  445. $type = null;
  446. if (isset($paths['prefix']) && isset($paths['path'])) {
  447. if (isset($paths['type'])) {
  448. $type = $paths['type'];
  449. }
  450. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  451. }
  452. } elseif (!is_numeric($type)) {
  453. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  454. continue;
  455. }
  456. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  457. }
  458. }
  459. return $this;
  460. }
  461. /**
  462. * Add prefix path for all elements
  463. *
  464. * @param string $prefix
  465. * @param string $path
  466. * @param string $type
  467. * @return Zend_Form
  468. */
  469. public function addElementPrefixPath($prefix, $path, $type = null)
  470. {
  471. $this->_elementPrefixPaths[] = array(
  472. 'prefix' => $prefix,
  473. 'path' => $path,
  474. 'type' => $type,
  475. );
  476. foreach ($this->getElements() as $element) {
  477. $element->addPrefixPath($prefix, $path, $type);
  478. }
  479. foreach ($this->getSubForms() as $subForm) {
  480. $subForm->addElementPrefixPath($prefix, $path, $type);
  481. }
  482. return $this;
  483. }
  484. /**
  485. * Add prefix paths for all elements
  486. *
  487. * @param array $spec
  488. * @return Zend_Form
  489. */
  490. public function addElementPrefixPaths(array $spec)
  491. {
  492. $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
  493. foreach ($this->getElements() as $element) {
  494. $element->addPrefixPaths($spec);
  495. }
  496. return $this;
  497. }
  498. /**
  499. * Add prefix path for all display groups
  500. *
  501. * @param string $prefix
  502. * @param string $path
  503. * @return Zend_Form
  504. */
  505. public function addDisplayGroupPrefixPath($prefix, $path)
  506. {
  507. $this->_displayGroupPrefixPaths[] = array(
  508. 'prefix' => $prefix,
  509. 'path' => $path,
  510. );
  511. foreach ($this->getDisplayGroups() as $group) {
  512. $group->addPrefixPath($prefix, $path);
  513. }
  514. return $this;
  515. }
  516. /**
  517. * Add multiple display group prefix paths at once
  518. *
  519. * @param array $spec
  520. * @return Zend_Form
  521. */
  522. public function addDisplayGroupPrefixPaths(array $spec)
  523. {
  524. foreach ($spec as $key => $value) {
  525. if (is_string($value) && !is_numeric($key)) {
  526. $this->addDisplayGroupPrefixPath($key, $value);
  527. continue;
  528. }
  529. if (is_string($value) && is_numeric($key)) {
  530. continue;
  531. }
  532. if (is_array($value)) {
  533. $count = count($value);
  534. if (array_keys($value) === range(0, $count - 1)) {
  535. if ($count < 2) {
  536. continue;
  537. }
  538. $prefix = array_shift($value);
  539. $path = array_shift($value);
  540. $this->addDisplayGroupPrefixPath($prefix, $path);
  541. continue;
  542. }
  543. if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
  544. $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
  545. }
  546. }
  547. }
  548. return $this;
  549. }
  550. // Form metadata:
  551. /**
  552. * Set form attribute
  553. *
  554. * @param string $key
  555. * @param mixed $value
  556. * @return Zend_Form
  557. */
  558. public function setAttrib($key, $value)
  559. {
  560. $key = (string) $key;
  561. $this->_attribs[$key] = $value;
  562. return $this;
  563. }
  564. /**
  565. * Add multiple form attributes at once
  566. *
  567. * @param array $attribs
  568. * @return Zend_Form
  569. */
  570. public function addAttribs(array $attribs)
  571. {
  572. foreach ($attribs as $key => $value) {
  573. $this->setAttrib($key, $value);
  574. }
  575. return $this;
  576. }
  577. /**
  578. * Set multiple form attributes at once
  579. *
  580. * Overwrites any previously set attributes.
  581. *
  582. * @param array $attribs
  583. * @return Zend_Form
  584. */
  585. public function setAttribs(array $attribs)
  586. {
  587. $this->clearAttribs();
  588. return $this->addAttribs($attribs);
  589. }
  590. /**
  591. * Retrieve a single form attribute
  592. *
  593. * @param string $key
  594. * @return mixed
  595. */
  596. public function getAttrib($key)
  597. {
  598. $key = (string) $key;
  599. if (!isset($this->_attribs[$key])) {
  600. return null;
  601. }
  602. return $this->_attribs[$key];
  603. }
  604. /**
  605. * Retrieve all form attributes/metadata
  606. *
  607. * @return array
  608. */
  609. public function getAttribs()
  610. {
  611. return $this->_attribs;
  612. }
  613. /**
  614. * Remove attribute
  615. *
  616. * @param string $key
  617. * @return bool
  618. */
  619. public function removeAttrib($key)
  620. {
  621. if (isset($this->_attribs[$key])) {
  622. unset($this->_attribs[$key]);
  623. return true;
  624. }
  625. return false;
  626. }
  627. /**
  628. * Clear all form attributes
  629. *
  630. * @return Zend_Form
  631. */
  632. public function clearAttribs()
  633. {
  634. $this->_attribs = array();
  635. return $this;
  636. }
  637. /**
  638. * Set form action
  639. *
  640. * @param string $action
  641. * @return Zend_Form
  642. */
  643. public function setAction($action)
  644. {
  645. return $this->setAttrib('action', (string) $action);
  646. }
  647. /**
  648. * Get form action
  649. *
  650. * Sets default to '' if not set.
  651. *
  652. * @return string
  653. */
  654. public function getAction()
  655. {
  656. $action = $this->getAttrib('action');
  657. if (null === $action) {
  658. $action = '';
  659. $this->setAction($action);
  660. }
  661. return $action;
  662. }
  663. /**
  664. * Set form method
  665. *
  666. * Only values in {@link $_methods()} allowed
  667. *
  668. * @param string $method
  669. * @return Zend_Form
  670. * @throws Zend_Form_Exception
  671. */
  672. public function setMethod($method)
  673. {
  674. $method = strtolower($method);
  675. if (!in_array($method, $this->_methods)) {
  676. require_once 'Zend/Form/Exception.php';
  677. throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
  678. }
  679. $this->setAttrib('method', $method);
  680. return $this;
  681. }
  682. /**
  683. * Retrieve form method
  684. *
  685. * @return string
  686. */
  687. public function getMethod()
  688. {
  689. if (null === ($method = $this->getAttrib('method'))) {
  690. $method = self::METHOD_POST;
  691. $this->setAttrib('method', $method);
  692. }
  693. return strtolower($method);
  694. }
  695. /**
  696. * Set encoding type
  697. *
  698. * @param string $value
  699. * @return Zend_Form
  700. */
  701. public function setEnctype($value)
  702. {
  703. $this->setAttrib('enctype', $value);
  704. return $this;
  705. }
  706. /**
  707. * Get encoding type
  708. *
  709. * @return string
  710. */
  711. public function getEnctype()
  712. {
  713. if (null === ($enctype = $this->getAttrib('enctype'))) {
  714. $enctype = self::ENCTYPE_URLENCODED;
  715. $this->setAttrib('enctype', $enctype);
  716. }
  717. return $this->getAttrib('enctype');
  718. }
  719. /**
  720. * Filter a name to only allow valid variable characters
  721. *
  722. * @param string $value
  723. * @param bool $allowBrackets
  724. * @return string
  725. */
  726. public function filterName($value, $allowBrackets = false)
  727. {
  728. $charset = '^a-zA-Z0-9_\x7f-\xff';
  729. if ($allowBrackets) {
  730. $charset .= '\[\]';
  731. }
  732. return preg_replace('/[' . $charset . ']/', '', (string) $value);
  733. }
  734. /**
  735. * Set form name
  736. *
  737. * @param string $name
  738. * @return Zend_Form
  739. */
  740. public function setName($name)
  741. {
  742. $name = $this->filterName($name);
  743. if (('0' !== $name) && empty($name)) {
  744. require_once 'Zend/Form/Exception.php';
  745. throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
  746. }
  747. return $this->setAttrib('name', $name);
  748. }
  749. /**
  750. * Get name attribute
  751. *
  752. * @return null|string
  753. */
  754. public function getName()
  755. {
  756. return $this->getAttrib('name');
  757. }
  758. /**
  759. * Get fully qualified name
  760. *
  761. * Places name as subitem of array and/or appends brackets.
  762. *
  763. * @return string
  764. */
  765. public function getFullyQualifiedName()
  766. {
  767. return $this->getName();
  768. }
  769. /**
  770. * Get element id
  771. *
  772. * @return string
  773. */
  774. public function getId()
  775. {
  776. if (null !== ($id = $this->getAttrib('id'))) {
  777. return $id;
  778. }
  779. $id = $this->getFullyQualifiedName();
  780. // Bail early if no array notation detected
  781. if (!strstr($id, '[')) {
  782. return $id;
  783. }
  784. // Strip array notation
  785. if ('[]' == substr($id, -2)) {
  786. $id = substr($id, 0, strlen($id) - 2);
  787. }
  788. $id = str_replace('][', '-', $id);
  789. $id = str_replace(array(']', '['), '-', $id);
  790. $id = trim($id, '-');
  791. return $id;
  792. }
  793. /**
  794. * Set form legend
  795. *
  796. * @param string $value
  797. * @return Zend_Form
  798. */
  799. public function setLegend($value)
  800. {
  801. $this->_legend = (string) $value;
  802. return $this;
  803. }
  804. /**
  805. * Get form legend
  806. *
  807. * @return string
  808. */
  809. public function getLegend()
  810. {
  811. return $this->_legend;
  812. }
  813. /**
  814. * Set form description
  815. *
  816. * @param string $value
  817. * @return Zend_Form
  818. */
  819. public function setDescription($value)
  820. {
  821. $this->_description = (string) $value;
  822. return $this;
  823. }
  824. /**
  825. * Retrieve form description
  826. *
  827. * @return string
  828. */
  829. public function getDescription()
  830. {
  831. return $this->_description;
  832. }
  833. /**
  834. * Set form order
  835. *
  836. * @param int $index
  837. * @return Zend_Form
  838. */
  839. public function setOrder($index)
  840. {
  841. $this->_formOrder = (int) $index;
  842. return $this;
  843. }
  844. /**
  845. * Get form order
  846. *
  847. * @return int|null
  848. */
  849. public function getOrder()
  850. {
  851. return $this->_formOrder;
  852. }
  853. // Element interaction:
  854. /**
  855. * Add a new element
  856. *
  857. * $element may be either a string element type, or an object of type
  858. * Zend_Form_Element. If a string element type is provided, $name must be
  859. * provided, and $options may be optionally provided for configuring the
  860. * element.
  861. *
  862. * If a Zend_Form_Element is provided, $name may be optionally provided,
  863. * and any provided $options will be ignored.
  864. *
  865. * @param string|Zend_Form_Element $element
  866. * @param string $name
  867. * @param array|Zend_Config $options
  868. * @return Zend_Form
  869. */
  870. public function addElement($element, $name = null, $options = null)
  871. {
  872. if (is_string($element)) {
  873. if (null === $name) {
  874. require_once 'Zend/Form/Exception.php';
  875. throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
  876. }
  877. if (is_array($this->_elementDecorators)) {
  878. if (null === $options) {
  879. $options = array('decorators' => $this->_elementDecorators);
  880. } elseif ($options instanceof Zend_Config) {
  881. $options = $options->toArray();
  882. }
  883. if (is_array($options)
  884. && !array_key_exists('decorators', $options)
  885. ) {
  886. $options['decorators'] = $this->_elementDecorators;
  887. }
  888. }
  889. $this->_elements[$name] = $this->createElement($element, $name, $options);
  890. } elseif ($element instanceof Zend_Form_Element) {
  891. $prefixPaths = array();
  892. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  893. if (!empty($this->_elementPrefixPaths)) {
  894. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  895. }
  896. if (null === $name) {
  897. $name = $element->getName();
  898. }
  899. $this->_elements[$name] = $element;
  900. $this->_elements[$name]->addPrefixPaths($prefixPaths);
  901. }
  902. $this->_order[$name] = $this->_elements[$name]->getOrder();
  903. $this->_orderUpdated = true;
  904. $this->_setElementsBelongTo($name);
  905. return $this;
  906. }
  907. /**
  908. * Create an element
  909. *
  910. * Acts as a factory for creating elements. Elements created with this
  911. * method will not be attached to the form, but will contain element
  912. * settings as specified in the form object (including plugin loader
  913. * prefix paths, default decorators, etc.).
  914. *
  915. * @param string $type
  916. * @param string $name
  917. * @param array|Zend_Config $options
  918. * @return Zend_Form_Element
  919. */
  920. public function createElement($type, $name, $options = null)
  921. {
  922. if (!is_string($type)) {
  923. require_once 'Zend/Form/Exception.php';
  924. throw new Zend_Form_Exception('Element type must be a string indicating type');
  925. }
  926. if (!is_string($name)) {
  927. require_once 'Zend/Form/Exception.php';
  928. throw new Zend_Form_Exception('Element name must be a string');
  929. }
  930. $prefixPaths = array();
  931. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  932. if (!empty($this->_elementPrefixPaths)) {
  933. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  934. }
  935. if ($options instanceof Zend_Config) {
  936. $options = $options->toArray();
  937. }
  938. if ((null === $options) || !is_array($options)) {
  939. $options = array('prefixPath' => $prefixPaths);
  940. } elseif (is_array($options)) {
  941. if (array_key_exists('prefixPath', $options)) {
  942. $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
  943. } else {
  944. $options['prefixPath'] = $prefixPaths;
  945. }
  946. }
  947. $class = $this->getPluginLoader(self::ELEMENT)->load($type);
  948. $element = new $class($name, $options);
  949. return $element;
  950. }
  951. /**
  952. * Add multiple elements at once
  953. *
  954. * @param array $elements
  955. * @return Zend_Form
  956. */
  957. public function addElements(array $elements)
  958. {
  959. foreach ($elements as $key => $spec) {
  960. $name = null;
  961. if (!is_numeric($key)) {
  962. $name = $key;
  963. }
  964. if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
  965. $this->addElement($spec, $name);
  966. continue;
  967. }
  968. if (is_array($spec)) {
  969. $argc = count($spec);
  970. $options = array();
  971. if (isset($spec['type'])) {
  972. $type = $spec['type'];
  973. if (isset($spec['name'])) {
  974. $name = $spec['name'];
  975. }
  976. if (isset($spec['options'])) {
  977. $options = $spec['options'];
  978. }
  979. $this->addElement($type, $name, $options);
  980. } else {
  981. switch ($argc) {
  982. case 0:
  983. continue;
  984. case (1 <= $argc):
  985. $type = array_shift($spec);
  986. case (2 <= $argc):
  987. if (null === $name) {
  988. $name = array_shift($spec);
  989. } else {
  990. $options = array_shift($spec);
  991. }
  992. case (3 <= $argc):
  993. if (empty($options)) {
  994. $options = array_shift($spec);
  995. }
  996. default:
  997. $this->addElement($type, $name, $options);
  998. }
  999. }
  1000. }
  1001. }
  1002. return $this;
  1003. }
  1004. /**
  1005. * Set form elements (overwrites existing elements)
  1006. *
  1007. * @param array $elements
  1008. * @return Zend_Form
  1009. */
  1010. public function setElements(array $elements)
  1011. {
  1012. $this->clearElements();
  1013. return $this->addElements($elements);
  1014. }
  1015. /**
  1016. * Retrieve a single element
  1017. *
  1018. * @param string $name
  1019. * @return Zend_Form_Element|null
  1020. */
  1021. public function getElement($name)
  1022. {
  1023. if (array_key_exists($name, $this->_elements)) {
  1024. return $this->_elements[$name];
  1025. }
  1026. return null;
  1027. }
  1028. /**
  1029. * Retrieve all elements
  1030. *
  1031. * @return array
  1032. */
  1033. public function getElements()
  1034. {
  1035. return $this->_elements;
  1036. }
  1037. /**
  1038. * Remove element
  1039. *
  1040. * @param string $name
  1041. * @return boolean
  1042. */
  1043. public function removeElement($name)
  1044. {
  1045. $name = (string) $name;
  1046. if (isset($this->_elements[$name])) {
  1047. unset($this->_elements[$name]);
  1048. if (array_key_exists($name, $this->_order)) {
  1049. unset($this->_order[$name]);
  1050. $this->_orderUpdated = true;
  1051. } else {
  1052. foreach ($this->_displayGroups as $group) {
  1053. if (null !== $group->getElement($name)) {
  1054. $group->removeElement($name);
  1055. }
  1056. }
  1057. }
  1058. return true;
  1059. }
  1060. return false;
  1061. }
  1062. /**
  1063. * Remove all form elements
  1064. *
  1065. * @return Zend_Form
  1066. */
  1067. public function clearElements()
  1068. {
  1069. foreach (array_keys($this->_elements) as $key) {
  1070. if (array_key_exists($key, $this->_order)) {
  1071. unset($this->_order[$key]);
  1072. }
  1073. }
  1074. $this->_elements = array();
  1075. $this->_orderUpdated = true;
  1076. return $this;
  1077. }
  1078. /**
  1079. * Set default values for elements
  1080. *
  1081. * If an element's name is not specified as a key in the array, its value
  1082. * is set to null.
  1083. *
  1084. * @param array $defaults
  1085. * @return Zend_Form
  1086. */
  1087. public function setDefaults(array $defaults)
  1088. {
  1089. foreach ($this->getElements() as $name => $element) {
  1090. if (array_key_exists($name, $defaults)) {
  1091. $this->setDefault($name, $defaults[$name]);
  1092. }
  1093. }
  1094. foreach ($this->getSubForms() as $name => $form) {
  1095. if (array_key_exists($name, $defaults)) {
  1096. $form->setDefaults($defaults[$name]);
  1097. } else {
  1098. $form->setDefaults($defaults);
  1099. }
  1100. }
  1101. return $this;
  1102. }
  1103. /**
  1104. * Set default value for an element
  1105. *
  1106. * @param string $name
  1107. * @param mixed $value
  1108. * @return Zend_Form
  1109. */
  1110. public function setDefault($name, $value)
  1111. {
  1112. $name = (string) $name;
  1113. if ($element = $this->getElement($name)) {
  1114. $element->setValue($value);
  1115. } else {
  1116. if (is_scalar($value)) {
  1117. foreach ($this->getSubForms() as $subForm) {
  1118. $subForm->setDefault($name, $value);
  1119. }
  1120. } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
  1121. $subForm->setDefaults($value);
  1122. }
  1123. }
  1124. return $this;
  1125. }
  1126. /**
  1127. * Retrieve value for single element
  1128. *
  1129. * @param string $name
  1130. * @return mixed
  1131. */
  1132. public function getValue($name)
  1133. {
  1134. if ($element = $this->getElement($name)) {
  1135. return $element->getValue();
  1136. }
  1137. if ($subForm = $this->getSubForm($name)) {
  1138. return $subForm->getValues(true);
  1139. }
  1140. foreach ($this->getSubForms() as $subForm) {
  1141. if ($name == $subForm->getElementsBelongTo()) {
  1142. return $subForm->getValues(true);
  1143. }
  1144. }
  1145. return null;
  1146. }
  1147. /**
  1148. * Retrieve all form element values
  1149. *
  1150. * @param bool $suppressArrayNotation
  1151. * @return array
  1152. */
  1153. public function getValues($suppressArrayNotation = false)
  1154. {
  1155. $values = array();
  1156. foreach ($this->getElements() as $key => $element) {
  1157. if (!$element->getIgnore()) {
  1158. $values[$key] = $element->getValue();
  1159. }
  1160. }
  1161. foreach ($this->getSubForms() as $key => $subForm) {
  1162. $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
  1163. $values = array_merge($values, $fValues);
  1164. }
  1165. if (!$suppressArrayNotation && $this->isArray()) {
  1166. $values = $this->_attachToArray($values, $this->getElementsBelongTo());
  1167. }
  1168. return $values;
  1169. }
  1170. /**
  1171. * Get unfiltered element value
  1172. *
  1173. * @param string $name
  1174. * @return mixed
  1175. */
  1176. public function getUnfilteredValue($name)
  1177. {
  1178. if ($element = $this->getElement($name)) {
  1179. return $element->getUnfilteredValue();
  1180. }
  1181. return null;
  1182. }
  1183. /**
  1184. * Retrieve all unfiltered element values
  1185. *
  1186. * @return array
  1187. */
  1188. public function getUnfilteredValues()
  1189. {
  1190. $values = array();
  1191. foreach ($this->getElements() as $key => $element) {
  1192. $values[$key] = $element->getUnfilteredValue();
  1193. }
  1194. return $values;
  1195. }
  1196. /**
  1197. * Set all elements' filters
  1198. *
  1199. * @param array $filters
  1200. * @return Zend_Form
  1201. */
  1202. public function setElementFilters(array $filters)
  1203. {
  1204. foreach ($this->getElements() as $element) {
  1205. $element->setFilters($filters);
  1206. }
  1207. return $this;
  1208. }
  1209. /**
  1210. * Set name of array elements belong to
  1211. *
  1212. * @param string $array
  1213. * @return Zend_Form
  1214. */
  1215. public function setElementsBelongTo($array)
  1216. {
  1217. $origName = $this->getElementsBelongTo();
  1218. $name = $this->filterName($array, true);
  1219. if (empty($name)) {
  1220. $name = null;
  1221. }
  1222. $this->_elementsBelongTo = $name;
  1223. if (null === $name) {
  1224. $this->setIsArray(false);
  1225. if (null !== $origName) {
  1226. $this->_setElementsBelongTo();
  1227. }
  1228. } else {
  1229. $this->setIsArray(true);
  1230. $this->_setElementsBelongTo();
  1231. }
  1232. return $this;
  1233. }
  1234. /**
  1235. * Set array to which elements belong
  1236. *
  1237. * @param string $name Element name
  1238. * @return void
  1239. */
  1240. protected function _setElementsBelongTo($name = null)
  1241. {
  1242. $array = $this->getElementsBelongTo();
  1243. if (null === $array) {
  1244. return;
  1245. }
  1246. if (null === $name) {
  1247. foreach ($this->getElements() as $element) {
  1248. $element->setBelongsTo($array);
  1249. }
  1250. } else {
  1251. if (null !== ($element = $this->getElement($name))) {
  1252. $element->setBelongsTo($array);
  1253. }
  1254. }
  1255. }
  1256. /**
  1257. * Get name of array elements belong to
  1258. *
  1259. * @return string|null
  1260. */
  1261. public function getElementsBelongTo()
  1262. {
  1263. if ((null === $this->_elementsBelongTo) && $this->isArray()) {
  1264. $name = $this->getName();
  1265. if (!empty($name)) {
  1266. return $name;
  1267. }
  1268. }
  1269. return $this->_elementsBelongTo;
  1270. }
  1271. /**
  1272. * Set flag indicating elements belong to array
  1273. *
  1274. * @param bool $flag Value of flag
  1275. * @return Zend_Form
  1276. */
  1277. public function setIsArray($flag)
  1278. {
  1279. $this->_isArray = (bool) $flag;
  1280. return $this;
  1281. }
  1282. /**
  1283. * Get flag indicating if elements belong to an array
  1284. *
  1285. * @return bool
  1286. */
  1287. public function isArray()
  1288. {
  1289. return $this->_isArray;
  1290. }
  1291. // Element groups:
  1292. /**
  1293. * Add a form group/subform
  1294. *
  1295. * @param Zend_Form $form
  1296. * @param string $name
  1297. * @param int $order
  1298. * @return Zend_Form
  1299. */
  1300. public function addSubForm(Zend_Form $form, $name, $order = null)
  1301. {
  1302. $name = (string) $name;
  1303. foreach ($this->_loaders as $type => $loader) {
  1304. $loaderPaths = $loader->getPaths();
  1305. foreach ($loaderPaths as $prefix => $paths) {
  1306. foreach ($paths as $path) {
  1307. $form->addPrefixPath($prefix, $path, $type);
  1308. }
  1309. }
  1310. }
  1311. if (!empty($this->_elementPrefixPaths)) {
  1312. foreach ($this->_elementPrefixPaths as $spec) {
  1313. list($prefix, $path, $type) = array_values($spec);
  1314. $form->addElementPrefixPath($prefix, $path, $type);
  1315. }
  1316. }
  1317. if (!empty($this->_displayGroupPrefixPaths)) {
  1318. foreach ($this->_displayGroupPrefixPaths as $spec) {
  1319. list($prefix, $path) = array_values($spec);
  1320. $form->addDisplayGroupPrefixPath($prefix, $path);
  1321. }
  1322. }
  1323. if (null !== $order) {
  1324. $form->setOrder($order);
  1325. }
  1326. $form->setName($name);
  1327. $this->_subForms[$name] = $form;
  1328. $this->_order[$name] = $order;
  1329. $this->_orderUpdated = true;
  1330. return $this;
  1331. }
  1332. /**
  1333. * Add multiple form subForms/subforms at once
  1334. *
  1335. * @param array $subForms
  1336. * @return Zend_Form
  1337. */
  1338. public function addSubForms(array $subForms)
  1339. {
  1340. foreach ($subForms as $key => $spec) {
  1341. $name = null;
  1342. if (!is_numeric($key)) {
  1343. $name = $key;
  1344. }
  1345. if ($spec instanceof Zend_Form) {
  1346. $this->addSubForm($spec, $name);
  1347. continue;
  1348. }
  1349. if (is_array($spec)) {
  1350. $argc = count($spec);
  1351. $order = null;
  1352. switch ($argc) {
  1353. case 0:
  1354. continue;
  1355. case (1 <= $argc):
  1356. $subForm = array_shift($spec);
  1357. case (2 <= $argc):
  1358. $name = array_shift($spec);
  1359. case (3 <= $argc):
  1360. $order = array_shift($spec);
  1361. default:
  1362. $this->addSubForm($subForm, $name, $order);
  1363. }
  1364. }
  1365. }
  1366. return $this;
  1367. }
  1368. /**
  1369. * Set multiple form subForms/subforms (overwrites)
  1370. *
  1371. * @param array $subForms
  1372. * @return Zend_Form
  1373. */
  1374. public function setSubForms(array $subForms)
  1375. {
  1376. $this->clearSubForms();
  1377. return $this->addSubForms($subForms);
  1378. }
  1379. /**
  1380. * Retrieve a form subForm/subform
  1381. *
  1382. * @param string $name
  1383. * @return Zend_Form|null
  1384. */
  1385. public function getSubForm($name)
  1386. {
  1387. $name = (string) $name;
  1388. if (isset($this->_subForms[$name])) {
  1389. return $this->_subForms[$name];
  1390. }
  1391. return null;
  1392. }
  1393. /**
  1394. * Retrieve all form subForms/subforms
  1395. *
  1396. * @return array
  1397. */
  1398. public function getSubForms()
  1399. {
  1400. return $this->_subForms;
  1401. }
  1402. /**
  1403. * Remove form subForm/subform
  1404. *
  1405. * @param string $name
  1406. * @return boolean
  1407. */
  1408. public function removeSubForm($name)
  1409. {
  1410. $name = (string) $name;
  1411. if (array_key_exists($name, $this->_subForms)) {
  1412. unset($this->_subForms[$name]);
  1413. if (array_key_exists($name, $this->_order)) {
  1414. unset($this->_order[$name]);
  1415. $this->_orderUpdated = true;
  1416. }
  1417. return true;
  1418. }
  1419. return false;
  1420. }
  1421. /**
  1422. * Remove all form subForms/subforms
  1423. *
  1424. * @return Zend_Form
  1425. */
  1426. public function clearSubForms()
  1427. {
  1428. foreach (array_keys($this->_subForms) as $key) {
  1429. if (array_key_exists($key, $this->_order)) {
  1430. unset($this->_order[$key]);
  1431. }
  1432. }
  1433. $this->_subForms = array();
  1434. $this->_orderUpdated = true;
  1435. return $this;
  1436. }
  1437. // Display groups:
  1438. /**
  1439. * Set default display group class
  1440. *
  1441. * @param string $class
  1442. * @return Zend_Form
  1443. */
  1444. public function setDefaultDisplayGroupClass($class)
  1445. {
  1446. $this->_defaultDisplayGroupClass = (string) $class;
  1447. return $this;
  1448. }
  1449. /**
  1450. * Retrieve default display group class
  1451. *
  1452. * @return string
  1453. */
  1454. public function getDefaultDisplayGroupClass()
  1455. {
  1456. return $this->_defaultDisplayGroupClass;
  1457. }
  1458. /**
  1459. * Add a display group
  1460. *
  1461. * Groups named elements for display purposes.
  1462. *
  1463. * If a referenced element does not yet exist in the form, it is omitted.
  1464. *
  1465. * @param array $elements
  1466. * @param string $name
  1467. * @param array|Zend_Config $options
  1468. * @return Zend_Form
  1469. * @throws Zend_Form_Exception if no valid elements provided
  1470. */
  1471. public function addDisplayGroup(array $elements, $name, $options = null)
  1472. {
  1473. $group = array();
  1474. foreach ($elements as $element) {
  1475. if (isset($this->_elements[$element])) {
  1476. $add = $this->getElement($element);
  1477. if (null !== $add) {
  1478. unset($this->_order[$element]);
  1479. $group[] = $add;
  1480. }
  1481. }
  1482. }
  1483. if (empty($group)) {
  1484. require_once 'Zend/Form/Exception.php';
  1485. throw new Zend_Form_Exception('No valid elements specified for display group');
  1486. }
  1487. $name = (string) $name;
  1488. if (is_array($options)) {
  1489. $options['elements'] = $group;
  1490. } elseif ($options instanceof Zend_Config) {
  1491. $options = $options->toArray();
  1492. $options['elements'] = $group;
  1493. } else {
  1494. $options = array('elements' => $group);
  1495. }
  1496. if (isset($options['displayGroupClass'])) {
  1497. $class = $options['displayGroupClass'];
  1498. unset($options['displayGroupClass']);
  1499. } else {
  1500. $class = $this->getDefaultDisplayGroupClass();
  1501. }
  1502. if (!class_exists($class)) {
  1503. require_once 'Zend/Loader.php';
  1504. Zend_Loader::loadClass($class);
  1505. }
  1506. $this->_displayGroups[$name] = new $class(
  1507. $name,
  1508. $this->getPluginLoader(self::DECORATOR),
  1509. $options
  1510. );
  1511. if (!empty($this->_displayGroupPrefixPaths)) {
  1512. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1513. }
  1514. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1515. $this->_orderUpdated = true;
  1516. return $this;
  1517. }
  1518. /**
  1519. * Add a display group object (used with cloning)
  1520. *
  1521. * @param Zend_Form_DisplayGroup $group
  1522. * @param string|null $name
  1523. * @return Zend_Form
  1524. */
  1525. protected function _addDisplayGroupObject(Zend_Form_DisplayGroup $group, $name = null)
  1526. {
  1527. if (null === $name) {
  1528. $name = $group->getName();
  1529. if (empty($name)) {
  1530. require_once 'Zend/Form/Exception.php';
  1531. throw new Zend_Form_Exception('Invalid display group added; requires name');
  1532. }
  1533. }
  1534. $this->_displayGroups[$name] = $group;
  1535. if (!empty($this->_displayGroupPrefixPaths)) {
  1536. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1537. }
  1538. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1539. $this->_orderUpdated = true;
  1540. return $this;
  1541. }
  1542. /**
  1543. * Add multiple display groups at once
  1544. *
  1545. * @param array $groups
  1546. * @return Zend_Form
  1547. */
  1548. public function addDisplayGroups(array $groups)
  1549. {
  1550. foreach ($groups as $key => $spec) {
  1551. $name = null;
  1552. if (!is_numeric($key)) {
  1553. $name = $key;
  1554. }
  1555. if ($spec instanceof Zend_Form_DisplayGroup) {
  1556. $this->_addDisplayGroupObject($spec);
  1557. }
  1558. if (!is_array($spec) || empty($spec)) {
  1559. continue;
  1560. }
  1561. $argc = count($spec);
  1562. $options = array();
  1563. if (isset($spec['elements'])) {
  1564. $elements = $spec['elements'];
  1565. if (isset($spec['name'])) {
  1566. $name = $spec['name'];
  1567. }
  1568. if (isset($spec['options'])) {
  1569. $options = $spec['options'];
  1570. }
  1571. $this->addDisplayGroup($elements, $name, $options);
  1572. } else {
  1573. switch ($argc) {
  1574. case (1 <= $argc):
  1575. $elements = array_shift($spec);
  1576. if (!is_array($elements) && (null !== $name)) {
  1577. $elements = array_merge((array) $elements, $spec);
  1578. $this->addDisplayGroup($elements, $name);
  1579. break;
  1580. }
  1581. case (2 <= $argc):
  1582. if (null !== $name) {
  1583. $options = array_shift($spec);
  1584. $this->addDisplayGroup($elements, $name, $options);
  1585. break;
  1586. }
  1587. $name = array_shift($spec);
  1588. case (3 <= $argc):
  1589. $options = array_shift($spec);
  1590. default:
  1591. $this->addDisplayGroup($elements, $name, $options);
  1592. }
  1593. }
  1594. }
  1595. return $this;
  1596. }
  1597. /**
  1598. * Add multiple display groups (overwrites)
  1599. *
  1600. * @param array $groups
  1601. * @return Zend_Form
  1602. */
  1603. public function setDisplayGroups(array $groups)
  1604. {
  1605. return $this->clearDisplayGroups()
  1606. ->addDisplayGroups($groups);
  1607. }
  1608. /**
  1609. * Return a display group
  1610. *
  1611. * @param string $name
  1612. * @return Zend_Form_DisplayGroup|null
  1613. */
  1614. public function getDisplayGroup($name)
  1615. {
  1616. $name = (string) $name;
  1617. if (isset($this->_displayGroups[$name])) {
  1618. return $this->_displayGroups[$name];
  1619. }
  1620. return null;
  1621. }
  1622. /**
  1623. * Return all display groups
  1624. *
  1625. * @return array
  1626. */
  1627. public function getDisplayGroups()
  1628. {
  1629. return $this->_displayGroups;
  1630. }
  1631. /**
  1632. * Remove a display group by name
  1633. *
  1634. * @param string $name
  1635. * @return boolean
  1636. */
  1637. public function removeDisplayGroup($name)
  1638. {
  1639. $name = (string) $name;
  1640. if (array_key_exists($name, $this->_displayGroups)) {
  1641. foreach ($this->_displayGroups[$name] as $key => $element) {
  1642. if (array_key_exists($key, $this->_elements)) {
  1643. $this->_order[$key] = $element->getOrder();
  1644. $this->_orderUpdated = true;
  1645. }
  1646. }
  1647. unset($this->_displayGroups[$name]);
  1648. if (array_key_exists($name, $this->_order)) {
  1649. unset($this->_order[$name]);
  1650. $this->_orderUpdated = true;
  1651. }
  1652. return true;
  1653. }
  1654. return false;
  1655. }
  1656. /**
  1657. * Remove all display groups
  1658. *
  1659. * @return Zend_Form
  1660. */
  1661. public function clearDisplayGroups()
  1662. {
  1663. foreach ($this->_displayGroups as $key => $group) {
  1664. if (array_key_exists($key, $this->_order)) {
  1665. unset($this->_order[$key]);
  1666. }
  1667. foreach ($group as $name => $element) {
  1668. if (isset($this->_elements[$name])) {
  1669. $this->_order[$name] = $element->getOrder();
  1670. }
  1671. $this->_order[$name] = $element->getOrder();
  1672. }
  1673. }
  1674. $this->_displayGroups = array();
  1675. $this->_orderUpdated = true;
  1676. return $this;
  1677. }
  1678. // Processing
  1679. /**
  1680. * Populate form
  1681. *
  1682. * Proxies to {@link setDefaults()}
  1683. *
  1684. * @param array $values
  1685. * @return Zend_Form
  1686. */
  1687. public function populate(array $values)
  1688. {
  1689. return $this->setDefaults($values);
  1690. }
  1691. /**
  1692. * Determine array key name from given value
  1693. *
  1694. * Given a value such as foo[bar][baz], returns the last element (in this case, 'baz').
  1695. *
  1696. * @param string $value
  1697. * @return string
  1698. */
  1699. protected function _getArrayName($value)
  1700. {
  1701. if (empty($value) || !is_string($value)) {
  1702. return $value;
  1703. }
  1704. if (!strstr($value, '[')) {
  1705. return $value;
  1706. }
  1707. $endPos = strlen($value) - 1;
  1708. if (']' != $value[$endPos]) {
  1709. return $value;
  1710. }
  1711. $start = strrpos($value, '[') + 1;
  1712. $name = substr($value, $start, $endPos - $start);
  1713. return $name;
  1714. }
  1715. /**
  1716. * Extract the value by walking the array using given array path.
  1717. *
  1718. * Given an array path such as foo[bar][baz], returns the value of the last
  1719. * element (in this case, 'baz').
  1720. *
  1721. * @param array $value Array to walk
  1722. * @param string $arrayPath Array notation path of the part to extract
  1723. * @return string
  1724. */
  1725. protected function _dissolveArrayValue($value, $arrayPath)
  1726. {
  1727. // As long as we have more levels
  1728. while ($arrayPos = strpos($arrayPath, '[')) {
  1729. // Get the next key in the path
  1730. $arrayKey = trim(substr($arrayPath, 0, $arrayPos), ']');
  1731. // Set the potentially final value or the next search point in the array
  1732. if (isset($value[$arrayKey])) {
  1733. $value = $value[$arrayKey];
  1734. }
  1735. // Set the next search point in the path
  1736. $arrayPath = trim(substr($arrayPath, $arrayPos + 1), ']');
  1737. }
  1738. if (isset($value[$arrayPath])) {
  1739. $value = $value[$arrayPath];
  1740. }
  1741. return $value;
  1742. }
  1743. /**
  1744. * Converts given arrayPath to an array and attaches given value at the end of it.
  1745. *
  1746. * @param mixed $value The value to attach
  1747. * @param string $arrayPath Given array path to convert and attach to.
  1748. * @return array
  1749. */
  1750. protected function _attachToArray($value, $arrayPath)
  1751. {
  1752. // As long as we have more levels
  1753. while ($arrayPos = strrpos($arrayPath, '[')) {
  1754. // Get the next key in the path
  1755. $arrayKey = trim(substr($arrayPath, $arrayPos + 1), ']');
  1756. // Attach
  1757. $value = array($arrayKey => $value);
  1758. // Set the next search point in the path
  1759. $arrayPath = trim(substr($arrayPath, 0, $arrayPos), ']');
  1760. }
  1761. $value = array($arrayPath => $value);
  1762. return $value;
  1763. }
  1764. /**
  1765. * Validate the form
  1766. *
  1767. * @param array $data
  1768. * @return boolean
  1769. */
  1770. public function isValid($data)
  1771. {
  1772. if (!is_array($data)) {
  1773. require_once 'Zend/Form/Exception.php';
  1774. throw new Zend_Form_Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array');
  1775. }
  1776. $translator = $this->getTranslator();
  1777. $valid = true;
  1778. if ($this->isArray()) {
  1779. $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
  1780. }
  1781. foreach ($this->getElements() as $key => $element) {
  1782. $element->setTranslator($translator);
  1783. if (!isset($data[$key])) {
  1784. $valid = $element->isValid(null, $data) && $valid;
  1785. } else {
  1786. $valid = $element->isValid($data[$key], $data) && $valid;
  1787. }
  1788. }
  1789. foreach ($this->getSubForms() as $key => $form) {
  1790. $form->setTranslator($translator);
  1791. if (isset($data[$key])) {
  1792. $valid = $form->isValid($data[$key]) && $valid;
  1793. } else {
  1794. $valid = $form->isValid($data) && $valid;
  1795. }
  1796. }
  1797. $this->_errorsExist = !$valid;
  1798. return $valid;
  1799. }
  1800. /**
  1801. * Validate a partial form
  1802. *
  1803. * Does not check for required flags.
  1804. *
  1805. * @param array $data
  1806. * @return boolean
  1807. */
  1808. public function isValidPartial(array $data)
  1809. {
  1810. if ($this->isArray()) {
  1811. $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
  1812. }
  1813. $translator = $this->getTranslator();
  1814. $valid = true;
  1815. $validatedSubForms = array();
  1816. foreach ($data as $key => $value) {
  1817. if (null !== ($element = $this->getElement($key))) {
  1818. if (null !== $translator) {
  1819. $element->setTranslator($translator);
  1820. }
  1821. $valid = $element->isValid($value, $data) && $valid;
  1822. } elseif (null !== ($subForm = $this->getSubForm($key))) {
  1823. if (null !== $translator) {
  1824. $subForm->setTranslator($translator);
  1825. }
  1826. $valid = $subForm->isValidPartial($data[$key]) && $valid;
  1827. $validatedSubForms[] = $key;
  1828. }
  1829. }
  1830. foreach ($this->getSubForms() as $key => $subForm) {
  1831. if (!in_array($key, $validatedSubForms)) {
  1832. if (null !== $translator) {
  1833. $subForm->setTranslator($translator);
  1834. }
  1835. $valid = $subForm->isValidPartial($data) && $valid;
  1836. }
  1837. }
  1838. $this->_errorsExist = !$valid;
  1839. return $valid;
  1840. }
  1841. /**
  1842. * Process submitted AJAX data
  1843. *
  1844. * Checks if provided $data is valid, via {@link isValidPartial()}. If so,
  1845. * it returns JSON-encoded boolean true. If not, it returns JSON-encoded
  1846. * error messages (as returned by {@link getMessages()}).
  1847. *
  1848. * @param array $data
  1849. * @return string JSON-encoded boolean true or error messages
  1850. */
  1851. public function processAjax(array $data)
  1852. {
  1853. require_once 'Zend/Json.php';
  1854. if ($this->isValidPartial($data)) {
  1855. return Zend_Json::encode(true);
  1856. }
  1857. $messages = $this->getMessages();
  1858. return Zend_Json::encode($messages);
  1859. }
  1860. /**
  1861. * Add a custom error message to return in the event of failed validation
  1862. *
  1863. * @param string $message
  1864. * @return Zend_Form
  1865. */
  1866. public function addErrorMessage($message)
  1867. {
  1868. $this->_errorMessages[] = (string) $message;
  1869. return $this;
  1870. }
  1871. /**
  1872. * Add multiple custom error messages to return in the event of failed validation
  1873. *
  1874. * @param array $messages
  1875. * @return Zend_Form
  1876. */
  1877. public function addErrorMessages(array $messages)
  1878. {
  1879. foreach ($messages as $message) {
  1880. $this->addErrorMessage($message);
  1881. }
  1882. return $this;
  1883. }
  1884. /**
  1885. * Same as addErrorMessages(), but clears custom error message stack first
  1886. *
  1887. * @param array $messages
  1888. * @return Zend_Form
  1889. */
  1890. public function setErrorMessages(array $messages)
  1891. {
  1892. $this->clearErrorMessages();
  1893. return $this->addErrorMessages($messages);
  1894. }
  1895. /**
  1896. * Retrieve custom error messages
  1897. *
  1898. * @return array
  1899. */
  1900. public function getErrorMessages()
  1901. {
  1902. return $this->_errorMessages;
  1903. }
  1904. /**
  1905. * Clear custom error messages stack
  1906. *
  1907. * @return Zend_Form
  1908. */
  1909. public function clearErrorMessages()
  1910. {
  1911. $this->_errorMessages = array();
  1912. return $this;
  1913. }
  1914. /**
  1915. * Mark the element as being in a failed validation state
  1916. *
  1917. * @return Zend_Form
  1918. */
  1919. public function markAsError()
  1920. {
  1921. $this->_errorsExist = true;
  1922. return $this;
  1923. }
  1924. /**
  1925. * Add an error message and mark element as failed validation
  1926. *
  1927. * @param string $message
  1928. * @return Zend_Form
  1929. */
  1930. public function addError($message)
  1931. {
  1932. $this->addErrorMessage($message);
  1933. $this->markAsError();
  1934. return $this;
  1935. }
  1936. /**
  1937. * Add multiple error messages and flag element as failed validation
  1938. *
  1939. * @param array $messages
  1940. * @return Zend_Form
  1941. */
  1942. public function addErrors(array $messages)
  1943. {
  1944. foreach ($messages as $message) {
  1945. $this->addError($message);
  1946. }
  1947. return $this;
  1948. }
  1949. /**
  1950. * Overwrite any previously set error messages and flag as failed validation
  1951. *
  1952. * @param array $messages
  1953. * @return Zend_Form
  1954. */
  1955. public function setErrors(array $messages)
  1956. {
  1957. $this->clearErrorMessages();
  1958. return $this->addErrors($messages);
  1959. }
  1960. public function persistData()
  1961. {
  1962. }
  1963. /**
  1964. * Are there errors in the form?
  1965. *
  1966. * @return bool
  1967. */
  1968. public function isErrors()
  1969. {
  1970. return $this->_errorsExist;
  1971. }
  1972. /**
  1973. * Get error codes for all elements failing validation
  1974. *
  1975. * @param string $name
  1976. * @return array
  1977. */
  1978. public function getErrors($name = null)
  1979. {
  1980. $errors = array();
  1981. if ((null !== $name) && isset($this->_elements[$name])) {
  1982. $errors = $this->getElement($name)->getErrors();
  1983. } elseif ((null !== $name) && isset($this->_subForms[$name])) {
  1984. $errors = $this->getSubForm($name)->getErrors();
  1985. } else {
  1986. foreach ($this->_elements as $key => $element) {
  1987. $errors[$key] = $element->getErrors();
  1988. }
  1989. foreach ($this->getSubForms() as $key => $subForm) {
  1990. $fErrors = $this->_attachToArray($subForm->getErrors(), $subForm->getElementsBelongTo());
  1991. $errors = array_merge($errors, $fErrors);
  1992. }
  1993. }
  1994. return $errors;
  1995. }
  1996. /**
  1997. * Retrieve error messages from elements failing validations
  1998. *
  1999. * @param string $name
  2000. * @param bool $suppressArrayNotation
  2001. * @return array
  2002. */
  2003. public function getMessages($name = null, $suppressArrayNotation = false)
  2004. {
  2005. if ((null !== $name) && isset($this->_elements[$name])) {
  2006. return $this->getElement($name)->getMessages();
  2007. }
  2008. if ((null !== $name) && isset($this->_subForms[$name])) {
  2009. return $this->getSubForm($name)->getMessages(null, true);
  2010. }
  2011. $arrayKeys = array();
  2012. foreach ($this->getSubForms() as $key => $subForm) {
  2013. $array = $this->_getArrayName($subForm->getElementsBelongTo());
  2014. if (!empty($array)) {
  2015. if ($name == $array) {
  2016. return $subForm->getMessages(null, true);
  2017. }
  2018. $arrayKeys[$key] = $subForm->getElementsBelongTo();
  2019. }
  2020. }
  2021. $customMessages = $this->_getErrorMessages();
  2022. if ($this->isErrors() && !empty($customMessages)) {
  2023. return $customMessages;
  2024. }
  2025. $messages = array();
  2026. foreach ($this->getElements() as $name => $element) {
  2027. $eMessages = $element->getMessages();
  2028. if (!empty($eMessages)) {
  2029. $messages[$name] = $eMessages;
  2030. }
  2031. }
  2032. foreach ($this->getSubForms() as $key => $subForm) {
  2033. $fMessages = $subForm->getMessages(null, true);
  2034. if (!empty($fMessages)) {
  2035. if (array_key_exists($key, $arrayKeys)) {
  2036. $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
  2037. $messages = array_merge($messages, $fMessages);
  2038. } else {
  2039. $messages[$key] = $fMessages;
  2040. }
  2041. }
  2042. }
  2043. if (!$suppressArrayNotation && $this->isArray()) {
  2044. $messages = $this->_attachToArray($messages, $this->getElementsBelongTo());
  2045. }
  2046. return $messages;
  2047. }
  2048. // Rendering
  2049. /**
  2050. * Set view object
  2051. *
  2052. * @param Zend_View_Interface $view
  2053. * @return Zend_Form
  2054. */
  2055. public function setView(Zend_View_Interface $view = null)
  2056. {
  2057. $this->_view = $view;
  2058. return $this;
  2059. }
  2060. /**
  2061. * Retrieve view object
  2062. *
  2063. * If none registered, attempts to pull from ViewRenderer.
  2064. *
  2065. * @return Zend_View_Interface|null
  2066. */
  2067. public function getView()
  2068. {
  2069. if (null === $this->_view) {
  2070. require_once 'Zend/Controller/Action/HelperBroker.php';
  2071. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2072. $this->setView($viewRenderer->view);
  2073. }
  2074. return $this->_view;
  2075. }
  2076. /**
  2077. * Instantiate a decorator based on class name or class name fragment
  2078. *
  2079. * @param string $name
  2080. * @param null|array $options
  2081. * @return Zend_Form_Decorator_Interface
  2082. */
  2083. protected function _getDecorator($name, $options)
  2084. {
  2085. $class = $this->getPluginLoader(self::DECORATOR)->load($name);
  2086. if (null === $options) {
  2087. $decorator = new $class;
  2088. } else {
  2089. $decorator = new $class($options);
  2090. }
  2091. return $decorator;
  2092. }
  2093. /**
  2094. * Add a decorator for rendering the element
  2095. *
  2096. * @param string|Zend_Form_Decorator_Interface $decorator
  2097. * @param array|Zend_Config $options Options with which to initialize decorator
  2098. * @return Zend_Form
  2099. */
  2100. public function addDecorator($decorator, $options = null)
  2101. {
  2102. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  2103. $name = get_class($decorator);
  2104. } elseif (is_string($decorator)) {
  2105. $name = $decorator;
  2106. $decorator = array(
  2107. 'decorator' => $name,
  2108. 'options' => $options,
  2109. );
  2110. } elseif (is_array($decorator)) {
  2111. foreach ($decorator as $name => $spec) {
  2112. break;
  2113. }
  2114. if (is_numeric($name)) {
  2115. require_once 'Zend/Form/Exception.php';
  2116. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  2117. }
  2118. if (is_string($spec)) {
  2119. $decorator = array(
  2120. 'decorator' => $spec,
  2121. 'options' => $options,
  2122. );
  2123. } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
  2124. $decorator = $spec;
  2125. }
  2126. } else {
  2127. require_once 'Zend/Form/Exception.php';
  2128. throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
  2129. }
  2130. $this->_decorators[$name] = $decorator;
  2131. return $this;
  2132. }
  2133. /**
  2134. * Add many decorators at once
  2135. *
  2136. * @param array $decorators
  2137. * @return Zend_Form
  2138. */
  2139. public function addDecorators(array $decorators)
  2140. {
  2141. foreach ($decorators as $decoratorInfo) {
  2142. if (is_string($decoratorInfo)) {
  2143. $this->addDecorator($decoratorInfo);
  2144. } elseif ($decoratorInfo instanceof Zend_Form_Decorator_Interface) {
  2145. $this->addDecorator($decoratorInfo);
  2146. } elseif (is_array($decoratorInfo)) {
  2147. $argc = count($decoratorInfo);
  2148. $options = array();
  2149. if (isset($decoratorInfo['decorator'])) {
  2150. $decorator = $decoratorInfo['decorator'];
  2151. if (isset($decoratorInfo['options'])) {
  2152. $options = $decoratorInfo['options'];
  2153. }
  2154. $this->addDecorator($decorator, $options);
  2155. } else {
  2156. switch (true) {
  2157. case (0 == $argc):
  2158. break;
  2159. case (1 <= $argc):
  2160. $decorator = array_shift($decoratorInfo);
  2161. case (2 <= $argc):
  2162. $options = array_shift($decoratorInfo);
  2163. default:
  2164. $this->addDecorator($decorator, $options);
  2165. break;
  2166. }
  2167. }
  2168. } else {
  2169. require_once 'Zend/Form/Exception.php';
  2170. throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
  2171. }
  2172. }
  2173. return $this;
  2174. }
  2175. /**
  2176. * Overwrite all decorators
  2177. *
  2178. * @param array $decorators
  2179. * @return Zend_Form
  2180. */
  2181. public function setDecorators(array $decorators)
  2182. {
  2183. $this->clearDecorators();
  2184. return $this->addDecorators($decorators);
  2185. }
  2186. /**
  2187. * Retrieve a registered decorator
  2188. *
  2189. * @param string $name
  2190. * @return false|Zend_Form_Decorator_Abstract
  2191. */
  2192. public function getDecorator($name)
  2193. {
  2194. if (!isset($this->_decorators[$name])) {
  2195. $len = strlen($name);
  2196. foreach ($this->_decorators as $localName => $decorator) {
  2197. if ($len > strlen($localName)) {
  2198. continue;
  2199. }
  2200. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  2201. if (is_array($decorator)) {
  2202. return $this->_loadDecorator($decorator, $localName);
  2203. }
  2204. return $decorator;
  2205. }
  2206. }
  2207. return false;
  2208. }
  2209. if (is_array($this->_decorators[$name])) {
  2210. return $this->_loadDecorator($this->_decorators[$name], $name);
  2211. }
  2212. return $this->_decorators[$name];
  2213. }
  2214. /**
  2215. * Retrieve all decorators
  2216. *
  2217. * @return array
  2218. */
  2219. public function getDecorators()
  2220. {
  2221. foreach ($this->_decorators as $key => $value) {
  2222. if (is_array($value)) {
  2223. $this->_loadDecorator($value, $key);
  2224. }
  2225. }
  2226. return $this->_decorators;
  2227. }
  2228. /**
  2229. * Remove a single decorator
  2230. *
  2231. * @param string $name
  2232. * @return bool
  2233. */
  2234. public function removeDecorator($name)
  2235. {
  2236. $decorator = $this->getDecorator($name);
  2237. if ($decorator) {
  2238. if (array_key_exists($name, $this->_decorators)) {
  2239. unset($this->_decorators[$name]);
  2240. } else {
  2241. $class = get_class($decorator);
  2242. if (!array_key_exists($class, $this->_decorators)) {
  2243. return false;
  2244. }
  2245. unset($this->_decorators[$class]);
  2246. }
  2247. return true;
  2248. }
  2249. return false;
  2250. }
  2251. /**
  2252. * Clear all decorators
  2253. *
  2254. * @return Zend_Form
  2255. */
  2256. public function clearDecorators()
  2257. {
  2258. $this->_decorators = array();
  2259. return $this;
  2260. }
  2261. /**
  2262. * Set all element decorators as specified
  2263. *
  2264. * @param array $decorators
  2265. * @param array|null $elements Specific elements to decorate or exclude from decoration
  2266. * @param bool $include Whether $elements is an inclusion or exclusion list
  2267. * @return Zend_Form
  2268. */
  2269. public function setElementDecorators(array $decorators, array $elements = null, $include = true)
  2270. {
  2271. if (is_array($elements)) {
  2272. if ($include) {
  2273. $elementObjs = array();
  2274. foreach ($elements as $name) {
  2275. if (null !== ($element = $this->getElement($name))) {
  2276. $elementObjs[] = $element;
  2277. }
  2278. }
  2279. } else {
  2280. $elementObjs = $this->getElements();
  2281. foreach ($elements as $name) {
  2282. if (array_key_exists($name, $elementObjs)) {
  2283. unset($elementObjs[$name]);
  2284. }
  2285. }
  2286. }
  2287. } else {
  2288. $elementObjs = $this->getElements();
  2289. }
  2290. foreach ($elementObjs as $element) {
  2291. $element->setDecorators($decorators);
  2292. }
  2293. $this->_elementDecorators = $decorators;
  2294. return $this;
  2295. }
  2296. /**
  2297. * Set all display group decorators as specified
  2298. *
  2299. * @param array $decorators
  2300. * @return Zend_Form
  2301. */
  2302. public function setDisplayGroupDecorators(array $decorators)
  2303. {
  2304. foreach ($this->getDisplayGroups() as $group) {
  2305. $group->setDecorators($decorators);
  2306. }
  2307. return $this;
  2308. }
  2309. /**
  2310. * Set all subform decorators as specified
  2311. *
  2312. * @param array $decorators
  2313. * @return Zend_Form
  2314. */
  2315. public function setSubFormDecorators(array $decorators)
  2316. {
  2317. foreach ($this->getSubForms() as $form) {
  2318. $form->setDecorators($decorators);
  2319. }
  2320. return $this;
  2321. }
  2322. /**
  2323. * Render form
  2324. *
  2325. * @param Zend_View_Interface $view
  2326. * @return string
  2327. */
  2328. public function render(Zend_View_Interface $view = null)
  2329. {
  2330. if (null !== $view) {
  2331. $this->setView($view);
  2332. }
  2333. $content = '';
  2334. foreach ($this->getDecorators() as $decorator) {
  2335. $decorator->setElement($this);
  2336. $content = $decorator->render($content);
  2337. }
  2338. return $content;
  2339. }
  2340. /**
  2341. * Serialize as string
  2342. *
  2343. * Proxies to {@link render()}.
  2344. *
  2345. * @return string
  2346. */
  2347. public function __toString()
  2348. {
  2349. try {
  2350. $return = $this->render();
  2351. return $return;
  2352. } catch (Exception $e) {
  2353. $message = "Exception caught by form: " . $e->getMessage()
  2354. . "\nStack Trace:\n" . $e->getTraceAsString();
  2355. trigger_error($message, E_USER_WARNING);
  2356. return '';
  2357. }
  2358. }
  2359. // Localization:
  2360. /**
  2361. * Set translator object
  2362. *
  2363. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2364. * @return Zend_Form
  2365. */
  2366. public function setTranslator($translator = null)
  2367. {
  2368. if (null === $translator) {
  2369. $this->_translator = null;
  2370. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2371. $this->_translator = $translator;
  2372. } elseif ($translator instanceof Zend_Translate) {
  2373. $this->_translator = $translator->getAdapter();
  2374. } else {
  2375. require_once 'Zend/Form/Exception.php';
  2376. throw new Zend_Form_Exception('Invalid translator specified');
  2377. }
  2378. return $this;
  2379. }
  2380. /**
  2381. * Set global default translator object
  2382. *
  2383. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2384. * @return void
  2385. */
  2386. public static function setDefaultTranslator($translator = null)
  2387. {
  2388. if (null === $translator) {
  2389. self::$_translatorDefault = null;
  2390. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2391. self::$_translatorDefault = $translator;
  2392. } elseif ($translator instanceof Zend_Translate) {
  2393. self::$_translatorDefault = $translator->getAdapter();
  2394. } else {
  2395. require_once 'Zend/Form/Exception.php';
  2396. throw new Zend_Form_Exception('Invalid translator specified');
  2397. }
  2398. }
  2399. /**
  2400. * Retrieve translator object
  2401. *
  2402. * @return Zend_Translate|null
  2403. */
  2404. public function getTranslator()
  2405. {
  2406. if ($this->translatorIsDisabled()) {
  2407. return null;
  2408. }
  2409. if (null === $this->_translator) {
  2410. return self::getDefaultTranslator();
  2411. }
  2412. return $this->_translator;
  2413. }
  2414. /**
  2415. * Get global default translator object
  2416. *
  2417. * @return null|Zend_Translate
  2418. */
  2419. public static function getDefaultTranslator()
  2420. {
  2421. if (null === self::$_translatorDefault) {
  2422. require_once 'Zend/Registry.php';
  2423. if (Zend_Registry::isRegistered('Zend_Translate')) {
  2424. $translator = Zend_Registry::get('Zend_Translate');
  2425. if ($translator instanceof Zend_Translate_Adapter) {
  2426. return $translator;
  2427. } elseif ($translator instanceof Zend_Translate) {
  2428. return $translator->getAdapter();
  2429. }
  2430. }
  2431. }
  2432. return self::$_translatorDefault;
  2433. }
  2434. /**
  2435. * Indicate whether or not translation should be disabled
  2436. *
  2437. * @param bool $flag
  2438. * @return Zend_Form
  2439. */
  2440. public function setDisableTranslator($flag)
  2441. {
  2442. $this->_translatorDisabled = (bool) $flag;
  2443. return $this;
  2444. }
  2445. /**
  2446. * Is translation disabled?
  2447. *
  2448. * @return bool
  2449. */
  2450. public function translatorIsDisabled()
  2451. {
  2452. return $this->_translatorDisabled;
  2453. }
  2454. /**
  2455. * Overloading: access to elements, form groups, and display groups
  2456. *
  2457. * @param string $name
  2458. * @return Zend_Form_Element|Zend_Form|null
  2459. */
  2460. public function __get($name)
  2461. {
  2462. if (isset($this->_elements[$name])) {
  2463. return $this->_elements[$name];
  2464. } elseif (isset($this->_subForms[$name])) {
  2465. return $this->_subForms[$name];
  2466. } elseif (isset($this->_displayGroups[$name])) {
  2467. return $this->_displayGroups[$name];
  2468. }
  2469. return null;
  2470. }
  2471. /**
  2472. * Overloading: access to elements, form groups, and display groups
  2473. *
  2474. * @param string $name
  2475. * @param Zend_Form_Element|Zend_Form $value
  2476. * @return void
  2477. * @throws Zend_Form_Exception for invalid $value
  2478. */
  2479. public function __set($name, $value)
  2480. {
  2481. if ($value instanceof Zend_Form_Element) {
  2482. $this->addElement($value, $name);
  2483. return;
  2484. } elseif ($value instanceof Zend_Form) {
  2485. $this->addSubForm($value, $name);
  2486. return;
  2487. } elseif (is_array($value)) {
  2488. $this->addDisplayGroup($value, $name);
  2489. return;
  2490. }
  2491. require_once 'Zend/Form/Exception.php';
  2492. if (is_object($value)) {
  2493. $type = get_class($value);
  2494. } else {
  2495. $type = gettype($value);
  2496. }
  2497. throw new Zend_Form_Exception('Only form elements and groups may be overloaded; variable of type "' . $type . '" provided');
  2498. }
  2499. /**
  2500. * Overloading: access to elements, form groups, and display groups
  2501. *
  2502. * @param string $name
  2503. * @return boolean
  2504. */
  2505. public function __isset($name)
  2506. {
  2507. if (isset($this->_elements[$name])
  2508. || isset($this->_subForms[$name])
  2509. || isset($this->_displayGroups[$name]))
  2510. {
  2511. return true;
  2512. }
  2513. return false;
  2514. }
  2515. /**
  2516. * Overloading: access to elements, form groups, and display groups
  2517. *
  2518. * @param string $name
  2519. * @return void
  2520. */
  2521. public function __unset($name)
  2522. {
  2523. if (isset($this->_elements[$name])) {
  2524. unset($this->_elements[$name]);
  2525. } elseif (isset($this->_subForms[$name])) {
  2526. unset($this->_subForms[$name]);
  2527. } elseif (isset($this->_displayGroups[$name])) {
  2528. unset($this->_displayGroups[$name]);
  2529. }
  2530. }
  2531. /**
  2532. * Overloading: allow rendering specific decorators
  2533. *
  2534. * Call renderDecoratorName() to render a specific decorator.
  2535. *
  2536. * @param string $method
  2537. * @param array $args
  2538. * @return string
  2539. * @throws Zend_Form_Exception for invalid decorator or invalid method call
  2540. */
  2541. public function __call($method, $args)
  2542. {
  2543. if ('render' == substr($method, 0, 6)) {
  2544. $decoratorName = substr($method, 6);
  2545. if (false !== ($decorator = $this->getDecorator($decoratorName))) {
  2546. $decorator->setElement($this);
  2547. $seed = '';
  2548. if (0 < count($args)) {
  2549. $seed = array_shift($args);
  2550. }
  2551. return $decorator->render($seed);
  2552. }
  2553. require_once 'Zend/Form/Exception.php';
  2554. throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
  2555. }
  2556. require_once 'Zend/Form/Exception.php';
  2557. throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
  2558. }
  2559. // Interfaces: Iterator, Countable
  2560. /**
  2561. * Current element/subform/display group
  2562. *
  2563. * @return Zend_Form_Element|Zend_Form_DisplayGroup|Zend_Form
  2564. */
  2565. public function current()
  2566. {
  2567. $this->_sort();
  2568. current($this->_order);
  2569. $key = key($this->_order);
  2570. if (isset($this->_elements[$key])) {
  2571. return $this->getElement($key);
  2572. } elseif (isset($this->_subForms[$key])) {
  2573. return $this->getSubForm($key);
  2574. } elseif (isset($this->_displayGroups[$key])) {
  2575. return $this->getDisplayGroup($key);
  2576. } else {
  2577. require_once 'Zend/Form/Exception.php';
  2578. throw new Zend_Form_Exception(sprintf('Corruption detected in form; invalid key ("%s") found in internal iterator', (string) $key));
  2579. }
  2580. }
  2581. /**
  2582. * Current element/subform/display group name
  2583. *
  2584. * @return string
  2585. */
  2586. public function key()
  2587. {
  2588. $this->_sort();
  2589. return key($this->_order);
  2590. }
  2591. /**
  2592. * Move pointer to next element/subform/display group
  2593. *
  2594. * @return void
  2595. */
  2596. public function next()
  2597. {
  2598. $this->_sort();
  2599. next($this->_order);
  2600. }
  2601. /**
  2602. * Move pointer to beginning of element/subform/display group loop
  2603. *
  2604. * @return void
  2605. */
  2606. public function rewind()
  2607. {
  2608. $this->_sort();
  2609. reset($this->_order);
  2610. }
  2611. /**
  2612. * Determine if current element/subform/display group is valid
  2613. *
  2614. * @return bool
  2615. */
  2616. public function valid()
  2617. {
  2618. $this->_sort();
  2619. return (current($this->_order) !== false);
  2620. }
  2621. /**
  2622. * Count of elements/subforms that are iterable
  2623. *
  2624. * @return int
  2625. */
  2626. public function count()
  2627. {
  2628. return count($this->_order);
  2629. }
  2630. /**
  2631. * Set flag to disable loading default decorators
  2632. *
  2633. * @param bool $flag
  2634. * @return Zend_Form
  2635. */
  2636. public function setDisableLoadDefaultDecorators($flag)
  2637. {
  2638. $this->_disableLoadDefaultDecorators = (bool) $flag;
  2639. return $this;
  2640. }
  2641. /**
  2642. * Should we load the default decorators?
  2643. *
  2644. * @return bool
  2645. */
  2646. public function loadDefaultDecoratorsIsDisabled()
  2647. {
  2648. return $this->_disableLoadDefaultDecorators;
  2649. }
  2650. /**
  2651. * Load the default decorators
  2652. *
  2653. * @return void
  2654. */
  2655. public function loadDefaultDecorators()
  2656. {
  2657. if ($this->loadDefaultDecoratorsIsDisabled()) {
  2658. return;
  2659. }
  2660. $decorators = $this->getDecorators();
  2661. if (empty($decorators)) {
  2662. $this->addDecorator('FormElements')
  2663. ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
  2664. ->addDecorator('Form');
  2665. }
  2666. }
  2667. /**
  2668. * Sort items according to their order
  2669. *
  2670. * @return void
  2671. */
  2672. protected function _sort()
  2673. {
  2674. if ($this->_orderUpdated) {
  2675. $items = array();
  2676. $index = 0;
  2677. foreach ($this->_order as $key => $order) {
  2678. if (null === $order) {
  2679. if (null === ($order = $this->{$key}->getOrder())) {
  2680. while (array_search($index, $this->_order, true)) {
  2681. ++$index;
  2682. }
  2683. $items[$index] = $key;
  2684. ++$index;
  2685. } else {
  2686. $items[$order] = $key;
  2687. }
  2688. } else {
  2689. $items[$order] = $key;
  2690. }
  2691. }
  2692. $items = array_flip($items);
  2693. asort($items);
  2694. $this->_order = $items;
  2695. $this->_orderUpdated = false;
  2696. }
  2697. }
  2698. /**
  2699. * Lazy-load a decorator
  2700. *
  2701. * @param array $decorator Decorator type and options
  2702. * @param mixed $name Decorator name or alias
  2703. * @return Zend_Form_Decorator_Interface
  2704. */
  2705. protected function _loadDecorator(array $decorator, $name)
  2706. {
  2707. $sameName = false;
  2708. if ($name == $decorator['decorator']) {
  2709. $sameName = true;
  2710. }
  2711. $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
  2712. if ($sameName) {
  2713. $newName = get_class($instance);
  2714. $decoratorNames = array_keys($this->_decorators);
  2715. $order = array_flip($decoratorNames);
  2716. $order[$newName] = $order[$name];
  2717. $decoratorsExchange = array();
  2718. unset($order[$name]);
  2719. asort($order);
  2720. foreach ($order as $key => $index) {
  2721. if ($key == $newName) {
  2722. $decoratorsExchange[$key] = $instance;
  2723. continue;
  2724. }
  2725. $decoratorsExchange[$key] = $this->_decorators[$key];
  2726. }
  2727. $this->_decorators = $decoratorsExchange;
  2728. } else {
  2729. $this->_decorators[$name] = $instance;
  2730. }
  2731. return $instance;
  2732. }
  2733. /**
  2734. * Retrieve optionally translated custom error messages
  2735. *
  2736. * @return array
  2737. */
  2738. protected function _getErrorMessages()
  2739. {
  2740. $messages = $this->getErrorMessages();
  2741. $translator = $this->getTranslator();
  2742. if (null !== $translator) {
  2743. foreach ($messages as $key => $message) {
  2744. $messages[$key] = $translator->translate($message);
  2745. }
  2746. }
  2747. return $messages;
  2748. }
  2749. }