Form.php 93 KB

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