Form.php 92 KB

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