Form.php 88 KB

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