Form.php 92 KB

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