QuickForm.php 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Create, validate and process HTML forms
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  17. * @author Bertrand Mansion <bmansion@mamasam.com>
  18. * @author Alexey Borzov <avb@php.net>
  19. * @copyright 2001-2009 The PHP Group
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id: QuickForm.php,v 1.166 2009/04/04 21:34:02 avb Exp $
  22. * @link http://pear.php.net/package/HTML_QuickForm
  23. */
  24. /**
  25. * Validation rules known to HTML_QuickForm
  26. * @see HTML_QuickForm::registerRule(), HTML_QuickForm::getRegisteredRules(),
  27. * HTML_QuickForm::isRuleRegistered()
  28. * @global array $GLOBALS['_HTML_QuickForm_registered_rules']
  29. */
  30. // {{{ error codes
  31. /**#@+
  32. * Error codes for HTML_QuickForm
  33. *
  34. * Codes are mapped to textual messages by errorMessage() method, if you add a
  35. * new code be sure to add a new message for it to errorMessage()
  36. *
  37. * @see HTML_QuickForm::errorMessage()
  38. */
  39. define('QUICKFORM_OK', 1);
  40. define('QUICKFORM_ERROR', -1);
  41. define('QUICKFORM_INVALID_RULE', -2);
  42. define('QUICKFORM_NONEXIST_ELEMENT', -3);
  43. define('QUICKFORM_INVALID_FILTER', -4);
  44. define('QUICKFORM_UNREGISTERED_ELEMENT', -5);
  45. define('QUICKFORM_INVALID_ELEMENT_NAME', -6);
  46. define('QUICKFORM_INVALID_PROCESS', -7);
  47. define('QUICKFORM_DEPRECATED', -8);
  48. define('QUICKFORM_INVALID_DATASOURCE', -9);
  49. /**#@-*/
  50. // }}}
  51. /**
  52. * Class HTML_QuickForm
  53. * Create, validate and process HTML forms
  54. *
  55. * @category HTML
  56. * @package HTML_QuickForm
  57. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  58. * @author Bertrand Mansion <bmansion@mamasam.com>
  59. * @author Alexey Borzov <avb@php.net>
  60. * @version Release: 3.2.11
  61. */
  62. class HTML_QuickForm extends HTML_Common
  63. {
  64. const MAX_ELEMENT_ARGUMENT = 10;
  65. private $dateTimePickerLibraryAdded;
  66. /**
  67. * Array containing the form fields
  68. * @since 1.0
  69. * @var array
  70. * @access private
  71. */
  72. public $_elements = array();
  73. /**
  74. * Array containing element name to index map
  75. * @since 1.1
  76. * @var array
  77. * @access private
  78. */
  79. public $_elementIndex = array();
  80. /**
  81. * Array containing indexes of duplicate elements
  82. * @since 2.10
  83. * @var array
  84. * @access private
  85. */
  86. public $_duplicateIndex = array();
  87. /**
  88. * Array containing required field IDs
  89. * @since 1.0
  90. * @var array
  91. * @access private
  92. */
  93. public $_required = array();
  94. /**
  95. * Prefix message in javascript alert if error
  96. * @since 1.0
  97. * @var string
  98. * @access public
  99. */
  100. public $_jsPrefix = 'Invalid information entered.';
  101. /**
  102. * Postfix message in javascript alert if error
  103. * @since 1.0
  104. * @var string
  105. * @access public
  106. */
  107. public $_jsPostfix = 'Please correct these fields.';
  108. /**
  109. * Datasource object implementing the informal
  110. * datasource protocol
  111. * @since 3.3
  112. * @var object
  113. * @access private
  114. */
  115. public $_datasource;
  116. /**
  117. * Array of default form values
  118. * @since 2.0
  119. * @var array
  120. * @access private
  121. */
  122. public $_defaultValues = array();
  123. /**
  124. * Array of constant form values
  125. * @since 2.0
  126. * @var array
  127. * @access private
  128. */
  129. public $_constantValues = array();
  130. /**
  131. * Array of submitted form values
  132. * @since 1.0
  133. * @var array
  134. * @access private
  135. */
  136. public $_submitValues = array();
  137. /**
  138. * Array of submitted form files
  139. * @since 1.0
  140. * @var integer
  141. * @access public
  142. */
  143. public $_submitFiles = array();
  144. /**
  145. * Value for maxfilesize hidden element if form contains file input
  146. * @since 1.0
  147. * @var integer
  148. * @access public
  149. */
  150. public $_maxFileSize = 1048576; // 1 Mb = 1048576
  151. /**
  152. * Flag to know if all fields are frozen
  153. * @since 1.0
  154. * @var boolean
  155. * @access private
  156. */
  157. public $_freezeAll = false;
  158. /**
  159. * Array containing the form rules
  160. * @since 1.0
  161. * @var array
  162. * @access private
  163. */
  164. public $_rules = array();
  165. /**
  166. * Form rules, global variety
  167. * @var array
  168. * @access private
  169. */
  170. public $_formRules = array();
  171. /**
  172. * Array containing the validation errors
  173. * @since 1.0
  174. * @var array
  175. * @access private
  176. */
  177. public $_errors = array();
  178. /**
  179. * Note for required fields in the form
  180. * @var string
  181. * @since 1.0
  182. * @access private
  183. */
  184. public $_requiredNote = '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>';
  185. /**
  186. * Whether the form was submitted
  187. * @var boolean
  188. * @access private
  189. */
  190. public $_flagSubmitted = false;
  191. // }}}
  192. // {{{ constructor
  193. /**
  194. * Class constructor
  195. * @param string $formName Form's name.
  196. * @param string $method (optional)Form's method defaults to 'POST'
  197. * @param string $action (optional)Form's action
  198. * @param string $target (optional)Form's target defaults to '_self'
  199. * @param mixed $attributes (optional)Extra attributes for <form> tag
  200. * @param bool $trackSubmit (optional)Whether to track if the form was submitted by adding a special hidden field
  201. * @access public
  202. */
  203. public function __construct($formName='', $method='post', $action='', $target='', $attributes=null, $trackSubmit = false)
  204. {
  205. parent::__construct($attributes);
  206. $method = (strtoupper($method) == 'GET') ? 'get' : 'post';
  207. $action = ($action == '') ? api_get_self() : $action;
  208. $target = empty($target) ? array() : array('target' => $target);
  209. $form_id = $formName;
  210. if (isset($attributes['id']) && !empty($attributes['id'])) {
  211. $form_id = Security::remove_XSS($attributes['id']);
  212. }
  213. $attributes = array(
  214. 'action' => $action,
  215. 'method' => $method,
  216. 'name' => $formName,
  217. 'id' => $form_id
  218. ) + $target;
  219. $this->updateAttributes($attributes);
  220. if (!$trackSubmit || isset($_REQUEST['_qf__' . $formName])) {
  221. if (1 == get_magic_quotes_gpc()) {
  222. $this->_submitValues = $this->_recursiveFilter('stripslashes', 'get' == $method? $_GET: $_POST);
  223. foreach ($_FILES as $keyFirst => $valFirst) {
  224. foreach ($valFirst as $keySecond => $valSecond) {
  225. if ('name' == $keySecond) {
  226. $this->_submitFiles[$keyFirst][$keySecond] = $this->_recursiveFilter('stripslashes', $valSecond);
  227. } else {
  228. $this->_submitFiles[$keyFirst][$keySecond] = $valSecond;
  229. }
  230. }
  231. }
  232. } else {
  233. $this->_submitValues = 'get' == $method? $_GET: $_POST;
  234. $this->_submitFiles = $_FILES;
  235. }
  236. $this->_flagSubmitted = count($this->_submitValues) > 0 || count($this->_submitFiles) > 0;
  237. }
  238. if ($trackSubmit) {
  239. unset($this->_submitValues['_qf__' . $formName]);
  240. $this->addElement('hidden', '_qf__' . $formName, null);
  241. }
  242. if (preg_match('/^([0-9]+)([a-zA-Z]*)$/', ini_get('upload_max_filesize'), $matches)) {
  243. // see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
  244. switch (strtoupper($matches['2'])) {
  245. case 'G':
  246. $this->_maxFileSize = $matches['1'] * 1073741824;
  247. break;
  248. case 'M':
  249. $this->_maxFileSize = $matches['1'] * 1048576;
  250. break;
  251. case 'K':
  252. $this->_maxFileSize = $matches['1'] * 1024;
  253. break;
  254. default:
  255. $this->_maxFileSize = $matches['1'];
  256. }
  257. }
  258. // $course_id = api_get_course_int_id();
  259. // //If I'm in a course replace the default max filesize with the course limits
  260. // if (!empty($course_id)) {
  261. // $free_course_quota = DocumentManager::get_course_quota() - DocumentManager::documents_total_space();
  262. // if (empty($this->_maxFileSize) || $free_course_quota <= $this->_maxFileSize) {
  263. // $this->_maxFileSize = intval($free_course_quota);
  264. // }
  265. // }
  266. } // end constructor
  267. // }}}
  268. // {{{ apiVersion()
  269. /**
  270. * Returns the current API version
  271. *
  272. * @since 1.0
  273. * @access public
  274. * @return float
  275. */
  276. function apiVersion()
  277. {
  278. return 3.2;
  279. } // end func apiVersion
  280. // }}}
  281. // {{{ registerElementType()
  282. /**
  283. * Registers a new element type
  284. *
  285. * @param string $typeName Name of element type
  286. * @param string $include Include path for element type
  287. * @param string $className Element class name
  288. * @since 1.0
  289. * @access public
  290. * @return void
  291. */
  292. function registerElementType($typeName, $include, $className)
  293. {
  294. $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][strtolower($typeName)] = array($include, $className);
  295. } // end func registerElementType
  296. // }}}
  297. // {{{ registerRule()
  298. /**
  299. * Registers a new validation rule
  300. *
  301. * @param string $ruleName Name of validation rule
  302. * @param string $type Either: 'regex', 'function' or 'rule' for an HTML_QuickForm_Rule object
  303. * @param string $data1 Name of function, regular expression or HTML_QuickForm_Rule classname
  304. * @param string $data2 Object parent of above function or HTML_QuickForm_Rule file path
  305. * @since 1.0
  306. * @access public
  307. * @return void
  308. */
  309. public static function registerRule($ruleName, $type, $data1, $data2 = null)
  310. {
  311. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  312. $registry->registerRule($ruleName, $type, $data1, $data2);
  313. } // end func registerRule
  314. // }}}
  315. // {{{ elementExists()
  316. /**
  317. * Returns true if element is in the form
  318. *
  319. * @param string $element form name of element to check
  320. * @since 1.0
  321. * @access public
  322. * @return boolean
  323. */
  324. public function elementExists($element = null)
  325. {
  326. return isset($this->_elementIndex[$element]);
  327. }
  328. // }}}
  329. // {{{ setDefaults()
  330. /**
  331. * Initializes default form values
  332. *
  333. * @param array $defaultValues values used to fill the form
  334. * @param mixed $filter (optional) filter(s) to apply to all default values
  335. * @since 1.0
  336. * @access public
  337. * @return void
  338. */
  339. public function setDefaults($defaultValues = null, $filter = null)
  340. {
  341. if (is_array($defaultValues)) {
  342. if (isset($filter)) {
  343. if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
  344. foreach ($filter as $val) {
  345. if (!is_callable($val)) {
  346. throw new \Exception("Callback function does not exist in QuickForm::setDefaults()");
  347. } else {
  348. $defaultValues = $this->_recursiveFilter($val, $defaultValues);
  349. }
  350. }
  351. } elseif (!is_callable($filter)) {
  352. throw new \Exception("Callback function does not exist in QuickForm::setDefaults()");
  353. } else {
  354. $defaultValues = $this->_recursiveFilter($filter, $defaultValues);
  355. }
  356. }
  357. $this->_defaultValues = HTML_QuickForm::arrayMerge($this->_defaultValues, $defaultValues);
  358. $this->_constantValues = $this->_defaultValues;
  359. foreach (array_keys($this->_elements) as $key) {
  360. $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
  361. }
  362. }
  363. } // end func setDefaults
  364. // }}}
  365. // {{{ setConstants()
  366. /**
  367. * Initializes constant form values.
  368. * These values won't get overridden by POST or GET vars
  369. *
  370. * @param array $constantValues values used to fill the form
  371. * @param mixed $filter (optional) filter(s) to apply to all default values
  372. *
  373. * @since 2.0
  374. * @access public
  375. * @return void
  376. */
  377. public function setConstants($constantValues = null, $filter = null)
  378. {
  379. if (is_array($constantValues)) {
  380. if (isset($filter)) {
  381. if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
  382. foreach ($filter as $val) {
  383. if (!is_callable($val)) {
  384. throw new \Exception("Callback function does not exist in QuickForm::setConstants()");
  385. } else {
  386. $constantValues = $this->_recursiveFilter($val, $constantValues);
  387. }
  388. }
  389. } elseif (!is_callable($filter)) {
  390. throw new \Exception("Callback function does not exist in QuickForm::setConstants()");
  391. } else {
  392. $constantValues = $this->_recursiveFilter($filter, $constantValues);
  393. }
  394. }
  395. $this->_constantValues = HTML_QuickForm::arrayMerge($this->_constantValues, $constantValues);
  396. foreach (array_keys($this->_elements) as $key) {
  397. $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
  398. }
  399. }
  400. } // end func setConstants
  401. // }}}
  402. // {{{ setMaxFileSize()
  403. /**
  404. * Sets the value of MAX_FILE_SIZE hidden element
  405. *
  406. * @param int $bytes Size in bytes
  407. * @since 3.0
  408. * @access public
  409. * @return void
  410. */
  411. public function setMaxFileSize($bytes = 0)
  412. {
  413. if ($bytes > 0) {
  414. $this->_maxFileSize = $bytes;
  415. }
  416. if (!$this->elementExists('MAX_FILE_SIZE')) {
  417. $this->addElement('hidden', 'MAX_FILE_SIZE', $this->_maxFileSize);
  418. } else {
  419. $el =& $this->getElement('MAX_FILE_SIZE');
  420. $el->updateAttributes(array('value' => $this->_maxFileSize));
  421. }
  422. } // end func setMaxFileSize
  423. // }}}
  424. // {{{ getMaxFileSize()
  425. /**
  426. * Returns the value of MAX_FILE_SIZE hidden element
  427. *
  428. * @since 3.0
  429. * @access public
  430. * @return int max file size in bytes
  431. */
  432. public function getMaxFileSize()
  433. {
  434. return $this->_maxFileSize;
  435. } // end func getMaxFileSize
  436. // }}}
  437. // {{{ &createElement()
  438. /**
  439. * Creates a new form element of the given type.
  440. *
  441. * This method accepts variable number of parameters, their
  442. * meaning and count depending on $elementType
  443. *
  444. * @param string $elementType type of element to add (text, textarea, file...)
  445. * @since 1.0
  446. * @access public
  447. * @return HTML_QuickForm_Element
  448. * @throws HTML_QuickForm_Error
  449. */
  450. public function &createElement($elementType)
  451. {
  452. $args = func_get_args();
  453. $element = HTML_QuickForm::_loadElement('createElement', $elementType, array_slice($args, 1));
  454. return $element;
  455. } // end func createElement
  456. // }}}
  457. // {{{ _loadElement()
  458. /**
  459. * Returns a form element of the given type
  460. *
  461. * @param string $event event to send to newly created element ('createElement' or 'addElement')
  462. * @param string $type element type
  463. * @param array $args arguments for event
  464. * @since 2.0
  465. * @access private
  466. * @return HTML_QuickForm_Element
  467. * @throws HTML_QuickForm_Error
  468. */
  469. public function &_loadElement($event, $type, $args)
  470. {
  471. $className = null;
  472. // Try if class exists
  473. if (!class_exists($type)) {
  474. // Try classic class name HTML_QuickForm_
  475. $className = 'HTML_QuickForm_'.$type;
  476. if (!class_exists($className)) {
  477. // Try classic class name HTML_QuickForm_ with strtolower
  478. $lowerType = strtolower($type);
  479. $className = 'HTML_QuickForm_'.$lowerType;
  480. if (!class_exists($className)) {
  481. // Try new class name CamelCase
  482. $className = underScoreToCamelCase($type);
  483. if (!class_exists($className)) {
  484. throw new \Exception("Class '$className' does not exist. ");
  485. }
  486. }
  487. }
  488. } else {
  489. $className = $type;
  490. }
  491. if (empty($className)) {
  492. throw new \Exception("Class '$className' does not exist. ");
  493. }
  494. for ($i = 0; $i < self::MAX_ELEMENT_ARGUMENT; $i++) {
  495. if (!isset($args[$i])) {
  496. $args[$i] = null;
  497. }
  498. }
  499. /** @var HTML_QuickForm_element $element */
  500. $element = new $className(
  501. $args[0],
  502. $args[1],
  503. $args[2],
  504. $args[3],
  505. $args[4],
  506. $args[5],
  507. $args[6],
  508. $args[7],
  509. $args[8],
  510. $args[9]
  511. );
  512. if ($event != 'createElement') {
  513. $err = $element->onQuickFormEvent($event, $args, $this);
  514. if ($err !== true) {
  515. return $err;
  516. }
  517. }
  518. return $element;
  519. }
  520. // }}}
  521. // {{{ addElement()
  522. /**
  523. * Adds an element into the form
  524. *
  525. * If $element is a string representing element type, then this
  526. * method accepts variable number of parameters, their meaning
  527. * and count depending on $element
  528. *
  529. * @param mixed $element element object or type of element to add (text, textarea, file...)
  530. * @since 1.0
  531. * @return HTML_QuickForm_element a reference to newly added element
  532. * @access public
  533. * @throws HTML_QuickForm_Error
  534. */
  535. public function &addElement($element)
  536. {
  537. if (is_object($element) && is_subclass_of($element, 'html_quickform_element')) {
  538. $elementObject = &$element;
  539. $elementObject->onQuickFormEvent('updateValue', null, $this);
  540. } else {
  541. $args = func_get_args();
  542. $elementObject =& $this->_loadElement('addElement', $element, array_slice($args, 1));
  543. if (PEAR::isError($elementObject)) {
  544. return $elementObject;
  545. }
  546. }
  547. $elementName = $elementObject->getName();
  548. // Add the element if it is not an incompatible duplicate
  549. if (!empty($elementName) &&
  550. isset($this->_elementIndex[$elementName])
  551. ) {
  552. if ($this->_elements[$this->_elementIndex[$elementName]]->getType() == $elementObject->getType()) {
  553. $this->_elements[] =& $elementObject;
  554. $elKeys = array_keys($this->_elements);
  555. $this->_duplicateIndex[$elementName][] = end($elKeys);
  556. } else {
  557. throw new \Exception("Element '$elementName' already exists in HTML_QuickForm::addElement()");
  558. }
  559. } else {
  560. $this->_elements[] =& $elementObject;
  561. $elKeys = array_keys($this->_elements);
  562. $this->_elementIndex[$elementName] = end($elKeys);
  563. }
  564. if ($this->_freezeAll) {
  565. $elementObject->freeze();
  566. }
  567. return $elementObject;
  568. }
  569. public function getElements()
  570. {
  571. return $this->_elements;
  572. }
  573. /**
  574. * Inserts a new element right before the other element
  575. *
  576. * Warning: it is not possible to check whether the $element is already
  577. * added to the form, therefore if you want to move the existing form
  578. * element to a new position, you'll have to use removeElement():
  579. * $form->insertElementBefore($form->removeElement('foo', false), 'bar');
  580. *
  581. * @access public
  582. * @since 3.2.4
  583. * @param HTML_QuickForm_element Element to insert
  584. * @param string Name of the element before which the new
  585. * one is inserted
  586. * @return HTML_QuickForm_element reference to inserted element
  587. */
  588. public function &insertElementBefore(&$element, $nameAfter)
  589. {
  590. if (!empty($this->_duplicateIndex[$nameAfter])) {
  591. throw new \Exception('Several elements named "' . $nameAfter . '" exist in HTML_QuickForm::insertElementBefore().');
  592. } elseif (!$this->elementExists($nameAfter)) {
  593. throw new \Exception("Element '$nameAfter' does not exist in HTML_QuickForm::insertElementBefore()");
  594. }
  595. $elementName = $element->getName();
  596. $targetIdx = $this->_elementIndex[$nameAfter];
  597. $duplicate = false;
  598. // Like in addElement(), check that it's not an incompatible duplicate
  599. if (!empty($elementName) && isset($this->_elementIndex[$elementName])) {
  600. if ($this->_elements[$this->_elementIndex[$elementName]]->getType() != $element->getType()) {
  601. throw new \Exception("Element '$elementName' already exists in HTML_QuickForm::insertElementBefore()");
  602. }
  603. $duplicate = true;
  604. }
  605. // Move all the elements after added back one place, reindex _elementIndex and/or _duplicateIndex
  606. $elKeys = array_keys($this->_elements);
  607. for ($i = end($elKeys); $i >= $targetIdx; $i--) {
  608. if (isset($this->_elements[$i])) {
  609. $currentName = $this->_elements[$i]->getName();
  610. $this->_elements[$i + 1] =& $this->_elements[$i];
  611. if ($this->_elementIndex[$currentName] == $i) {
  612. $this->_elementIndex[$currentName] = $i + 1;
  613. } else {
  614. $dupIdx = array_search($i, $this->_duplicateIndex[$currentName]);
  615. $this->_duplicateIndex[$currentName][$dupIdx] = $i + 1;
  616. }
  617. unset($this->_elements[$i]);
  618. }
  619. }
  620. // Put the element in place finally
  621. $this->_elements[$targetIdx] =& $element;
  622. if (!$duplicate) {
  623. $this->_elementIndex[$elementName] = $targetIdx;
  624. } else {
  625. $this->_duplicateIndex[$elementName][] = $targetIdx;
  626. }
  627. $element->onQuickFormEvent('updateValue', null, $this);
  628. if ($this->_freezeAll) {
  629. $element->freeze();
  630. }
  631. // If not done, the elements will appear in reverse order
  632. ksort($this->_elements);
  633. return $element;
  634. }
  635. // }}}
  636. // {{{ addGroup()
  637. /**
  638. * Adds an element group
  639. * @param array $elements array of elements composing the group
  640. * @param string $name (optional)group name
  641. * @param string $groupLabel (optional)group label
  642. * @param string $separator (optional)string to separate elements
  643. * @param string $appendName (optional)specify whether the group name should be
  644. * used in the form element name ex: group[element]
  645. * @return HTML_QuickForm_group reference to a newly added group
  646. * @since 2.8
  647. * @access public
  648. * @throws HTML_QuickForm_Error
  649. */
  650. public function &addGroup($elements, $name=null, $groupLabel='', $separator=null, $appendName = true)
  651. {
  652. static $anonGroups = 1;
  653. if (0 == strlen($name)) {
  654. $name = 'qf_group_' . $anonGroups++;
  655. $appendName = false;
  656. }
  657. $group = & $this->addElement('group', $name, $groupLabel, $elements, $separator, $appendName);
  658. return $group;
  659. } // end func addGroup
  660. // }}}
  661. // {{{ &getElement()
  662. /**
  663. * Returns a reference to the element
  664. *
  665. * @param string $element Element name
  666. * @since 2.0
  667. * @access public
  668. * @return HTML_QuickForm_element reference to element
  669. * @throws HTML_QuickForm_Error
  670. */
  671. public function &getElement($element)
  672. {
  673. if (isset($this->_elementIndex[$element])) {
  674. return $this->_elements[$this->_elementIndex[$element]];
  675. } else {
  676. throw new \Exception("Element '$element' does not exist in HTML_QuickForm::getElement()");
  677. }
  678. }
  679. /**
  680. * Returns the element's raw value
  681. *
  682. * This returns the value as submitted by the form (not filtered)
  683. * or set via setDefaults() or setConstants()
  684. *
  685. * @param string $element Element name
  686. * @since 2.0
  687. * @access public
  688. * @return mixed element value
  689. * @throws HTML_QuickForm_Error
  690. */
  691. function &getElementValue($element)
  692. {
  693. if (!isset($this->_elementIndex[$element])) {
  694. throw new \Exception("Element '$element' does not exist in HTML_QuickForm::getElementValue()");
  695. }
  696. $value = $this->_elements[$this->_elementIndex[$element]]->getValue();
  697. if (isset($this->_duplicateIndex[$element])) {
  698. foreach ($this->_duplicateIndex[$element] as $index) {
  699. if (null !== ($v = $this->_elements[$index]->getValue())) {
  700. if (is_array($value)) {
  701. $value[] = $v;
  702. } else {
  703. $value = (null === $value)? $v: array($value, $v);
  704. }
  705. }
  706. }
  707. }
  708. return $value;
  709. }
  710. /**
  711. * Returns the elements value after submit and filter
  712. *
  713. * @param string Element name
  714. * @since 2.0
  715. * @access public
  716. * @return mixed submitted element value or null if not set
  717. */
  718. function getSubmitValue($elementName)
  719. {
  720. $value = null;
  721. if (isset($this->_submitValues[$elementName]) || isset($this->_submitFiles[$elementName])) {
  722. $value = isset($this->_submitValues[$elementName])? $this->_submitValues[$elementName]: array();
  723. if (is_array($value) && isset($this->_submitFiles[$elementName])) {
  724. foreach ($this->_submitFiles[$elementName] as $k => $v) {
  725. $value = HTML_QuickForm::arrayMerge(
  726. $value,
  727. $this->_reindexFiles($this->_submitFiles[$elementName][$k], $k)
  728. );
  729. }
  730. }
  731. } elseif ('file' == $this->getElementType($elementName)) {
  732. return $this->getElementValue($elementName);
  733. } elseif (false !== ($pos = strpos($elementName, '['))) {
  734. $base = str_replace(
  735. array('\\', '\''), array('\\\\', '\\\''),
  736. substr($elementName, 0, $pos)
  737. );
  738. $idx = "['" . str_replace(
  739. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  740. substr($elementName, $pos + 1, -1)
  741. ) . "']";
  742. if (isset($this->_submitValues[$base])) {
  743. $value = eval("return (isset(\$this->_submitValues['{$base}']{$idx})) ? \$this->_submitValues['{$base}']{$idx} : null;");
  744. }
  745. if ((is_array($value) || null === $value) && isset($this->_submitFiles[$base])) {
  746. $props = array('name', 'type', 'size', 'tmp_name', 'error');
  747. $code = "if (!isset(\$this->_submitFiles['{$base}']['name']{$idx})) {\n" .
  748. " return null;\n" .
  749. "} else {\n" .
  750. " \$v = array();\n";
  751. foreach ($props as $prop) {
  752. $code .= " \$v = HTML_QuickForm::arrayMerge(\$v, \$this->_reindexFiles(\$this->_submitFiles['{$base}']['{$prop}']{$idx}, '{$prop}'));\n";
  753. }
  754. $fileValue = eval($code . " return \$v;\n}\n");
  755. if (null !== $fileValue) {
  756. $value = null === $value? $fileValue: HTML_QuickForm::arrayMerge($value, $fileValue);
  757. }
  758. }
  759. }
  760. // This is only supposed to work for groups with appendName = false
  761. if (null === $value && 'group' == $this->getElementType($elementName)) {
  762. $group =& $this->getElement($elementName);
  763. $elements =& $group->getElements();
  764. foreach (array_keys($elements) as $key) {
  765. $name = $group->getElementName($key);
  766. // prevent endless recursion in case of radios and such
  767. if ($name != $elementName) {
  768. if (null !== ($v = $this->getSubmitValue($name))) {
  769. $value[$name] = $v;
  770. }
  771. }
  772. }
  773. }
  774. if ($this->getElementType($elementName) == 'date_range_picker') {
  775. /** @var DateRangePicker $element */
  776. $element = $this->getElement($elementName);
  777. $parsedDates = $element->parseDateRange($value);
  778. $validateFormat = $element->getAttribute('validate_format');
  779. if (!$element->validateDates($parsedDates, $validateFormat)) {
  780. $this->_errors[$elementName] = get_lang('CheckDates');
  781. }
  782. $this->_submitValues[$elementName.'_start'] = $parsedDates['start'];
  783. $this->_submitValues[$elementName.'_end'] = $parsedDates['end'];
  784. }
  785. return $value;
  786. }
  787. /**
  788. * A helper function to change the indexes in $_FILES array
  789. *
  790. * @param mixed Some value from the $_FILES array
  791. * @param string The key from the $_FILES array that should be appended
  792. * @return array
  793. */
  794. function _reindexFiles($value, $key)
  795. {
  796. if (!is_array($value)) {
  797. return array($key => $value);
  798. } else {
  799. $ret = array();
  800. foreach ($value as $k => $v) {
  801. $ret[$k] = $this->_reindexFiles($v, $key);
  802. }
  803. return $ret;
  804. }
  805. }
  806. // }}}
  807. // {{{ getElementError()
  808. /**
  809. * Returns error corresponding to validated element
  810. *
  811. * @param string $element Name of form element to check
  812. * @since 1.0
  813. * @access public
  814. * @return string error message corresponding to checked element
  815. */
  816. function getElementError($element)
  817. {
  818. if (isset($this->_errors[$element])) {
  819. return $this->_errors[$element];
  820. }
  821. } // end func getElementError
  822. // }}}
  823. // {{{ setElementError()
  824. /**
  825. * Set error message for a form element
  826. *
  827. * @param string $element Name of form element to set error for
  828. * @param string $message Error message, if empty then removes the current error message
  829. * @since 1.0
  830. * @access public
  831. * @return void
  832. */
  833. function setElementError($element, $message = null)
  834. {
  835. if (!empty($message)) {
  836. $this->_errors[$element] = $message;
  837. } else {
  838. unset($this->_errors[$element]);
  839. }
  840. } // end func setElementError
  841. // }}}
  842. // {{{ getElementType()
  843. /**
  844. * Returns the type of the given element
  845. *
  846. * @param string $element Name of form element
  847. * @since 1.1
  848. * @access public
  849. * @return string Type of the element, false if the element is not found
  850. */
  851. function getElementType($element)
  852. {
  853. if (isset($this->_elementIndex[$element])) {
  854. return $this->_elements[$this->_elementIndex[$element]]->getType();
  855. }
  856. return false;
  857. } // end func getElementType
  858. // }}}
  859. // {{{ updateElementAttr()
  860. /**
  861. * Updates Attributes for one or more elements
  862. *
  863. * @param mixed $elements Array of element names/objects or string of elements to be updated
  864. * @param mixed $attrs Array or sting of html attributes
  865. * @since 2.10
  866. * @access public
  867. * @return void
  868. */
  869. function updateElementAttr($elements, $attrs)
  870. {
  871. if (is_string($elements)) {
  872. $elements = split('[ ]?,[ ]?', $elements);
  873. }
  874. foreach (array_keys($elements) as $key) {
  875. if (is_object($elements[$key]) && is_a($elements[$key], 'HTML_QuickForm_element')) {
  876. $elements[$key]->updateAttributes($attrs);
  877. } elseif (isset($this->_elementIndex[$elements[$key]])) {
  878. $this->_elements[$this->_elementIndex[$elements[$key]]]->updateAttributes($attrs);
  879. if (isset($this->_duplicateIndex[$elements[$key]])) {
  880. foreach ($this->_duplicateIndex[$elements[$key]] as $index) {
  881. $this->_elements[$index]->updateAttributes($attrs);
  882. }
  883. }
  884. }
  885. }
  886. } // end func updateElementAttr
  887. // }}}
  888. // {{{ removeElement()
  889. /**
  890. * Removes an element
  891. *
  892. * The method "unlinks" an element from the form, returning the reference
  893. * to the element object. If several elements named $elementName exist,
  894. * it removes the first one, leaving the others intact.
  895. *
  896. * @param string $elementName The element name
  897. * @param boolean $removeRules True if rules for this element are to be removed too
  898. * @access public
  899. * @since 2.0
  900. * @return HTML_QuickForm_element a reference to the removed element
  901. * @throws HTML_QuickForm_Error
  902. */
  903. function &removeElement($elementName, $removeRules = true)
  904. {
  905. if (!isset($this->_elementIndex[$elementName])) {
  906. throw new \Exception("Element '$elementName' does not exist in HTML_QuickForm::removeElement()");
  907. }
  908. $el =& $this->_elements[$this->_elementIndex[$elementName]];
  909. unset($this->_elements[$this->_elementIndex[$elementName]]);
  910. if (empty($this->_duplicateIndex[$elementName])) {
  911. unset($this->_elementIndex[$elementName]);
  912. } else {
  913. $this->_elementIndex[$elementName] = array_shift($this->_duplicateIndex[$elementName]);
  914. }
  915. if ($removeRules) {
  916. $this->_required = array_diff($this->_required, array($elementName));
  917. unset($this->_rules[$elementName], $this->_errors[$elementName]);
  918. if ('group' == $el->getType()) {
  919. foreach (array_keys($el->getElements()) as $key) {
  920. unset($this->_rules[$el->getElementName($key)]);
  921. }
  922. }
  923. }
  924. return $el;
  925. }
  926. /**
  927. * Adds a validation rule for the given field
  928. *
  929. * If the element is in fact a group, it will be considered as a whole.
  930. * To validate grouped elements as separated entities,
  931. * use addGroupRule instead of addRule.
  932. *
  933. * @param string $element Form element name
  934. * @param string $message Message to display for invalid data
  935. * @param string $type Rule type, use getRegisteredRules() to get types
  936. * @param string $format (optional)Required for extra rule data
  937. * @param string $validation (optional)Where to perform validation: "server", "client"
  938. * @param boolean $reset Client-side validation: reset the form element to its original value if there is an error?
  939. * @param boolean $force Force the rule to be applied, even if the target form element does not exist
  940. * @since 1.0
  941. * @access public
  942. */
  943. public function addRule(
  944. $element,
  945. $message,
  946. $type,
  947. $format = null,
  948. $validation = 'server',
  949. $reset = false,
  950. $force = false
  951. ) {
  952. if (!$force) {
  953. if (!is_array($element) && !$this->elementExists($element)) {
  954. throw new \Exception("Element '$element' does not exist in HTML_QuickForm::addRule()");
  955. } elseif (is_array($element)) {
  956. foreach ($element as $el) {
  957. if (!$this->elementExists($el)) {
  958. throw new \Exception("Element '$el' does not exist in HTML_QuickForm::addRule()");
  959. }
  960. }
  961. }
  962. }
  963. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  964. throw new \Exception("Rule '$type' is not registered in HTML_QuickForm::addRule()");
  965. } elseif (is_string($newName)) {
  966. $type = $newName;
  967. }
  968. if (is_array($element)) {
  969. $dependent = $element;
  970. $element = array_shift($dependent);
  971. } else {
  972. $dependent = null;
  973. }
  974. if ($type == 'required' || $type == 'uploadedfile') {
  975. $this->_required[] = $element;
  976. }
  977. if (!isset($this->_rules[$element])) {
  978. $this->_rules[$element] = array();
  979. }
  980. if ($validation == 'client') {
  981. $this->updateAttributes(
  982. array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);')
  983. );
  984. }
  985. $this->_rules[$element][] = array(
  986. 'type' => $type,
  987. 'format' => $format,
  988. 'message' => $message,
  989. 'validation' => $validation,
  990. 'reset' => $reset,
  991. 'dependent' => $dependent,
  992. );
  993. }
  994. /**
  995. * Adds a validation rule for the given group of elements
  996. *
  997. * Only groups with a name can be assigned a validation rule
  998. * Use addGroupRule when you need to validate elements inside the group.
  999. * Use addRule if you need to validate the group as a whole. In this case,
  1000. * the same rule will be applied to all elements in the group.
  1001. * Use addRule if you need to validate the group against a function.
  1002. *
  1003. * @param string $group Form group name
  1004. * @param mixed $arg1 Array for multiple elements or error message string for one element
  1005. * @param string $type (optional)Rule type use getRegisteredRules() to get types
  1006. * @param string $format (optional)Required for extra rule data
  1007. * @param int $howmany (optional)How many valid elements should be in the group
  1008. * @param string $validation (optional)Where to perform validation: "server", "client"
  1009. * @param bool $reset Client-side: whether to reset the element's value to its original state if validation failed.
  1010. * @since 2.5
  1011. * @access public
  1012. * @throws HTML_QuickForm_Error
  1013. */
  1014. function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false)
  1015. {
  1016. if (!$this->elementExists($group)) {
  1017. throw new \Exception("Group '$group' does not exist in HTML_QuickForm::addGroupRule()");
  1018. }
  1019. $groupObj =& $this->getElement($group);
  1020. if (is_array($arg1)) {
  1021. $required = 0;
  1022. foreach ($arg1 as $elementIndex => $rules) {
  1023. $elementName = $groupObj->getElementName($elementIndex);
  1024. foreach ($rules as $rule) {
  1025. $format = (isset($rule[2])) ? $rule[2] : null;
  1026. $validation = (isset($rule[3]) && 'client' == $rule[3])? 'client': 'server';
  1027. $reset = isset($rule[4]) && $rule[4];
  1028. $type = $rule[1];
  1029. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  1030. throw new \Exception("Rule '$type' is not registered in HTML_QuickForm::addGroupRule()");
  1031. } elseif (is_string($newName)) {
  1032. $type = $newName;
  1033. }
  1034. $this->_rules[$elementName][] = array(
  1035. 'type' => $type,
  1036. 'format' => $format,
  1037. 'message' => $rule[0],
  1038. 'validation' => $validation,
  1039. 'reset' => $reset,
  1040. 'group' => $group,
  1041. );
  1042. if ('required' == $type || 'uploadedfile' == $type) {
  1043. $groupObj->_required[] = $elementName;
  1044. $this->_required[] = $elementName;
  1045. $required++;
  1046. }
  1047. if ('client' == $validation) {
  1048. $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);'));
  1049. }
  1050. }
  1051. }
  1052. if ($required > 0 && count($groupObj->getElements()) == $required) {
  1053. $this->_required[] = $group;
  1054. }
  1055. } elseif (is_string($arg1)) {
  1056. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  1057. throw new \Exception("Rule '$type' is not registered in HTML_QuickForm::addGroupRule()");
  1058. } elseif (is_string($newName)) {
  1059. $type = $newName;
  1060. }
  1061. // addGroupRule() should also handle <select multiple>
  1062. if (is_a($groupObj, 'html_quickform_group')) {
  1063. // Radios need to be handled differently when required
  1064. if ($type == 'required' && $groupObj->getGroupType() == 'radio') {
  1065. $howmany = ($howmany == 0) ? 1 : $howmany;
  1066. } else {
  1067. $howmany = ($howmany == 0) ? count($groupObj->getElements()) : $howmany;
  1068. }
  1069. }
  1070. $this->_rules[$group][] = array(
  1071. 'type' => $type,
  1072. 'format' => $format,
  1073. 'message' => $arg1,
  1074. 'validation' => $validation,
  1075. 'howmany' => $howmany,
  1076. 'reset' => $reset,
  1077. );
  1078. if ($type == 'required') {
  1079. $this->_required[] = $group;
  1080. }
  1081. if ($validation == 'client') {
  1082. $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);'));
  1083. }
  1084. }
  1085. } // end func addGroupRule
  1086. // }}}
  1087. // {{{ addFormRule()
  1088. /**
  1089. * Adds a global validation rule
  1090. *
  1091. * This should be used when for a rule involving several fields or if
  1092. * you want to use some completely custom validation for your form.
  1093. * The rule function/method should return true in case of successful
  1094. * validation and array('element name' => 'error') when there were errors.
  1095. *
  1096. * @access public
  1097. * @param mixed Callback, either function name or array(&$object, 'method')
  1098. * @throws HTML_QuickForm_Error
  1099. */
  1100. public function addFormRule($rule)
  1101. {
  1102. if (!is_callable($rule)) {
  1103. throw new \Exception('Callback function does not exist in HTML_QuickForm::addFormRule()');
  1104. }
  1105. $this->_formRules[] = $rule;
  1106. }
  1107. // }}}
  1108. // {{{ applyFilter()
  1109. /**
  1110. * Applies a data filter for the given field(s)
  1111. *
  1112. * @param mixed $element Form element name or array of such names
  1113. * @param mixed $filter Callback, either function name or array(&$object, 'method')
  1114. * @since 2.0
  1115. * @access public
  1116. * @throws HTML_QuickForm_Error
  1117. */
  1118. function applyFilter($element, $filter)
  1119. {
  1120. if (!is_callable($filter)) {
  1121. throw new \Exception("Callback function does not exist in QuickForm::applyFilter()");
  1122. }
  1123. if ($element == '__ALL__') {
  1124. $this->_submitValues = $this->_recursiveFilter($filter, $this->_submitValues);
  1125. } else {
  1126. if (!is_array($element)) {
  1127. $element = array($element);
  1128. }
  1129. foreach ($element as $elName) {
  1130. $value = $this->getSubmitValue($elName);
  1131. if (null !== $value) {
  1132. if (false === strpos($elName, '[')) {
  1133. $this->_submitValues[$elName] = $this->_recursiveFilter($filter, $value);
  1134. } else {
  1135. $idx = "['" . str_replace(
  1136. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  1137. $elName
  1138. ) . "']";
  1139. eval("\$this->_submitValues{$idx} = \$this->_recursiveFilter(\$filter, \$value);");
  1140. }
  1141. }
  1142. }
  1143. }
  1144. } // end func applyFilter
  1145. // }}}
  1146. // {{{ _recursiveFilter()
  1147. /**
  1148. * Recursively apply a filter function
  1149. *
  1150. * @param string $filter filter to apply
  1151. * @param mixed $value submitted values
  1152. * @since 2.0
  1153. * @access private
  1154. * @return cleaned values
  1155. */
  1156. function _recursiveFilter($filter, $value)
  1157. {
  1158. if (is_array($value)) {
  1159. $cleanValues = array();
  1160. foreach ($value as $k => $v) {
  1161. $cleanValues[$k] = $this->_recursiveFilter($filter, $v);
  1162. }
  1163. return $cleanValues;
  1164. } else {
  1165. return call_user_func($filter, $value);
  1166. }
  1167. } // end func _recursiveFilter
  1168. // }}}
  1169. // {{{ arrayMerge()
  1170. /**
  1171. * Merges two arrays
  1172. *
  1173. * Merges two array like the PHP function array_merge but recursively.
  1174. * The main difference is that existing keys will not be renumbered
  1175. * if they are integers.
  1176. *
  1177. * @access public
  1178. * @param array $a original array
  1179. * @param array $b array which will be merged into first one
  1180. * @return array merged array
  1181. */
  1182. static function arrayMerge($a, $b)
  1183. {
  1184. foreach ($b as $k => $v) {
  1185. if (is_array($v)) {
  1186. if (isset($a[$k]) && !is_array($a[$k])) {
  1187. $a[$k] = $v;
  1188. } else {
  1189. if (!isset($a[$k])) {
  1190. $a[$k] = array();
  1191. }
  1192. $a[$k] = HTML_QuickForm::arrayMerge($a[$k], $v);
  1193. }
  1194. } else {
  1195. $a[$k] = $v;
  1196. }
  1197. }
  1198. return $a;
  1199. } // end func arrayMerge
  1200. // }}}
  1201. // {{{ isTypeRegistered()
  1202. /**
  1203. * Returns whether or not the form element type is supported
  1204. *
  1205. * @param string $type Form element type
  1206. * @since 1.0
  1207. * @access public
  1208. * @return boolean
  1209. */
  1210. static function isTypeRegistered($type)
  1211. {
  1212. return isset($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][strtolower($type)]);
  1213. }
  1214. /**
  1215. * Returns an array of registered element types
  1216. *
  1217. * @since 1.0
  1218. * @access public
  1219. * @return array
  1220. */
  1221. function getRegisteredTypes()
  1222. {
  1223. return array_keys($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']);
  1224. } // end func getRegisteredTypes
  1225. // }}}
  1226. // {{{ isRuleRegistered()
  1227. /**
  1228. * Returns whether or not the given rule is supported
  1229. *
  1230. * @param string $name Validation rule name
  1231. * @param bool Whether to automatically register subclasses of HTML_QuickForm_Rule
  1232. * @since 1.0
  1233. * @access public
  1234. * @return mixed true if previously registered, false if not, new rule name if auto-registering worked
  1235. */
  1236. function isRuleRegistered($name, $autoRegister = false)
  1237. {
  1238. return true;
  1239. if (is_scalar($name) && isset($GLOBALS['_HTML_QuickForm_registered_rules'][$name])) {
  1240. return true;
  1241. } elseif (!$autoRegister) {
  1242. return false;
  1243. }
  1244. $ruleName = false;
  1245. if (is_object($name) && is_a($name, 'html_quickform_rule')) {
  1246. $ruleName = !empty($name->name)? $name->name: strtolower(get_class($name));
  1247. } elseif (is_string($name) && class_exists($name)) {
  1248. $parent = strtolower($name);
  1249. do {
  1250. if ('html_quickform_rule' == strtolower($parent)) {
  1251. $ruleName = strtolower($name);
  1252. break;
  1253. }
  1254. } while ($parent = get_parent_class($parent));
  1255. }
  1256. if ($ruleName) {
  1257. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  1258. $registry->registerRule($ruleName, null, $name);
  1259. }
  1260. return $ruleName;
  1261. } // end func isRuleRegistered
  1262. // }}}
  1263. // {{{ getRegisteredRules()
  1264. /**
  1265. * Returns an array of registered validation rules
  1266. *
  1267. * @since 1.0
  1268. * @access public
  1269. * @return array
  1270. */
  1271. function getRegisteredRules()
  1272. {
  1273. return array_keys($GLOBALS['_HTML_QuickForm_registered_rules']);
  1274. } // end func getRegisteredRules
  1275. // }}}
  1276. // {{{ isElementRequired()
  1277. /**
  1278. * Returns whether or not the form element is required
  1279. *
  1280. * @param string $element Form element name
  1281. * @since 1.0
  1282. * @access public
  1283. * @return boolean
  1284. */
  1285. function isElementRequired($element)
  1286. {
  1287. return in_array($element, $this->_required, true);
  1288. } // end func isElementRequired
  1289. // }}}
  1290. // {{{ isElementFrozen()
  1291. /**
  1292. * Returns whether or not the form element is frozen
  1293. *
  1294. * @param string $element Form element name
  1295. * @since 1.0
  1296. * @access public
  1297. * @return boolean
  1298. */
  1299. function isElementFrozen($element)
  1300. {
  1301. if (isset($this->_elementIndex[$element])) {
  1302. return $this->_elements[$this->_elementIndex[$element]]->isFrozen();
  1303. }
  1304. return false;
  1305. } // end func isElementFrozen
  1306. // }}}
  1307. // {{{ setJsWarnings()
  1308. /**
  1309. * Sets JavaScript warning messages
  1310. *
  1311. * @param string $pref Prefix warning
  1312. * @param string $post Postfix warning
  1313. * @since 1.1
  1314. * @access public
  1315. * @return void
  1316. */
  1317. function setJsWarnings($pref, $post)
  1318. {
  1319. $this->_jsPrefix = $pref;
  1320. $this->_jsPostfix = $post;
  1321. } // end func setJsWarnings
  1322. // }}}
  1323. // {{{ setRequiredNote()
  1324. /**
  1325. * Sets required-note
  1326. *
  1327. * @param string $note Message indicating some elements are required
  1328. * @since 1.1
  1329. * @access public
  1330. * @return void
  1331. */
  1332. function setRequiredNote($note)
  1333. {
  1334. $this->_requiredNote = $note;
  1335. } // end func setRequiredNote
  1336. // }}}
  1337. // {{{ getRequiredNote()
  1338. /**
  1339. * Returns the required note
  1340. *
  1341. * @since 2.0
  1342. * @access public
  1343. * @return string
  1344. */
  1345. function getRequiredNote()
  1346. {
  1347. return $this->_requiredNote;
  1348. }
  1349. /**
  1350. * Performs the server side validation
  1351. * @access public
  1352. * @since 1.0
  1353. * @return boolean true if no error found
  1354. * @throws HTML_QuickForm_Error
  1355. */
  1356. public function validate()
  1357. {
  1358. if (count($this->_rules) == 0 && count($this->_formRules) == 0 &&
  1359. $this->isSubmitted()) {
  1360. return (0 == count($this->_errors));
  1361. } elseif (!$this->isSubmitted()) {
  1362. return false;
  1363. }
  1364. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  1365. foreach ($this->_rules as $target => $rules) {
  1366. $submitValue = $this->getSubmitValue($target);
  1367. foreach ($rules as $rule) {
  1368. if ((isset($rule['group']) && isset($this->_errors[$rule['group']])) ||
  1369. isset($this->_errors[$target])) {
  1370. continue 2;
  1371. }
  1372. // If element is not required and is empty, we shouldn't validate it
  1373. if (!$this->isElementRequired($target)) {
  1374. if (!isset($submitValue) || '' == $submitValue) {
  1375. continue 2;
  1376. // Fix for bug #3501: we shouldn't validate not uploaded files, either.
  1377. // Unfortunately, we can't just use $element->isUploadedFile() since
  1378. // the element in question can be buried in group. Thus this hack.
  1379. // See also bug #12014, we should only consider a file that has
  1380. // status UPLOAD_ERR_NO_FILE as not uploaded, in all other cases
  1381. // validation should be performed, so that e.g. 'maxfilesize' rule
  1382. // will display an error if status is UPLOAD_ERR_INI_SIZE
  1383. // or UPLOAD_ERR_FORM_SIZE
  1384. } elseif (is_array($submitValue)) {
  1385. if (false === ($pos = strpos($target, '['))) {
  1386. $isUpload = !empty($this->_submitFiles[$target]);
  1387. } else {
  1388. $base = str_replace(
  1389. array('\\', '\''), array('\\\\', '\\\''),
  1390. substr($target, 0, $pos)
  1391. );
  1392. $idx = "['" . str_replace(
  1393. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  1394. substr($target, $pos + 1, -1)
  1395. ) . "']";
  1396. eval("\$isUpload = isset(\$this->_submitFiles['{$base}']['name']{$idx});");
  1397. }
  1398. if ($isUpload && (!isset($submitValue['error']) || UPLOAD_ERR_NO_FILE == $submitValue['error'])) {
  1399. continue 2;
  1400. }
  1401. }
  1402. }
  1403. if (isset($rule['dependent']) && is_array($rule['dependent'])) {
  1404. $values = array($submitValue);
  1405. foreach ($rule['dependent'] as $elName) {
  1406. $values[] = $this->getSubmitValue($elName);
  1407. }
  1408. $result = $registry->validate($rule['type'], $values, $rule['format'], true);
  1409. } elseif (is_array($submitValue) && !isset($rule['howmany'])) {
  1410. $result = $registry->validate($rule['type'], $submitValue, $rule['format'], true);
  1411. } else {
  1412. $result = $registry->validate($rule['type'], $submitValue, $rule['format'], false);
  1413. }
  1414. if (!$result || (!empty($rule['howmany']) && $rule['howmany'] > (int)$result)) {
  1415. if (isset($rule['group'])) {
  1416. $this->_errors[$rule['group']] = $rule['message'];
  1417. } else {
  1418. $this->_errors[$target] = $rule['message'];
  1419. }
  1420. }
  1421. }
  1422. }
  1423. // process the global rules now
  1424. foreach ($this->_formRules as $rule) {
  1425. if (true !== ($res = call_user_func($rule, $this->_submitValues, $this->_submitFiles))) {
  1426. if (is_array($res)) {
  1427. $this->_errors += $res;
  1428. } else {
  1429. throw new \Exception('Form rule callback returned invalid value in HTML_QuickForm::validate()');
  1430. }
  1431. }
  1432. }
  1433. return (0 == count($this->_errors));
  1434. }
  1435. /**
  1436. * Displays elements without HTML input tags
  1437. *
  1438. * @param mixed $elementList array or string of element(s) to be frozen
  1439. * @since 1.0
  1440. * @access public
  1441. * @throws HTML_QuickForm_Error
  1442. */
  1443. public function freeze($elementList = null, $setTemplateFrozen = '')
  1444. {
  1445. if (!isset($elementList)) {
  1446. $this->_freezeAll = true;
  1447. $elementList = array();
  1448. } else {
  1449. if (!is_array($elementList)) {
  1450. $elementList = preg_split('/[ ]*,[ ]*/', $elementList);
  1451. }
  1452. $elementList = array_flip($elementList);
  1453. }
  1454. foreach (array_keys($this->_elements) as $key) {
  1455. $name = $this->_elements[$key]->getName();
  1456. if ($this->_freezeAll || isset($elementList[$name])) {
  1457. if (!empty($setTemplateFrozen)) {
  1458. $this->_elements[$key]->setCustomFrozenTemplate($setTemplateFrozen);
  1459. }
  1460. $this->_elements[$key]->freeze();
  1461. unset($elementList[$name]);
  1462. }
  1463. }
  1464. if (!empty($elementList)) {
  1465. throw new \Exception("Nonexistant element(s): '" . implode("', '", array_keys($elementList)) . "' in HTML_QuickForm::freeze()");
  1466. }
  1467. return true;
  1468. }
  1469. /**
  1470. * Returns whether or not the whole form is frozen
  1471. *
  1472. * @since 3.0
  1473. * @access public
  1474. * @return boolean
  1475. */
  1476. public function isFrozen()
  1477. {
  1478. return $this->_freezeAll;
  1479. }
  1480. /**
  1481. * Performs the form data processing
  1482. *
  1483. * @param mixed $callback Callback, either function name or array(&$object, 'method')
  1484. * @param bool $mergeFiles Whether uploaded files should be processed too
  1485. * @since 1.0
  1486. * @access public
  1487. * @throws HTML_QuickForm_Error
  1488. * @return mixed Whatever value the $callback function returns
  1489. */
  1490. public function process($callback, $mergeFiles = true)
  1491. {
  1492. if (!is_callable($callback)) {
  1493. throw new \Exception("Callback function does not exist in QuickForm::process()");
  1494. }
  1495. $values = ($mergeFiles === true) ? HTML_QuickForm::arrayMerge($this->_submitValues, $this->_submitFiles) : $this->_submitValues;
  1496. return call_user_func($callback, $values);
  1497. }
  1498. /**
  1499. * Accepts a renderer
  1500. *
  1501. * @param object An HTML_QuickForm_Renderer object
  1502. * @since 3.0
  1503. * @access public
  1504. * @return void
  1505. */
  1506. public function accept(&$renderer)
  1507. {
  1508. $renderer->startForm($this);
  1509. foreach (array_keys($this->_elements) as $key) {
  1510. $element =& $this->_elements[$key];
  1511. $elementName = $element->getName();
  1512. $required = ($this->isElementRequired($elementName) && !$element->isFrozen());
  1513. $error = $this->getElementError($elementName);
  1514. $element->accept($renderer, $required, $error);
  1515. }
  1516. $renderer->finishForm($this);
  1517. }
  1518. /**
  1519. * Returns a reference to default renderer object
  1520. *
  1521. * @access public
  1522. * @since 3.0
  1523. * @return HTML_QuickForm_Renderer_Default
  1524. */
  1525. public function &defaultRenderer()
  1526. {
  1527. if (!isset($GLOBALS['_HTML_QuickForm_default_renderer'])) {
  1528. // Modified by Ivan Tcholakov, 16-MAR-2010. Suppressing a deprecation warning on PHP 5.3
  1529. //$GLOBALS['_HTML_QuickForm_default_renderer'] =& new HTML_QuickForm_Renderer_Default();
  1530. $GLOBALS['_HTML_QuickForm_default_renderer'] = new HTML_QuickForm_Renderer_Default();
  1531. }
  1532. return $GLOBALS['_HTML_QuickForm_default_renderer'];
  1533. }
  1534. /**
  1535. * Returns an HTML version of the form
  1536. *
  1537. * @param string $in_data (optional) Any extra data to insert right
  1538. * before form is rendered. Useful when using templates.
  1539. *
  1540. * @return string Html version of the form
  1541. * @since 1.0
  1542. * @access public
  1543. */
  1544. public function toHtml($in_data = null)
  1545. {
  1546. if (!is_null($in_data)) {
  1547. $this->addElement('html', $in_data);
  1548. }
  1549. $renderer =& $this->defaultRenderer();
  1550. $this->accept($renderer);
  1551. return $renderer->toHtml();
  1552. }
  1553. /**
  1554. * Returns the client side validation script
  1555. *
  1556. * @since 2.0
  1557. * @access public
  1558. * @return string Javascript to perform validation, empty string if no 'client' rules were added
  1559. */
  1560. public function getValidationScript()
  1561. {
  1562. if (empty($this->_rules) || empty($this->_attributes['onsubmit'])) {
  1563. return '';
  1564. }
  1565. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  1566. $test = array();
  1567. $js_escape = array(
  1568. "\r" => '\r',
  1569. "\n" => '\n',
  1570. "\t" => '\t',
  1571. "'" => "\\'",
  1572. '"' => '\"',
  1573. '\\' => '\\\\'
  1574. );
  1575. foreach ($this->_rules as $elementName => $rules) {
  1576. foreach ($rules as $rule) {
  1577. if ('client' == $rule['validation']) {
  1578. unset($element);
  1579. $dependent = isset($rule['dependent']) && is_array($rule['dependent']);
  1580. $rule['message'] = strtr($rule['message'], $js_escape);
  1581. if (isset($rule['group'])) {
  1582. $group =& $this->getElement($rule['group']);
  1583. // No JavaScript validation for frozen elements
  1584. if ($group->isFrozen()) {
  1585. continue 2;
  1586. }
  1587. $elements =& $group->getElements();
  1588. foreach (array_keys($elements) as $key) {
  1589. if ($elementName == $group->getElementName($key)) {
  1590. $element =& $elements[$key];
  1591. break;
  1592. }
  1593. }
  1594. } elseif ($dependent) {
  1595. $element = array();
  1596. $element[] =& $this->getElement($elementName);
  1597. foreach ($rule['dependent'] as $elName) {
  1598. $element[] =& $this->getElement($elName);
  1599. }
  1600. } else {
  1601. $element =& $this->getElement($elementName);
  1602. }
  1603. // No JavaScript validation for frozen elements
  1604. if (is_object($element) && $element->isFrozen()) {
  1605. continue 2;
  1606. } elseif (is_array($element)) {
  1607. foreach (array_keys($element) as $key) {
  1608. if ($element[$key]->isFrozen()) {
  1609. continue 3;
  1610. }
  1611. }
  1612. }
  1613. $test[] = $registry->getValidationScript($element, $elementName, $rule);
  1614. }
  1615. }
  1616. }
  1617. if (count($test) > 0) {
  1618. return
  1619. "\n<script type=\"text/javascript\">\n" .
  1620. "//<![CDATA[\n" .
  1621. "function validate_" . $this->_attributes['id'] . "(frm) {\n" .
  1622. " var value = '';\n" .
  1623. " var errFlag = new Array();\n" .
  1624. " var _qfGroups = {};\n" .
  1625. " _qfMsg = '';\n\n" .
  1626. join("\n", $test) .
  1627. "\n if (_qfMsg != '') {\n" .
  1628. " _qfMsg = '" . strtr($this->_jsPrefix, $js_escape) . "' + _qfMsg;\n" .
  1629. " _qfMsg = _qfMsg + '\\n" . strtr($this->_jsPostfix, $js_escape) . "';\n" .
  1630. " alert(_qfMsg);\n" .
  1631. " return false;\n" .
  1632. " }\n" .
  1633. " return true;\n" .
  1634. "}\n" .
  1635. "//]]>\n" .
  1636. "</script>";
  1637. }
  1638. return '';
  1639. }
  1640. /**
  1641. * Returns the values submitted by the form
  1642. *
  1643. * @since 2.0
  1644. * @access public
  1645. * @param bool Whether uploaded files should be returned too
  1646. * @return array
  1647. */
  1648. public function getSubmitValues($mergeFiles = false)
  1649. {
  1650. return $mergeFiles? HTML_QuickForm::arrayMerge($this->_submitValues, $this->_submitFiles): $this->_submitValues;
  1651. }
  1652. /**
  1653. * Returns the form's contents in an array.
  1654. *
  1655. * The description of the array structure is in HTML_QuickForm_Renderer_Array docs
  1656. *
  1657. * @since 2.0
  1658. * @access public
  1659. * @param bool Whether to collect hidden elements (passed to the Renderer's constructor)
  1660. * @return array of form contents
  1661. */
  1662. public function toArray($collectHidden = false)
  1663. {
  1664. include_once 'HTML/QuickForm/Renderer/Array.php';
  1665. // Modified by Ivan Tcholakov, 16-MAR-2010. Suppressing a deprecation warning on PHP 5.3
  1666. //$renderer =& new HTML_QuickForm_Renderer_Array($collectHidden);
  1667. $renderer = new HTML_QuickForm_Renderer_Array($collectHidden);
  1668. //
  1669. $this->accept($renderer);
  1670. return $renderer->toArray();
  1671. }
  1672. /**
  1673. * Returns a 'safe' element's value
  1674. *
  1675. * This method first tries to find a cleaned-up submitted value,
  1676. * it will return a value set by setValue()/setDefaults()/setConstants()
  1677. * if submitted value does not exist for the given element.
  1678. *
  1679. * @param string Name of an element
  1680. * @access public
  1681. * @return mixed
  1682. * @throws HTML_QuickForm_Error
  1683. */
  1684. function exportValue($element)
  1685. {
  1686. if (!isset($this->_elementIndex[$element])) {
  1687. throw new \Exception("Element '$element' does not exist in HTML_QuickForm::getElementValue()");
  1688. }
  1689. $value = $this->_elements[$this->_elementIndex[$element]]->exportValue($this->_submitValues, false);
  1690. if (isset($this->_duplicateIndex[$element])) {
  1691. foreach ($this->_duplicateIndex[$element] as $index) {
  1692. if (null !== ($v = $this->_elements[$index]->exportValue($this->_submitValues, false))) {
  1693. if (is_array($value)) {
  1694. $value[] = $v;
  1695. } else {
  1696. $value = (null === $value)? $v: array($value, $v);
  1697. }
  1698. }
  1699. }
  1700. }
  1701. return $value;
  1702. }
  1703. /**
  1704. * Returns 'safe' elements' values
  1705. *
  1706. * Unlike getSubmitValues(), this will return only the values
  1707. * corresponding to the elements present in the form.
  1708. *
  1709. * @param mixed Array/string of element names, whose values we want. If not set then return all elements.
  1710. * @access public
  1711. * @return array An assoc array of elements' values
  1712. * @throws HTML_QuickForm_Error
  1713. */
  1714. function exportValues($elementList = null)
  1715. {
  1716. $values = array();
  1717. if (null === $elementList) {
  1718. // iterate over all elements, calling their exportValue() methods
  1719. foreach (array_keys($this->_elements) as $key) {
  1720. $value = $this->_elements[$key]->exportValue($this->_submitValues, true);
  1721. if (is_array($value)) {
  1722. // This shit throws a bogus warning in PHP 4.3.x
  1723. $values = HTML_QuickForm::arrayMerge($values, $value);
  1724. }
  1725. }
  1726. } else {
  1727. if (!is_array($elementList)) {
  1728. $elementList = array_map('trim', explode(',', $elementList));
  1729. }
  1730. foreach ($elementList as $elementName) {
  1731. $value = $this->exportValue($elementName);
  1732. if (PEAR::isError($value)) {
  1733. return $value;
  1734. }
  1735. $values[$elementName] = $value;
  1736. }
  1737. }
  1738. return $values;
  1739. }
  1740. /**
  1741. * Tells whether the form was already submitted
  1742. *
  1743. * This is useful since the _submitFiles and _submitValues arrays
  1744. * may be completely empty after the trackSubmit value is removed.
  1745. *
  1746. * @access public
  1747. * @return bool
  1748. */
  1749. public function isSubmitted()
  1750. {
  1751. return $this->_flagSubmitted;
  1752. }
  1753. /**
  1754. * Tell whether a result from a QuickForm method is an error (an instance of HTML_QuickForm_Error)
  1755. *
  1756. * @access public
  1757. * @param mixed result code
  1758. * @return bool whether $value is an error
  1759. * @static
  1760. */
  1761. public function isError($value)
  1762. {
  1763. return (is_object($value) && is_a($value, 'html_quickform_error'));
  1764. }
  1765. /**
  1766. * Return a textual error message for an QuickForm error code
  1767. *
  1768. * @access public
  1769. * @param int error code
  1770. * @return string error message
  1771. * @static
  1772. */
  1773. public static function errorMessage($value)
  1774. {
  1775. // make the variable static so that it only has to do the defining on the first call
  1776. static $errorMessages;
  1777. // define the varies error messages
  1778. if (!isset($errorMessages)) {
  1779. $errorMessages = array(
  1780. QUICKFORM_OK => 'no error',
  1781. QUICKFORM_ERROR => 'unknown error',
  1782. QUICKFORM_INVALID_RULE => 'the rule does not exist as a registered rule',
  1783. QUICKFORM_NONEXIST_ELEMENT => 'nonexistent html element',
  1784. QUICKFORM_INVALID_FILTER => 'invalid filter',
  1785. QUICKFORM_UNREGISTERED_ELEMENT => 'unregistered element',
  1786. QUICKFORM_INVALID_ELEMENT_NAME => 'element already exists',
  1787. QUICKFORM_INVALID_PROCESS => 'process callback does not exist',
  1788. QUICKFORM_DEPRECATED => 'method is deprecated',
  1789. QUICKFORM_INVALID_DATASOURCE => 'datasource is not an object',
  1790. );
  1791. }
  1792. // If this is an error object, then grab the corresponding error code
  1793. if (HTML_QuickForm::isError($value)) {
  1794. $value = $value->getCode();
  1795. }
  1796. // return the textual error message corresponding to the code
  1797. return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[QUICKFORM_ERROR];
  1798. }
  1799. /**
  1800. * @param HTML_QuickForm_element $element
  1801. */
  1802. public function setRequired(HTML_QuickForm_element $element)
  1803. {
  1804. $this->addRule(
  1805. $element->getName(),
  1806. get_lang('ThisFieldIsRequired'),
  1807. 'required'
  1808. );
  1809. }
  1810. }
  1811. /**
  1812. * Class for errors thrown by HTML_QuickForm package
  1813. *
  1814. * @category HTML
  1815. * @package HTML_QuickForm
  1816. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  1817. * @author Bertrand Mansion <bmansion@mamasam.com>
  1818. * @version Release: 3.2.11
  1819. */
  1820. class HTML_QuickForm_Error extends PEAR_Error
  1821. {
  1822. /**
  1823. * Prefix for all error messages
  1824. * @var string
  1825. */
  1826. public $error_message_prefix = 'QuickForm Error: ';
  1827. /**
  1828. * Creates a quickform error object, extending the PEAR_Error class
  1829. *
  1830. * @param int $code the error code
  1831. * @param int $mode the reaction to the error, either return, die or trigger/callback
  1832. * @param int $level intensity of the error (PHP error code)
  1833. * @param mixed $debuginfo any information that can inform user as to nature of the error
  1834. */
  1835. public function __construct(
  1836. $code = QUICKFORM_ERROR,
  1837. $mode = PEAR_ERROR_RETURN,
  1838. $level = E_USER_NOTICE,
  1839. $debuginfo = null
  1840. ) {
  1841. if (is_int($code)) {
  1842. parent::__construct(HTML_QuickForm::errorMessage($code), $code, $mode, $level, $debuginfo);
  1843. } else {
  1844. parent::__construct("Invalid error code: $code", QUICKFORM_ERROR, $mode, $level, $debuginfo);
  1845. }
  1846. }
  1847. }