Form.php 89 KB

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