Form.php 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386
  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. $group[] = $add;
  1605. }
  1606. }
  1607. }
  1608. if (empty($group)) {
  1609. require_once 'Zend/Form/Exception.php';
  1610. throw new Zend_Form_Exception('No valid elements specified for display group');
  1611. }
  1612. $name = (string) $name;
  1613. if (is_array($options)) {
  1614. $options['form'] = $this;
  1615. $options['elements'] = $group;
  1616. } elseif ($options instanceof Zend_Config) {
  1617. $options = $options->toArray();
  1618. $options['form'] = $this;
  1619. $options['elements'] = $group;
  1620. } else {
  1621. $options = array(
  1622. 'form' => $this,
  1623. 'elements' => $group,
  1624. );
  1625. }
  1626. if (isset($options['displayGroupClass'])) {
  1627. $class = $options['displayGroupClass'];
  1628. unset($options['displayGroupClass']);
  1629. } else {
  1630. $class = $this->getDefaultDisplayGroupClass();
  1631. }
  1632. if (!class_exists($class)) {
  1633. require_once 'Zend/Loader.php';
  1634. Zend_Loader::loadClass($class);
  1635. }
  1636. $this->_displayGroups[$name] = new $class(
  1637. $name,
  1638. $this->getPluginLoader(self::DECORATOR),
  1639. $options
  1640. );
  1641. if (!empty($this->_displayGroupPrefixPaths)) {
  1642. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1643. }
  1644. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1645. $this->_orderUpdated = true;
  1646. return $this;
  1647. }
  1648. /**
  1649. * Add a display group object (used with cloning)
  1650. *
  1651. * @param Zend_Form_DisplayGroup $group
  1652. * @param string|null $name
  1653. * @return Zend_Form
  1654. */
  1655. protected function _addDisplayGroupObject(Zend_Form_DisplayGroup $group, $name = null)
  1656. {
  1657. if (null === $name) {
  1658. $name = $group->getName();
  1659. if ('' === (string)$name) {
  1660. require_once 'Zend/Form/Exception.php';
  1661. throw new Zend_Form_Exception('Invalid display group added; requires name');
  1662. }
  1663. }
  1664. $this->_displayGroups[$name] = $group;
  1665. $group->setForm($this);
  1666. if (!empty($this->_displayGroupPrefixPaths)) {
  1667. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1668. }
  1669. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1670. $this->_orderUpdated = true;
  1671. return $this;
  1672. }
  1673. /**
  1674. * Add multiple display groups at once
  1675. *
  1676. * @param array $groups
  1677. * @return Zend_Form
  1678. */
  1679. public function addDisplayGroups(array $groups)
  1680. {
  1681. foreach ($groups as $key => $spec) {
  1682. $name = null;
  1683. if (!is_numeric($key)) {
  1684. $name = $key;
  1685. }
  1686. if ($spec instanceof Zend_Form_DisplayGroup) {
  1687. $this->_addDisplayGroupObject($spec);
  1688. }
  1689. if (!is_array($spec) || empty($spec)) {
  1690. continue;
  1691. }
  1692. $argc = count($spec);
  1693. $options = array();
  1694. if (isset($spec['elements'])) {
  1695. $elements = $spec['elements'];
  1696. if (isset($spec['name'])) {
  1697. $name = $spec['name'];
  1698. }
  1699. if (isset($spec['options'])) {
  1700. $options = $spec['options'];
  1701. }
  1702. $this->addDisplayGroup($elements, $name, $options);
  1703. } else {
  1704. switch ($argc) {
  1705. case (1 <= $argc):
  1706. $elements = array_shift($spec);
  1707. if (!is_array($elements) && (null !== $name)) {
  1708. $elements = array_merge((array) $elements, $spec);
  1709. $this->addDisplayGroup($elements, $name);
  1710. break;
  1711. }
  1712. case (2 <= $argc):
  1713. if (null !== $name) {
  1714. $options = array_shift($spec);
  1715. $this->addDisplayGroup($elements, $name, $options);
  1716. break;
  1717. }
  1718. $name = array_shift($spec);
  1719. case (3 <= $argc):
  1720. $options = array_shift($spec);
  1721. default:
  1722. $this->addDisplayGroup($elements, $name, $options);
  1723. }
  1724. }
  1725. }
  1726. return $this;
  1727. }
  1728. /**
  1729. * Add multiple display groups (overwrites)
  1730. *
  1731. * @param array $groups
  1732. * @return Zend_Form
  1733. */
  1734. public function setDisplayGroups(array $groups)
  1735. {
  1736. return $this->clearDisplayGroups()
  1737. ->addDisplayGroups($groups);
  1738. }
  1739. /**
  1740. * Return a display group
  1741. *
  1742. * @param string $name
  1743. * @return Zend_Form_DisplayGroup|null
  1744. */
  1745. public function getDisplayGroup($name)
  1746. {
  1747. $name = (string) $name;
  1748. if (isset($this->_displayGroups[$name])) {
  1749. return $this->_displayGroups[$name];
  1750. }
  1751. return null;
  1752. }
  1753. /**
  1754. * Return all display groups
  1755. *
  1756. * @return array
  1757. */
  1758. public function getDisplayGroups()
  1759. {
  1760. return $this->_displayGroups;
  1761. }
  1762. /**
  1763. * Remove a display group by name
  1764. *
  1765. * @param string $name
  1766. * @return boolean
  1767. */
  1768. public function removeDisplayGroup($name)
  1769. {
  1770. $name = (string) $name;
  1771. if (array_key_exists($name, $this->_displayGroups)) {
  1772. foreach ($this->_displayGroups[$name] as $key => $element) {
  1773. if (array_key_exists($key, $this->_elements)) {
  1774. $this->_order[$key] = $element->getOrder();
  1775. $this->_orderUpdated = true;
  1776. }
  1777. }
  1778. unset($this->_displayGroups[$name]);
  1779. if (array_key_exists($name, $this->_order)) {
  1780. unset($this->_order[$name]);
  1781. $this->_orderUpdated = true;
  1782. }
  1783. return true;
  1784. }
  1785. return false;
  1786. }
  1787. /**
  1788. * Remove all display groups
  1789. *
  1790. * @return Zend_Form
  1791. */
  1792. public function clearDisplayGroups()
  1793. {
  1794. foreach ($this->_displayGroups as $key => $group) {
  1795. if (array_key_exists($key, $this->_order)) {
  1796. unset($this->_order[$key]);
  1797. }
  1798. foreach ($group as $name => $element) {
  1799. if (isset($this->_elements[$name])) {
  1800. $this->_order[$name] = $element->getOrder();
  1801. }
  1802. $this->_order[$name] = $element->getOrder();
  1803. }
  1804. }
  1805. $this->_displayGroups = array();
  1806. $this->_orderUpdated = true;
  1807. return $this;
  1808. }
  1809. // Processing
  1810. /**
  1811. * Populate form
  1812. *
  1813. * Proxies to {@link setDefaults()}
  1814. *
  1815. * @param array $values
  1816. * @return Zend_Form
  1817. */
  1818. public function populate(array $values)
  1819. {
  1820. return $this->setDefaults($values);
  1821. }
  1822. /**
  1823. * Determine array key name from given value
  1824. *
  1825. * Given a value such as foo[bar][baz], returns the last element (in this case, 'baz').
  1826. *
  1827. * @param string $value
  1828. * @return string
  1829. */
  1830. protected function _getArrayName($value)
  1831. {
  1832. if (!is_string($value) || '' === $value) {
  1833. return $value;
  1834. }
  1835. if (!strstr($value, '[')) {
  1836. return $value;
  1837. }
  1838. $endPos = strlen($value) - 1;
  1839. if (']' != $value[$endPos]) {
  1840. return $value;
  1841. }
  1842. $start = strrpos($value, '[') + 1;
  1843. $name = substr($value, $start, $endPos - $start);
  1844. return $name;
  1845. }
  1846. /**
  1847. * Extract the value by walking the array using given array path.
  1848. *
  1849. * Given an array path such as foo[bar][baz], returns the value of the last
  1850. * element (in this case, 'baz').
  1851. *
  1852. * @param array $value Array to walk
  1853. * @param string $arrayPath Array notation path of the part to extract
  1854. * @return string
  1855. */
  1856. protected function _dissolveArrayValue($value, $arrayPath)
  1857. {
  1858. // As long as we have more levels
  1859. while ($arrayPos = strpos($arrayPath, '[')) {
  1860. // Get the next key in the path
  1861. $arrayKey = trim(substr($arrayPath, 0, $arrayPos), ']');
  1862. // Set the potentially final value or the next search point in the array
  1863. if (isset($value[$arrayKey])) {
  1864. $value = $value[$arrayKey];
  1865. }
  1866. // Set the next search point in the path
  1867. $arrayPath = trim(substr($arrayPath, $arrayPos + 1), ']');
  1868. }
  1869. if (isset($value[$arrayPath])) {
  1870. $value = $value[$arrayPath];
  1871. }
  1872. return $value;
  1873. }
  1874. /**
  1875. * Given an array, an optional arrayPath and a key this method
  1876. * dissolves the arrayPath and unsets the key within the array
  1877. * if it exists.
  1878. *
  1879. * @param array $array
  1880. * @param string|null $arrayPath
  1881. * @param string $key
  1882. * @return array
  1883. */
  1884. protected function _dissolveArrayUnsetKey($array, $arrayPath, $key)
  1885. {
  1886. $unset =& $array;
  1887. $path = trim(strtr((string)$arrayPath, array('[' => '/', ']' => '')), '/');
  1888. $segs = ('' !== $path) ? explode('/', $path) : array();
  1889. foreach ($segs as $seg) {
  1890. if (!array_key_exists($seg, (array)$unset)) {
  1891. return $array;
  1892. }
  1893. $unset =& $unset[$seg];
  1894. }
  1895. if (array_key_exists($key, (array)$unset)) {
  1896. unset($unset[$key]);
  1897. }
  1898. return $array;
  1899. }
  1900. /**
  1901. * Converts given arrayPath to an array and attaches given value at the end of it.
  1902. *
  1903. * @param mixed $value The value to attach
  1904. * @param string $arrayPath Given array path to convert and attach to.
  1905. * @return array
  1906. */
  1907. protected function _attachToArray($value, $arrayPath)
  1908. {
  1909. // As long as we have more levels
  1910. while ($arrayPos = strrpos($arrayPath, '[')) {
  1911. // Get the next key in the path
  1912. $arrayKey = trim(substr($arrayPath, $arrayPos + 1), ']');
  1913. // Attach
  1914. $value = array($arrayKey => $value);
  1915. // Set the next search point in the path
  1916. $arrayPath = trim(substr($arrayPath, 0, $arrayPos), ']');
  1917. }
  1918. $value = array($arrayPath => $value);
  1919. return $value;
  1920. }
  1921. /**
  1922. * Returns a one dimensional numerical indexed array with the
  1923. * Elements, SubForms and Elements from DisplayGroups as Values.
  1924. *
  1925. * Subitems are inserted based on their order Setting if set,
  1926. * otherwise they are appended, the resulting numerical index
  1927. * may differ from the order value.
  1928. *
  1929. * @access protected
  1930. * @return array
  1931. */
  1932. public function getElementsAndSubFormsOrdered()
  1933. {
  1934. $ordered = array();
  1935. foreach ($this->_order as $name => $order) {
  1936. $order = isset($order) ? $order : count($ordered);
  1937. if ($this->$name instanceof Zend_Form_Element ||
  1938. $this->$name instanceof Zend_Form) {
  1939. array_splice($ordered, $order, 0, array($this->$name));
  1940. } else if ($this->$name instanceof Zend_Form_DisplayGroup) {
  1941. $subordered = array();
  1942. foreach ($this->$name->getElements() as $element) {
  1943. $suborder = $element->getOrder();
  1944. $suborder = (null !== $suborder) ? $suborder : count($subordered);
  1945. array_splice($subordered, $suborder, 0, array($element));
  1946. }
  1947. if (!empty($subordered)) {
  1948. array_splice($ordered, $order, 0, $subordered);
  1949. }
  1950. }
  1951. }
  1952. return $ordered;
  1953. }
  1954. /**
  1955. * This is a helper function until php 5.3 is widespreaded
  1956. *
  1957. * @param array $into
  1958. * @access protected
  1959. * @return void
  1960. */
  1961. protected function _array_replace_recursive(array $into)
  1962. {
  1963. $fromArrays = array_slice(func_get_args(),1);
  1964. foreach ($fromArrays as $from) {
  1965. foreach ($from as $key => $value) {
  1966. if (is_array($value)) {
  1967. if (!isset($into[$key])) {
  1968. $into[$key] = array();
  1969. }
  1970. $into[$key] = $this->_array_replace_recursive($into[$key], $from[$key]);
  1971. } else {
  1972. $into[$key] = $value;
  1973. }
  1974. }
  1975. }
  1976. return $into;
  1977. }
  1978. /**
  1979. * Validate the form
  1980. *
  1981. * @param array $data
  1982. * @return boolean
  1983. */
  1984. public function isValid($data)
  1985. {
  1986. if (!is_array($data)) {
  1987. require_once 'Zend/Form/Exception.php';
  1988. throw new Zend_Form_Exception(__METHOD__ . ' expects an array');
  1989. }
  1990. $translator = $this->getTranslator();
  1991. $valid = true;
  1992. $eBelongTo = null;
  1993. if ($this->isArray()) {
  1994. $eBelongTo = $this->getElementsBelongTo();
  1995. $data = $this->_dissolveArrayValue($data, $eBelongTo);
  1996. }
  1997. $context = $data;
  1998. foreach ($this->getElements() as $key => $element) {
  1999. if (null !== $translator && $this->hasTranslator()
  2000. && !$element->hasTranslator()) {
  2001. $element->setTranslator($translator);
  2002. }
  2003. $check = $data;
  2004. if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
  2005. $check = $this->_dissolveArrayValue($data, $belongsTo);
  2006. }
  2007. if (!isset($check[$key])) {
  2008. $valid = $element->isValid(null, $context) && $valid;
  2009. } else {
  2010. $valid = $element->isValid($check[$key], $context) && $valid;
  2011. $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
  2012. }
  2013. }
  2014. foreach ($this->getSubForms() as $key => $form) {
  2015. if (null !== $translator && !$form->hasTranslator()) {
  2016. $form->setTranslator($translator);
  2017. }
  2018. if (isset($data[$key]) && !$form->isArray()) {
  2019. $valid = $form->isValid($data[$key]) && $valid;
  2020. } else {
  2021. $valid = $form->isValid($data) && $valid;
  2022. }
  2023. }
  2024. $this->_errorsExist = !$valid;
  2025. // If manually flagged as an error, return invalid status
  2026. if ($this->_errorsForced) {
  2027. return false;
  2028. }
  2029. return $valid;
  2030. }
  2031. /**
  2032. * Validate a partial form
  2033. *
  2034. * Does not check for required flags.
  2035. *
  2036. * @param array $data
  2037. * @return boolean
  2038. */
  2039. public function isValidPartial(array $data)
  2040. {
  2041. $eBelongTo = null;
  2042. if ($this->isArray()) {
  2043. $eBelongTo = $this->getElementsBelongTo();
  2044. $data = $this->_dissolveArrayValue($data, $eBelongTo);
  2045. }
  2046. $translator = $this->getTranslator();
  2047. $valid = true;
  2048. $context = $data;
  2049. foreach ($this->getElements() as $key => $element) {
  2050. $check = $data;
  2051. if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
  2052. $check = $this->_dissolveArrayValue($data, $belongsTo);
  2053. }
  2054. if (isset($check[$key])) {
  2055. if (null !== $translator && !$element->hasTranslator()) {
  2056. $element->setTranslator($translator);
  2057. }
  2058. $valid = $element->isValid($check[$key], $context) && $valid;
  2059. $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
  2060. }
  2061. }
  2062. foreach ($this->getSubForms() as $key => $form) {
  2063. if (null !== $translator && !$form->hasTranslator()) {
  2064. $form->setTranslator($translator);
  2065. }
  2066. if (isset($data[$key]) && !$form->isArray()) {
  2067. $valid = $form->isValidPartial($data[$key]) && $valid;
  2068. } else {
  2069. $valid = $form->isValidPartial($data) && $valid;
  2070. }
  2071. }
  2072. $this->_errorsExist = !$valid;
  2073. return $valid;
  2074. }
  2075. /**
  2076. * Process submitted AJAX data
  2077. *
  2078. * Checks if provided $data is valid, via {@link isValidPartial()}. If so,
  2079. * it returns JSON-encoded boolean true. If not, it returns JSON-encoded
  2080. * error messages (as returned by {@link getMessages()}).
  2081. *
  2082. * @param array $data
  2083. * @return string JSON-encoded boolean true or error messages
  2084. */
  2085. public function processAjax(array $data)
  2086. {
  2087. require_once 'Zend/Json.php';
  2088. if ($this->isValidPartial($data)) {
  2089. return Zend_Json::encode(true);
  2090. }
  2091. $messages = $this->getMessages();
  2092. return Zend_Json::encode($messages);
  2093. }
  2094. /**
  2095. * Add a custom error message to return in the event of failed validation
  2096. *
  2097. * @param string $message
  2098. * @return Zend_Form
  2099. */
  2100. public function addErrorMessage($message)
  2101. {
  2102. $this->_errorMessages[] = (string) $message;
  2103. return $this;
  2104. }
  2105. /**
  2106. * Add multiple custom error messages to return in the event of failed validation
  2107. *
  2108. * @param array $messages
  2109. * @return Zend_Form
  2110. */
  2111. public function addErrorMessages(array $messages)
  2112. {
  2113. foreach ($messages as $message) {
  2114. $this->addErrorMessage($message);
  2115. }
  2116. return $this;
  2117. }
  2118. /**
  2119. * Same as addErrorMessages(), but clears custom error message stack first
  2120. *
  2121. * @param array $messages
  2122. * @return Zend_Form
  2123. */
  2124. public function setErrorMessages(array $messages)
  2125. {
  2126. $this->clearErrorMessages();
  2127. return $this->addErrorMessages($messages);
  2128. }
  2129. /**
  2130. * Retrieve custom error messages
  2131. *
  2132. * @return array
  2133. */
  2134. public function getErrorMessages()
  2135. {
  2136. return $this->_errorMessages;
  2137. }
  2138. /**
  2139. * Clear custom error messages stack
  2140. *
  2141. * @return Zend_Form
  2142. */
  2143. public function clearErrorMessages()
  2144. {
  2145. $this->_errorMessages = array();
  2146. return $this;
  2147. }
  2148. /**
  2149. * Mark the element as being in a failed validation state
  2150. *
  2151. * @return Zend_Form
  2152. */
  2153. public function markAsError()
  2154. {
  2155. $this->_errorsExist = true;
  2156. $this->_errorsForced = true;
  2157. return $this;
  2158. }
  2159. /**
  2160. * Add an error message and mark element as failed validation
  2161. *
  2162. * @param string $message
  2163. * @return Zend_Form
  2164. */
  2165. public function addError($message)
  2166. {
  2167. $this->addErrorMessage($message);
  2168. $this->markAsError();
  2169. return $this;
  2170. }
  2171. /**
  2172. * Add multiple error messages and flag element as failed validation
  2173. *
  2174. * @param array $messages
  2175. * @return Zend_Form
  2176. */
  2177. public function addErrors(array $messages)
  2178. {
  2179. foreach ($messages as $message) {
  2180. $this->addError($message);
  2181. }
  2182. return $this;
  2183. }
  2184. /**
  2185. * Overwrite any previously set error messages and flag as failed validation
  2186. *
  2187. * @param array $messages
  2188. * @return Zend_Form
  2189. */
  2190. public function setErrors(array $messages)
  2191. {
  2192. $this->clearErrorMessages();
  2193. return $this->addErrors($messages);
  2194. }
  2195. public function persistData()
  2196. {
  2197. }
  2198. /**
  2199. * Are there errors in the form?
  2200. *
  2201. * @return bool
  2202. */
  2203. public function isErrors()
  2204. {
  2205. return $this->_errorsExist;
  2206. }
  2207. /**
  2208. * Get error codes for all elements failing validation
  2209. *
  2210. * @param string $name
  2211. * @return array
  2212. */
  2213. public function getErrors($name = null, $suppressArrayNotation = false)
  2214. {
  2215. $errors = array();
  2216. if (null !== $name) {
  2217. if (isset($this->_elements[$name])) {
  2218. return $this->getElement($name)->getErrors();
  2219. } else if (isset($this->_subForms[$name])) {
  2220. return $this->getSubForm($name)->getErrors(null, true);
  2221. }
  2222. }
  2223. foreach ($this->_elements as $key => $element) {
  2224. $errors[$key] = $element->getErrors();
  2225. }
  2226. foreach ($this->getSubForms() as $key => $subForm) {
  2227. $merge = array();
  2228. if (!$subForm->isArray()) {
  2229. $merge[$key] = $subForm->getErrors();
  2230. } else {
  2231. $merge = $this->_attachToArray($subForm->getErrors(null, true),
  2232. $subForm->getElementsBelongTo());
  2233. }
  2234. $errors = $this->_array_replace_recursive($errors, $merge);
  2235. }
  2236. if (!$suppressArrayNotation &&
  2237. $this->isArray() &&
  2238. !$this->_getIsRendered()) {
  2239. $errors = $this->_attachToArray($errors, $this->getElementsBelongTo());
  2240. }
  2241. return $errors;
  2242. }
  2243. /**
  2244. * Retrieve error messages from elements failing validations
  2245. *
  2246. * @param string $name
  2247. * @param bool $suppressArrayNotation
  2248. * @return array
  2249. */
  2250. public function getMessages($name = null, $suppressArrayNotation = false)
  2251. {
  2252. if (null !== $name) {
  2253. if (isset($this->_elements[$name])) {
  2254. return $this->getElement($name)->getMessages();
  2255. } else if (isset($this->_subForms[$name])) {
  2256. return $this->getSubForm($name)->getMessages(null, true);
  2257. }
  2258. foreach ($this->getSubForms() as $key => $subForm) {
  2259. if ($subForm->isArray()) {
  2260. $belongTo = $subForm->getElementsBelongTo();
  2261. if ($name == $this->_getArrayName($belongTo)) {
  2262. return $subForm->getMessages(null, true);
  2263. }
  2264. }
  2265. }
  2266. }
  2267. $customMessages = $this->_getErrorMessages();
  2268. if ($this->isErrors() && !empty($customMessages)) {
  2269. return $customMessages;
  2270. }
  2271. $messages = array();
  2272. foreach ($this->getElements() as $name => $element) {
  2273. $eMessages = $element->getMessages();
  2274. if (!empty($eMessages)) {
  2275. $messages[$name] = $eMessages;
  2276. }
  2277. }
  2278. foreach ($this->getSubForms() as $key => $subForm) {
  2279. $merge = $subForm->getMessages(null, true);
  2280. if (!empty($merge)) {
  2281. if (!$subForm->isArray()) {
  2282. $merge = array($key => $merge);
  2283. } else {
  2284. $merge = $this->_attachToArray($merge,
  2285. $subForm->getElementsBelongTo());
  2286. }
  2287. $messages = $this->_array_replace_recursive($messages, $merge);
  2288. }
  2289. }
  2290. if (!$suppressArrayNotation &&
  2291. $this->isArray() &&
  2292. !$this->_getIsRendered()) {
  2293. $messages = $this->_attachToArray($messages, $this->getElementsBelongTo());
  2294. }
  2295. return $messages;
  2296. }
  2297. /**
  2298. * Retrieve translated custom error messages
  2299. * Proxies to {@link _getErrorMessages()}.
  2300. *
  2301. * @return array
  2302. */
  2303. public function getCustomMessages()
  2304. {
  2305. return $this->_getErrorMessages();
  2306. }
  2307. // Rendering
  2308. /**
  2309. * Set view object
  2310. *
  2311. * @param Zend_View_Interface $view
  2312. * @return Zend_Form
  2313. */
  2314. public function setView(Zend_View_Interface $view = null)
  2315. {
  2316. $this->_view = $view;
  2317. return $this;
  2318. }
  2319. /**
  2320. * Retrieve view object
  2321. *
  2322. * If none registered, attempts to pull from ViewRenderer.
  2323. *
  2324. * @return Zend_View_Interface|null
  2325. */
  2326. public function getView()
  2327. {
  2328. if (null === $this->_view) {
  2329. require_once 'Zend/Controller/Action/HelperBroker.php';
  2330. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2331. $this->setView($viewRenderer->view);
  2332. }
  2333. return $this->_view;
  2334. }
  2335. /**
  2336. * Instantiate a decorator based on class name or class name fragment
  2337. *
  2338. * @param string $name
  2339. * @param null|array $options
  2340. * @return Zend_Form_Decorator_Interface
  2341. */
  2342. protected function _getDecorator($name, $options)
  2343. {
  2344. $class = $this->getPluginLoader(self::DECORATOR)->load($name);
  2345. if (null === $options) {
  2346. $decorator = new $class;
  2347. } else {
  2348. $decorator = new $class($options);
  2349. }
  2350. return $decorator;
  2351. }
  2352. /**
  2353. * Add a decorator for rendering the element
  2354. *
  2355. * @param string|Zend_Form_Decorator_Interface $decorator
  2356. * @param array|Zend_Config $options Options with which to initialize decorator
  2357. * @return Zend_Form
  2358. */
  2359. public function addDecorator($decorator, $options = null)
  2360. {
  2361. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  2362. $name = get_class($decorator);
  2363. } elseif (is_string($decorator)) {
  2364. $name = $decorator;
  2365. $decorator = array(
  2366. 'decorator' => $name,
  2367. 'options' => $options,
  2368. );
  2369. } elseif (is_array($decorator)) {
  2370. foreach ($decorator as $name => $spec) {
  2371. break;
  2372. }
  2373. if (is_numeric($name)) {
  2374. require_once 'Zend/Form/Exception.php';
  2375. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  2376. }
  2377. if (is_string($spec)) {
  2378. $decorator = array(
  2379. 'decorator' => $spec,
  2380. 'options' => $options,
  2381. );
  2382. } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
  2383. $decorator = $spec;
  2384. }
  2385. } else {
  2386. require_once 'Zend/Form/Exception.php';
  2387. throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
  2388. }
  2389. $this->_decorators[$name] = $decorator;
  2390. return $this;
  2391. }
  2392. /**
  2393. * Add many decorators at once
  2394. *
  2395. * @param array $decorators
  2396. * @return Zend_Form
  2397. */
  2398. public function addDecorators(array $decorators)
  2399. {
  2400. foreach ($decorators as $decoratorName => $decoratorInfo) {
  2401. if (is_string($decoratorInfo) ||
  2402. $decoratorInfo instanceof Zend_Form_Decorator_Interface) {
  2403. if (!is_numeric($decoratorName)) {
  2404. $this->addDecorator(array($decoratorName => $decoratorInfo));
  2405. } else {
  2406. $this->addDecorator($decoratorInfo);
  2407. }
  2408. } elseif (is_array($decoratorInfo)) {
  2409. $argc = count($decoratorInfo);
  2410. $options = array();
  2411. if (isset($decoratorInfo['decorator'])) {
  2412. $decorator = $decoratorInfo['decorator'];
  2413. if (isset($decoratorInfo['options'])) {
  2414. $options = $decoratorInfo['options'];
  2415. }
  2416. $this->addDecorator($decorator, $options);
  2417. } else {
  2418. switch (true) {
  2419. case (0 == $argc):
  2420. break;
  2421. case (1 <= $argc):
  2422. $decorator = array_shift($decoratorInfo);
  2423. case (2 <= $argc):
  2424. $options = array_shift($decoratorInfo);
  2425. default:
  2426. $this->addDecorator($decorator, $options);
  2427. break;
  2428. }
  2429. }
  2430. } else {
  2431. require_once 'Zend/Form/Exception.php';
  2432. throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
  2433. }
  2434. }
  2435. return $this;
  2436. }
  2437. /**
  2438. * Overwrite all decorators
  2439. *
  2440. * @param array $decorators
  2441. * @return Zend_Form
  2442. */
  2443. public function setDecorators(array $decorators)
  2444. {
  2445. $this->clearDecorators();
  2446. return $this->addDecorators($decorators);
  2447. }
  2448. /**
  2449. * Retrieve a registered decorator
  2450. *
  2451. * @param string $name
  2452. * @return false|Zend_Form_Decorator_Abstract
  2453. */
  2454. public function getDecorator($name)
  2455. {
  2456. if (!isset($this->_decorators[$name])) {
  2457. $len = strlen($name);
  2458. foreach ($this->_decorators as $localName => $decorator) {
  2459. if ($len > strlen($localName)) {
  2460. continue;
  2461. }
  2462. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  2463. if (is_array($decorator)) {
  2464. return $this->_loadDecorator($decorator, $localName);
  2465. }
  2466. return $decorator;
  2467. }
  2468. }
  2469. return false;
  2470. }
  2471. if (is_array($this->_decorators[$name])) {
  2472. return $this->_loadDecorator($this->_decorators[$name], $name);
  2473. }
  2474. return $this->_decorators[$name];
  2475. }
  2476. /**
  2477. * Retrieve all decorators
  2478. *
  2479. * @return array
  2480. */
  2481. public function getDecorators()
  2482. {
  2483. foreach ($this->_decorators as $key => $value) {
  2484. if (is_array($value)) {
  2485. $this->_loadDecorator($value, $key);
  2486. }
  2487. }
  2488. return $this->_decorators;
  2489. }
  2490. /**
  2491. * Remove a single decorator
  2492. *
  2493. * @param string $name
  2494. * @return bool
  2495. */
  2496. public function removeDecorator($name)
  2497. {
  2498. $decorator = $this->getDecorator($name);
  2499. if ($decorator) {
  2500. if (array_key_exists($name, $this->_decorators)) {
  2501. unset($this->_decorators[$name]);
  2502. } else {
  2503. $class = get_class($decorator);
  2504. if (!array_key_exists($class, $this->_decorators)) {
  2505. return false;
  2506. }
  2507. unset($this->_decorators[$class]);
  2508. }
  2509. return true;
  2510. }
  2511. return false;
  2512. }
  2513. /**
  2514. * Clear all decorators
  2515. *
  2516. * @return Zend_Form
  2517. */
  2518. public function clearDecorators()
  2519. {
  2520. $this->_decorators = array();
  2521. return $this;
  2522. }
  2523. /**
  2524. * Set all element decorators as specified
  2525. *
  2526. * @param array $decorators
  2527. * @param array|null $elements Specific elements to decorate or exclude from decoration
  2528. * @param bool $include Whether $elements is an inclusion or exclusion list
  2529. * @return Zend_Form
  2530. */
  2531. public function setElementDecorators(array $decorators, array $elements = null, $include = true)
  2532. {
  2533. if (is_array($elements)) {
  2534. if ($include) {
  2535. $elementObjs = array();
  2536. foreach ($elements as $name) {
  2537. if (null !== ($element = $this->getElement($name))) {
  2538. $elementObjs[] = $element;
  2539. }
  2540. }
  2541. } else {
  2542. $elementObjs = $this->getElements();
  2543. foreach ($elements as $name) {
  2544. if (array_key_exists($name, $elementObjs)) {
  2545. unset($elementObjs[$name]);
  2546. }
  2547. }
  2548. }
  2549. } else {
  2550. $elementObjs = $this->getElements();
  2551. }
  2552. foreach ($elementObjs as $element) {
  2553. $element->setDecorators($decorators);
  2554. }
  2555. $this->_elementDecorators = $decorators;
  2556. return $this;
  2557. }
  2558. /**
  2559. * Set all display group decorators as specified
  2560. *
  2561. * @param array $decorators
  2562. * @return Zend_Form
  2563. */
  2564. public function setDisplayGroupDecorators(array $decorators)
  2565. {
  2566. foreach ($this->getDisplayGroups() as $group) {
  2567. $group->setDecorators($decorators);
  2568. }
  2569. return $this;
  2570. }
  2571. /**
  2572. * Set all subform decorators as specified
  2573. *
  2574. * @param array $decorators
  2575. * @return Zend_Form
  2576. */
  2577. public function setSubFormDecorators(array $decorators)
  2578. {
  2579. foreach ($this->getSubForms() as $form) {
  2580. $form->setDecorators($decorators);
  2581. }
  2582. return $this;
  2583. }
  2584. /**
  2585. * Render form
  2586. *
  2587. * @param Zend_View_Interface $view
  2588. * @return string
  2589. */
  2590. public function render(Zend_View_Interface $view = null)
  2591. {
  2592. if (null !== $view) {
  2593. $this->setView($view);
  2594. }
  2595. $content = '';
  2596. foreach ($this->getDecorators() as $decorator) {
  2597. $decorator->setElement($this);
  2598. $content = $decorator->render($content);
  2599. }
  2600. $this->_setIsRendered();
  2601. return $content;
  2602. }
  2603. /**
  2604. * Serialize as string
  2605. *
  2606. * Proxies to {@link render()}.
  2607. *
  2608. * @return string
  2609. */
  2610. public function __toString()
  2611. {
  2612. try {
  2613. $return = $this->render();
  2614. return $return;
  2615. } catch (Exception $e) {
  2616. $message = "Exception caught by form: " . $e->getMessage()
  2617. . "\nStack Trace:\n" . $e->getTraceAsString();
  2618. trigger_error($message, E_USER_WARNING);
  2619. return '';
  2620. }
  2621. }
  2622. // Localization:
  2623. /**
  2624. * Set translator object
  2625. *
  2626. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2627. * @return Zend_Form
  2628. */
  2629. public function setTranslator($translator = null)
  2630. {
  2631. if (null === $translator) {
  2632. $this->_translator = null;
  2633. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2634. $this->_translator = $translator;
  2635. } elseif ($translator instanceof Zend_Translate) {
  2636. $this->_translator = $translator->getAdapter();
  2637. } else {
  2638. require_once 'Zend/Form/Exception.php';
  2639. throw new Zend_Form_Exception('Invalid translator specified');
  2640. }
  2641. return $this;
  2642. }
  2643. /**
  2644. * Set global default translator object
  2645. *
  2646. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2647. * @return void
  2648. */
  2649. public static function setDefaultTranslator($translator = null)
  2650. {
  2651. if (null === $translator) {
  2652. self::$_translatorDefault = null;
  2653. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2654. self::$_translatorDefault = $translator;
  2655. } elseif ($translator instanceof Zend_Translate) {
  2656. self::$_translatorDefault = $translator->getAdapter();
  2657. } else {
  2658. require_once 'Zend/Form/Exception.php';
  2659. throw new Zend_Form_Exception('Invalid translator specified');
  2660. }
  2661. }
  2662. /**
  2663. * Retrieve translator object
  2664. *
  2665. * @return Zend_Translate|null
  2666. */
  2667. public function getTranslator()
  2668. {
  2669. if ($this->translatorIsDisabled()) {
  2670. return null;
  2671. }
  2672. if (null === $this->_translator) {
  2673. return self::getDefaultTranslator();
  2674. }
  2675. return $this->_translator;
  2676. }
  2677. /**
  2678. * Does this form have its own specific translator?
  2679. *
  2680. * @return bool
  2681. */
  2682. public function hasTranslator()
  2683. {
  2684. return (bool)$this->_translator;
  2685. }
  2686. /**
  2687. * Get global default translator object
  2688. *
  2689. * @return null|Zend_Translate
  2690. */
  2691. public static function getDefaultTranslator()
  2692. {
  2693. if (null === self::$_translatorDefault) {
  2694. require_once 'Zend/Registry.php';
  2695. if (Zend_Registry::isRegistered('Zend_Translate')) {
  2696. $translator = Zend_Registry::get('Zend_Translate');
  2697. if ($translator instanceof Zend_Translate_Adapter) {
  2698. return $translator;
  2699. } elseif ($translator instanceof Zend_Translate) {
  2700. return $translator->getAdapter();
  2701. }
  2702. }
  2703. }
  2704. return self::$_translatorDefault;
  2705. }
  2706. /**
  2707. * Is there a default translation object set?
  2708. *
  2709. * @return boolean
  2710. */
  2711. public static function hasDefaultTranslator()
  2712. {
  2713. return (bool)self::$_translatorDefault;
  2714. }
  2715. /**
  2716. * Indicate whether or not translation should be disabled
  2717. *
  2718. * @param bool $flag
  2719. * @return Zend_Form
  2720. */
  2721. public function setDisableTranslator($flag)
  2722. {
  2723. $this->_translatorDisabled = (bool) $flag;
  2724. return $this;
  2725. }
  2726. /**
  2727. * Is translation disabled?
  2728. *
  2729. * @return bool
  2730. */
  2731. public function translatorIsDisabled()
  2732. {
  2733. return $this->_translatorDisabled;
  2734. }
  2735. /**
  2736. * Overloading: access to elements, form groups, and display groups
  2737. *
  2738. * @param string $name
  2739. * @return Zend_Form_Element|Zend_Form|null
  2740. */
  2741. public function __get($name)
  2742. {
  2743. if (isset($this->_elements[$name])) {
  2744. return $this->_elements[$name];
  2745. } elseif (isset($this->_subForms[$name])) {
  2746. return $this->_subForms[$name];
  2747. } elseif (isset($this->_displayGroups[$name])) {
  2748. return $this->_displayGroups[$name];
  2749. }
  2750. return null;
  2751. }
  2752. /**
  2753. * Overloading: access to elements, form groups, and display groups
  2754. *
  2755. * @param string $name
  2756. * @param Zend_Form_Element|Zend_Form $value
  2757. * @return void
  2758. * @throws Zend_Form_Exception for invalid $value
  2759. */
  2760. public function __set($name, $value)
  2761. {
  2762. if ($value instanceof Zend_Form_Element) {
  2763. $this->addElement($value, $name);
  2764. return;
  2765. } elseif ($value instanceof Zend_Form) {
  2766. $this->addSubForm($value, $name);
  2767. return;
  2768. } elseif (is_array($value)) {
  2769. $this->addDisplayGroup($value, $name);
  2770. return;
  2771. }
  2772. require_once 'Zend/Form/Exception.php';
  2773. if (is_object($value)) {
  2774. $type = get_class($value);
  2775. } else {
  2776. $type = gettype($value);
  2777. }
  2778. throw new Zend_Form_Exception('Only form elements and groups may be overloaded; variable of type "' . $type . '" provided');
  2779. }
  2780. /**
  2781. * Overloading: access to elements, form groups, and display groups
  2782. *
  2783. * @param string $name
  2784. * @return boolean
  2785. */
  2786. public function __isset($name)
  2787. {
  2788. if (isset($this->_elements[$name])
  2789. || isset($this->_subForms[$name])
  2790. || isset($this->_displayGroups[$name]))
  2791. {
  2792. return true;
  2793. }
  2794. return false;
  2795. }
  2796. /**
  2797. * Overloading: access to elements, form groups, and display groups
  2798. *
  2799. * @param string $name
  2800. * @return void
  2801. */
  2802. public function __unset($name)
  2803. {
  2804. if (isset($this->_elements[$name])) {
  2805. unset($this->_elements[$name]);
  2806. } elseif (isset($this->_subForms[$name])) {
  2807. unset($this->_subForms[$name]);
  2808. } elseif (isset($this->_displayGroups[$name])) {
  2809. unset($this->_displayGroups[$name]);
  2810. }
  2811. }
  2812. /**
  2813. * Overloading: allow rendering specific decorators
  2814. *
  2815. * Call renderDecoratorName() to render a specific decorator.
  2816. *
  2817. * @param string $method
  2818. * @param array $args
  2819. * @return string
  2820. * @throws Zend_Form_Exception for invalid decorator or invalid method call
  2821. */
  2822. public function __call($method, $args)
  2823. {
  2824. if ('render' == substr($method, 0, 6)) {
  2825. $decoratorName = substr($method, 6);
  2826. if (false !== ($decorator = $this->getDecorator($decoratorName))) {
  2827. $decorator->setElement($this);
  2828. $seed = '';
  2829. if (0 < count($args)) {
  2830. $seed = array_shift($args);
  2831. }
  2832. if ($decoratorName === 'FormElements' ||
  2833. $decoratorName === 'PrepareElements') {
  2834. $this->_setIsRendered();
  2835. }
  2836. return $decorator->render($seed);
  2837. }
  2838. require_once 'Zend/Form/Exception.php';
  2839. throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
  2840. }
  2841. require_once 'Zend/Form/Exception.php';
  2842. throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
  2843. }
  2844. // Interfaces: Iterator, Countable
  2845. /**
  2846. * Current element/subform/display group
  2847. *
  2848. * @return Zend_Form_Element|Zend_Form_DisplayGroup|Zend_Form
  2849. */
  2850. public function current()
  2851. {
  2852. $this->_sort();
  2853. current($this->_order);
  2854. $key = key($this->_order);
  2855. if (isset($this->_elements[$key])) {
  2856. return $this->getElement($key);
  2857. } elseif (isset($this->_subForms[$key])) {
  2858. return $this->getSubForm($key);
  2859. } elseif (isset($this->_displayGroups[$key])) {
  2860. return $this->getDisplayGroup($key);
  2861. } else {
  2862. require_once 'Zend/Form/Exception.php';
  2863. throw new Zend_Form_Exception(sprintf('Corruption detected in form; invalid key ("%s") found in internal iterator', (string) $key));
  2864. }
  2865. }
  2866. /**
  2867. * Current element/subform/display group name
  2868. *
  2869. * @return string
  2870. */
  2871. public function key()
  2872. {
  2873. $this->_sort();
  2874. return key($this->_order);
  2875. }
  2876. /**
  2877. * Move pointer to next element/subform/display group
  2878. *
  2879. * @return void
  2880. */
  2881. public function next()
  2882. {
  2883. $this->_sort();
  2884. next($this->_order);
  2885. }
  2886. /**
  2887. * Move pointer to beginning of element/subform/display group loop
  2888. *
  2889. * @return void
  2890. */
  2891. public function rewind()
  2892. {
  2893. $this->_sort();
  2894. reset($this->_order);
  2895. }
  2896. /**
  2897. * Determine if current element/subform/display group is valid
  2898. *
  2899. * @return bool
  2900. */
  2901. public function valid()
  2902. {
  2903. $this->_sort();
  2904. return (current($this->_order) !== false);
  2905. }
  2906. /**
  2907. * Count of elements/subforms that are iterable
  2908. *
  2909. * @return int
  2910. */
  2911. public function count()
  2912. {
  2913. return count($this->_order);
  2914. }
  2915. /**
  2916. * Set flag to disable loading default decorators
  2917. *
  2918. * @param bool $flag
  2919. * @return Zend_Form
  2920. */
  2921. public function setDisableLoadDefaultDecorators($flag)
  2922. {
  2923. $this->_disableLoadDefaultDecorators = (bool) $flag;
  2924. return $this;
  2925. }
  2926. /**
  2927. * Should we load the default decorators?
  2928. *
  2929. * @return bool
  2930. */
  2931. public function loadDefaultDecoratorsIsDisabled()
  2932. {
  2933. return $this->_disableLoadDefaultDecorators;
  2934. }
  2935. /**
  2936. * Load the default decorators
  2937. *
  2938. * @return void
  2939. */
  2940. public function loadDefaultDecorators()
  2941. {
  2942. if ($this->loadDefaultDecoratorsIsDisabled()) {
  2943. return $this;
  2944. }
  2945. $decorators = $this->getDecorators();
  2946. if (empty($decorators)) {
  2947. $this->addDecorator('FormElements')
  2948. ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
  2949. ->addDecorator('Form');
  2950. }
  2951. return $this;
  2952. }
  2953. /**
  2954. * Remove an element from iteration
  2955. *
  2956. * @param string $name Element/group/form name
  2957. * @return void
  2958. */
  2959. public function removeFromIteration($name)
  2960. {
  2961. if (array_key_exists($name, $this->_order)) {
  2962. unset($this->_order[$name]);
  2963. $this->_orderUpdated = true;
  2964. }
  2965. }
  2966. /**
  2967. * Sort items according to their order
  2968. *
  2969. * @return void
  2970. */
  2971. protected function _sort()
  2972. {
  2973. if ($this->_orderUpdated) {
  2974. $items = array();
  2975. $index = 0;
  2976. foreach ($this->_order as $key => $order) {
  2977. if (null === $order) {
  2978. if (null === ($order = $this->{$key}->getOrder())) {
  2979. while (array_search($index, $this->_order, true)) {
  2980. ++$index;
  2981. }
  2982. $items[$index] = $key;
  2983. ++$index;
  2984. } else {
  2985. $items[$order] = $key;
  2986. }
  2987. } else {
  2988. $items[$order] = $key;
  2989. }
  2990. }
  2991. $items = array_flip($items);
  2992. asort($items);
  2993. $this->_order = $items;
  2994. $this->_orderUpdated = false;
  2995. }
  2996. }
  2997. /**
  2998. * Lazy-load a decorator
  2999. *
  3000. * @param array $decorator Decorator type and options
  3001. * @param mixed $name Decorator name or alias
  3002. * @return Zend_Form_Decorator_Interface
  3003. */
  3004. protected function _loadDecorator(array $decorator, $name)
  3005. {
  3006. $sameName = false;
  3007. if ($name == $decorator['decorator']) {
  3008. $sameName = true;
  3009. }
  3010. $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
  3011. if ($sameName) {
  3012. $newName = get_class($instance);
  3013. $decoratorNames = array_keys($this->_decorators);
  3014. $order = array_flip($decoratorNames);
  3015. $order[$newName] = $order[$name];
  3016. $decoratorsExchange = array();
  3017. unset($order[$name]);
  3018. asort($order);
  3019. foreach ($order as $key => $index) {
  3020. if ($key == $newName) {
  3021. $decoratorsExchange[$key] = $instance;
  3022. continue;
  3023. }
  3024. $decoratorsExchange[$key] = $this->_decorators[$key];
  3025. }
  3026. $this->_decorators = $decoratorsExchange;
  3027. } else {
  3028. $this->_decorators[$name] = $instance;
  3029. }
  3030. return $instance;
  3031. }
  3032. /**
  3033. * Retrieve optionally translated custom error messages
  3034. *
  3035. * @return array
  3036. */
  3037. protected function _getErrorMessages()
  3038. {
  3039. $messages = $this->getErrorMessages();
  3040. $translator = $this->getTranslator();
  3041. if (null !== $translator) {
  3042. foreach ($messages as $key => $message) {
  3043. $messages[$key] = $translator->translate($message);
  3044. }
  3045. }
  3046. return $messages;
  3047. }
  3048. }