Form.php 97 KB

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