form_test.module 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for the form API tests.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function form_test_menu() {
  10. $items['form-test/alter'] = array(
  11. 'title' => 'Form altering test',
  12. 'page callback' => 'drupal_get_form',
  13. 'page arguments' => array('form_test_alter_form'),
  14. 'access arguments' => array('access content'),
  15. 'type' => MENU_CALLBACK,
  16. );
  17. $items['form-test/validate'] = array(
  18. 'title' => 'Form validation handlers test',
  19. 'page callback' => 'drupal_get_form',
  20. 'page arguments' => array('form_test_validate_form'),
  21. 'access arguments' => array('access content'),
  22. 'type' => MENU_CALLBACK,
  23. );
  24. $items['form-test/validate-required'] = array(
  25. 'title' => 'Form #required validation',
  26. 'page callback' => 'drupal_get_form',
  27. 'page arguments' => array('form_test_validate_required_form'),
  28. 'access callback' => TRUE,
  29. 'type' => MENU_CALLBACK,
  30. );
  31. $items['form-test/validate-required-no-title'] = array(
  32. 'title' => 'Form #required validation without #title',
  33. 'page callback' => 'drupal_get_form',
  34. 'page arguments' => array('form_test_validate_required_form_no_title'),
  35. 'access callback' => TRUE,
  36. 'type' => MENU_CALLBACK,
  37. );
  38. $items['form-test/limit-validation-errors'] = array(
  39. 'title' => 'Form validation with some error suppression',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('form_test_limit_validation_errors_form'),
  42. 'access arguments' => array('access content'),
  43. 'type' => MENU_CALLBACK,
  44. );
  45. $items['form_test/tableselect/multiple-true'] = array(
  46. 'title' => 'Tableselect checkboxes test',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('_form_test_tableselect_multiple_true_form'),
  49. 'access arguments' => array('access content'),
  50. 'type' => MENU_CALLBACK,
  51. );
  52. $items['form_test/tableselect/multiple-false'] = array(
  53. 'title' => 'Tableselect radio button test',
  54. 'page callback' => 'drupal_get_form',
  55. 'page arguments' => array('_form_test_tableselect_multiple_false_form'),
  56. 'access arguments' => array('access content'),
  57. 'type' => MENU_CALLBACK,
  58. );
  59. $items['form_test/tableselect/empty-text'] = array(
  60. 'title' => 'Tableselect empty text test',
  61. 'page callback' => 'drupal_get_form',
  62. 'page arguments' => array('_form_test_tableselect_empty_form'),
  63. 'access arguments' => array('access content'),
  64. 'type' => MENU_CALLBACK,
  65. );
  66. $items['form_test/tableselect/advanced-select'] = array(
  67. 'title' => 'Tableselect js_select tests',
  68. 'page callback' => 'drupal_get_form',
  69. 'page arguments' => array('_form_test_tableselect_js_select_form'),
  70. 'access arguments' => array('access content'),
  71. 'type' => MENU_CALLBACK,
  72. );
  73. $items['form_test/vertical-tabs'] = array(
  74. 'title' => 'Vertical tabs tests',
  75. 'page callback' => 'drupal_get_form',
  76. 'page arguments' => array('_form_test_vertical_tabs_form'),
  77. 'access arguments' => array('access content'),
  78. 'type' => MENU_CALLBACK,
  79. );
  80. $items['form_test/form-storage'] = array(
  81. 'title' => 'Form storage test',
  82. 'page callback' => 'drupal_get_form',
  83. 'page arguments' => array('form_test_storage_form'),
  84. 'access arguments' => array('access content'),
  85. 'type' => MENU_CALLBACK,
  86. );
  87. $items['form_test/form-storage-legacy'] = array(
  88. 'title' => 'Emulate legacy AHAH-style ajax callback',
  89. 'page callback' => 'form_test_storage_legacy_handler',
  90. 'access arguments' => array('access content'),
  91. 'type' => MENU_CALLBACK,
  92. );
  93. $items['form_test/form-storage-page-cache'] = array(
  94. 'title' => 'Form storage with page cache test',
  95. 'page callback' => 'drupal_get_form',
  96. 'page arguments' => array('form_test_storage_page_cache_form'),
  97. 'access arguments' => array('access content'),
  98. 'type' => MENU_CALLBACK,
  99. );
  100. $items['form_test/wrapper-callback'] = array(
  101. 'title' => 'Form wrapper callback test',
  102. 'page callback' => 'form_test_wrapper_callback',
  103. 'page arguments' => array('form_test_wrapper_callback_form'),
  104. 'access arguments' => array('access content'),
  105. 'type' => MENU_CALLBACK,
  106. );
  107. $items['form_test/form-state-values-clean'] = array(
  108. 'title' => 'Form state values clearance test',
  109. 'page callback' => 'drupal_get_form',
  110. 'page arguments' => array('form_test_form_state_values_clean_form'),
  111. 'access arguments' => array('access content'),
  112. 'type' => MENU_CALLBACK,
  113. );
  114. $items['form_test/form-state-values-clean-advanced'] = array(
  115. 'title' => 'Form state values clearance advanced test',
  116. 'page callback' => 'drupal_get_form',
  117. 'page arguments' => array('form_test_form_state_values_clean_advanced_form'),
  118. 'access arguments' => array('access content'),
  119. 'type' => MENU_CALLBACK,
  120. );
  121. $items['form-test/checkbox'] = array(
  122. 'title' => t('Form test'),
  123. 'page callback' => 'drupal_get_form',
  124. 'page arguments' => array('_form_test_checkbox'),
  125. 'access callback' => TRUE,
  126. 'type' => MENU_CALLBACK,
  127. );
  128. $items['form-test/select'] = array(
  129. 'title' => t('Select'),
  130. 'page callback' => 'drupal_get_form',
  131. 'page arguments' => array('form_test_select'),
  132. 'access callback' => TRUE,
  133. );
  134. $items['form-test/checkboxes-radios'] = array(
  135. 'title' => t('Checkboxes, Radios'),
  136. 'page callback' => 'drupal_get_form',
  137. 'page arguments' => array('form_test_checkboxes_radios'),
  138. 'access callback' => TRUE,
  139. );
  140. $items['form-test/disabled-elements'] = array(
  141. 'title' => t('Form test'),
  142. 'page callback' => 'drupal_get_form',
  143. 'page arguments' => array('_form_test_disabled_elements'),
  144. 'access callback' => TRUE,
  145. 'type' => MENU_CALLBACK,
  146. );
  147. $items['form-test/input-forgery'] = array(
  148. 'title' => t('Form test'),
  149. 'page callback' => 'drupal_get_form',
  150. 'page arguments' => array('_form_test_input_forgery'),
  151. 'access callback' => TRUE,
  152. 'type' => MENU_CALLBACK,
  153. );
  154. $items['form-test/form-rebuild-preserve-values'] = array(
  155. 'title' => 'Form values preservation during rebuild test',
  156. 'page callback' => 'drupal_get_form',
  157. 'page arguments' => array('form_test_form_rebuild_preserve_values_form'),
  158. 'access arguments' => array('access content'),
  159. 'type' => MENU_CALLBACK,
  160. );
  161. $items['form-test/redirect'] = array(
  162. 'title' => 'Redirect test',
  163. 'page callback' => 'drupal_get_form',
  164. 'page arguments' => array('form_test_redirect'),
  165. 'access callback' => TRUE,
  166. 'type' => MENU_CALLBACK,
  167. );
  168. $items['form_test/form-labels'] = array(
  169. 'title' => 'Form label test',
  170. 'page callback' => 'drupal_get_form',
  171. 'page arguments' => array('form_label_test_form'),
  172. 'access arguments' => array('access content'),
  173. 'type' => MENU_CALLBACK,
  174. );
  175. $items['form-test/state-persist'] = array(
  176. 'title' => 'Form state persistence without storage',
  177. 'page callback' => 'drupal_get_form',
  178. 'page arguments' => array('form_test_state_persist'),
  179. 'access callback' => TRUE,
  180. 'type' => MENU_CALLBACK,
  181. );
  182. $items['form-test/clicked-button'] = array(
  183. 'title' => 'Clicked button test',
  184. 'page callback' => 'drupal_get_form',
  185. 'page arguments' => array('form_test_clicked_button'),
  186. 'access callback' => TRUE,
  187. 'type' => MENU_CALLBACK,
  188. );
  189. if (module_exists('node')) {
  190. $items['form-test/two-instances-of-same-form'] = array(
  191. 'title' => 'AJAX test with two form instances',
  192. 'page callback' => 'form_test_two_instances',
  193. 'access callback' => 'node_access',
  194. 'access arguments' => array('create', 'page'),
  195. 'file path' => drupal_get_path('module', 'node'),
  196. 'file' => 'node.pages.inc',
  197. 'type' => MENU_CALLBACK,
  198. );
  199. }
  200. $items['form-test/double-form'] = array(
  201. 'title' => 'Double form test',
  202. 'page callback' => 'form_test_double_form',
  203. 'access callback' => TRUE,
  204. 'type' => MENU_CALLBACK,
  205. );
  206. $items['form-test/load-include-menu'] = array(
  207. 'title' => 'FAPI test loading includes',
  208. 'page callback' => 'drupal_get_form',
  209. 'page arguments' => array('form_test_load_include_menu'),
  210. 'access callback' => TRUE,
  211. 'file' => 'form_test.file.inc',
  212. 'type' => MENU_CALLBACK,
  213. );
  214. $items['form-test/load-include-custom'] = array(
  215. 'title' => 'FAPI test loading includes',
  216. 'page callback' => 'drupal_get_form',
  217. 'page arguments' => array('form_test_load_include_custom'),
  218. 'access callback' => TRUE,
  219. 'type' => MENU_CALLBACK,
  220. );
  221. $items['form-test/checkboxes-zero'] = array(
  222. 'title' => 'FAPI test involving checkboxes and zero',
  223. 'page callback' => 'drupal_get_form',
  224. 'page arguments' => array('form_test_checkboxes_zero'),
  225. 'access callback' => TRUE,
  226. 'type' => MENU_CALLBACK,
  227. );
  228. return $items;
  229. }
  230. /**
  231. * Form submit handler to return form values as JSON.
  232. */
  233. function _form_test_submit_values_json($form, &$form_state) {
  234. drupal_json_output($form_state['values']);
  235. drupal_exit();
  236. }
  237. /**
  238. * Form builder for testing hook_form_alter() and hook_form_FORM_ID_alter().
  239. */
  240. function form_test_alter_form($form, &$form_state) {
  241. // Elements can be added as needed for future testing needs, but for now,
  242. // we're only testing alter hooks that do not require any elements added by
  243. // this function.
  244. return $form;
  245. }
  246. /**
  247. * Implements hook_form_FORM_ID_alter() on behalf of block.module.
  248. */
  249. function block_form_form_test_alter_form_alter(&$form, &$form_state) {
  250. drupal_set_message('block_form_form_test_alter_form_alter() executed.');
  251. }
  252. /**
  253. * Implements hook_form_alter().
  254. */
  255. function form_test_form_alter(&$form, &$form_state, $form_id) {
  256. if ($form_id == 'form_test_alter_form') {
  257. drupal_set_message('form_test_form_alter() executed.');
  258. }
  259. }
  260. /**
  261. * Implements hook_form_FORM_ID_alter().
  262. */
  263. function form_test_form_form_test_alter_form_alter(&$form, &$form_state) {
  264. drupal_set_message('form_test_form_form_test_alter_form_alter() executed.');
  265. }
  266. /**
  267. * Implements hook_form_FORM_ID_alter() on behalf of system.module.
  268. */
  269. function system_form_form_test_alter_form_alter(&$form, &$form_state) {
  270. drupal_set_message('system_form_form_test_alter_form_alter() executed.');
  271. }
  272. /**
  273. * Form builder for testing drupal_validate_form().
  274. *
  275. * Serves for testing form processing and alterations by form validation
  276. * handlers, especially for the case of a validation error:
  277. * - form_set_value() should be able to alter submitted values in
  278. * $form_state['values'] without affecting the form element.
  279. * - #element_validate handlers should be able to alter the $element in the form
  280. * structure and the alterations should be contained in the rebuilt form.
  281. * - #validate handlers should be able to alter the $form and the alterations
  282. * should be contained in the rebuilt form.
  283. */
  284. function form_test_validate_form($form, &$form_state) {
  285. $form['name'] = array(
  286. '#type' => 'textfield',
  287. '#title' => 'Name',
  288. '#default_value' => '',
  289. '#element_validate' => array('form_test_element_validate_name'),
  290. );
  291. $form['submit'] = array(
  292. '#type' => 'submit',
  293. '#value' => 'Save',
  294. );
  295. // To simplify this test, enable form caching and use form storage to
  296. // remember our alteration.
  297. $form_state['cache'] = TRUE;
  298. return $form;
  299. }
  300. /**
  301. * Form element validation handler for 'name' in form_test_validate_form().
  302. */
  303. function form_test_element_validate_name(&$element, &$form_state) {
  304. $triggered = FALSE;
  305. if ($form_state['values']['name'] == 'element_validate') {
  306. // Alter the form element.
  307. $element['#value'] = '#value changed by #element_validate';
  308. // Alter the submitted value in $form_state.
  309. form_set_value($element, 'value changed by form_set_value() in #element_validate', $form_state);
  310. $triggered = TRUE;
  311. }
  312. if ($form_state['values']['name'] == 'element_validate_access') {
  313. $form_state['storage']['form_test_name'] = $form_state['values']['name'];
  314. // Alter the form element.
  315. $element['#access'] = FALSE;
  316. $triggered = TRUE;
  317. }
  318. elseif (!empty($form_state['storage']['form_test_name'])) {
  319. // To simplify this test, just take over the element's value into $form_state.
  320. form_set_value($element, $form_state['storage']['form_test_name'], $form_state);
  321. $triggered = TRUE;
  322. }
  323. if ($triggered) {
  324. // Output the element's value from $form_state.
  325. drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state['values']['name'])));
  326. // Trigger a form validation error to see our changes.
  327. form_set_error('');
  328. }
  329. }
  330. /**
  331. * Form validation handler for form_test_validate_form().
  332. */
  333. function form_test_validate_form_validate(&$form, &$form_state) {
  334. if ($form_state['values']['name'] == 'validate') {
  335. // Alter the form element.
  336. $form['name']['#value'] = '#value changed by #validate';
  337. // Alter the submitted value in $form_state.
  338. form_set_value($form['name'], 'value changed by form_set_value() in #validate', $form_state);
  339. // Output the element's value from $form_state.
  340. drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state['values']['name'])));
  341. // Trigger a form validation error to see our changes.
  342. form_set_error('');
  343. }
  344. }
  345. /**
  346. * Form constructor to test the #required property.
  347. */
  348. function form_test_validate_required_form($form, &$form_state) {
  349. $options = drupal_map_assoc(array('foo', 'bar'));
  350. $form['textfield'] = array(
  351. '#type' => 'textfield',
  352. '#title' => 'Textfield',
  353. '#required' => TRUE,
  354. );
  355. $form['checkboxes'] = array(
  356. '#type' => 'checkboxes',
  357. '#title' => 'Checkboxes',
  358. '#options' => $options,
  359. '#required' => TRUE,
  360. );
  361. $form['select'] = array(
  362. '#type' => 'select',
  363. '#title' => 'Select',
  364. '#options' => $options,
  365. '#required' => TRUE,
  366. );
  367. $form['radios'] = array(
  368. '#type' => 'radios',
  369. '#title' => 'Radios',
  370. '#options' => $options,
  371. '#required' => TRUE,
  372. );
  373. $form['radios_optional'] = array(
  374. '#type' => 'radios',
  375. '#title' => 'Radios (optional)',
  376. '#options' => $options,
  377. );
  378. $form['radios_optional_default_value_false'] = array(
  379. '#type' => 'radios',
  380. '#title' => 'Radios (optional, with a default value of FALSE)',
  381. '#options' => $options,
  382. '#default_value' => FALSE,
  383. );
  384. $form['actions'] = array('#type' => 'actions');
  385. $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  386. return $form;
  387. }
  388. /**
  389. * Form submission handler for form_test_validate_required_form().
  390. */
  391. function form_test_validate_required_form_submit($form, &$form_state) {
  392. drupal_set_message('The form_test_validate_required_form form was submitted successfully.');
  393. }
  394. /**
  395. * Form constructor to test the #required property without #title.
  396. */
  397. function form_test_validate_required_form_no_title($form, &$form_state) {
  398. $form['textfield'] = array(
  399. '#type' => 'textfield',
  400. '#required' => TRUE,
  401. );
  402. $form['actions'] = array('#type' => 'actions');
  403. $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  404. return $form;
  405. }
  406. /**
  407. * Form submission handler for form_test_validate_required_form_no_title().
  408. */
  409. function form_test_validate_required_form_no_title_submit($form, &$form_state) {
  410. drupal_set_message('The form_test_validate_required_form_no_title form was submitted successfully.');
  411. }
  412. /**
  413. * Builds a simple form with a button triggering partial validation.
  414. */
  415. function form_test_limit_validation_errors_form($form, &$form_state) {
  416. $form['title'] = array(
  417. '#type' => 'textfield',
  418. '#title' => 'Title',
  419. '#required' => TRUE,
  420. );
  421. $form['test'] = array(
  422. '#title' => 'Test',
  423. '#type' => 'textfield',
  424. '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  425. );
  426. $form['test_numeric_index'] = array(
  427. '#tree' => TRUE,
  428. );
  429. $form['test_numeric_index'][0] = array(
  430. '#title' => 'Test (numeric index)',
  431. '#type' => 'textfield',
  432. '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  433. );
  434. $form['test_substring'] = array(
  435. '#tree' => TRUE,
  436. );
  437. $form['test_substring']['foo'] = array(
  438. '#title' => 'Test (substring) foo',
  439. '#type' => 'textfield',
  440. '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  441. );
  442. $form['test_substring']['foobar'] = array(
  443. '#title' => 'Test (substring) foobar',
  444. '#type' => 'textfield',
  445. '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
  446. );
  447. $form['actions']['partial'] = array(
  448. '#type' => 'submit',
  449. '#limit_validation_errors' => array(array('test')),
  450. '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
  451. '#value' => t('Partial validate'),
  452. );
  453. $form['actions']['partial_numeric_index'] = array(
  454. '#type' => 'submit',
  455. '#limit_validation_errors' => array(array('test_numeric_index', 0)),
  456. '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
  457. '#value' => t('Partial validate (numeric index)'),
  458. );
  459. $form['actions']['substring'] = array(
  460. '#type' => 'submit',
  461. '#limit_validation_errors' => array(array('test_substring', 'foo')),
  462. '#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
  463. '#value' => t('Partial validate (substring)'),
  464. );
  465. $form['actions']['full'] = array(
  466. '#type' => 'submit',
  467. '#value' => t('Full validate'),
  468. );
  469. return $form;
  470. }
  471. /**
  472. * Form element validation handler for the 'test' element.
  473. */
  474. function form_test_limit_validation_errors_element_validate_test(&$element, &$form_state) {
  475. if ($element['#value'] == 'invalid') {
  476. form_error($element, t('@label element is invalid', array('@label' => $element['#title'])));
  477. }
  478. }
  479. /**
  480. * Form submit handler for the partial validation submit button.
  481. */
  482. function form_test_limit_validation_errors_form_partial_submit($form, $form_state) {
  483. // The title has not been validated, thus its value - in case of the test case
  484. // an empty string - may not be set.
  485. if (!isset($form_state['values']['title']) && isset($form_state['values']['test'])) {
  486. drupal_set_message('Only validated values appear in the form values.');
  487. }
  488. }
  489. /**
  490. * Create a header and options array. Helper function for callbacks.
  491. */
  492. function _form_test_tableselect_get_data() {
  493. $header = array(
  494. 'one' => t('One'),
  495. 'two' => t('Two'),
  496. 'three' => t('Three'),
  497. 'four' => t('Four'),
  498. );
  499. $options['row1'] = array(
  500. 'one' => 'row1col1',
  501. 'two' => t('row1col2'),
  502. 'three' => t('row1col3'),
  503. 'four' => t('row1col4'),
  504. );
  505. $options['row2'] = array(
  506. 'one' => 'row2col1',
  507. 'two' => t('row2col2'),
  508. 'three' => t('row2col3'),
  509. 'four' => t('row2col4'),
  510. );
  511. $options['row3'] = array(
  512. 'one' => 'row3col1',
  513. 'two' => t('row3col2'),
  514. 'three' => t('row3col3'),
  515. 'four' => t('row3col4'),
  516. );
  517. return array($header, $options);
  518. }
  519. /**
  520. * Build a form to test the tableselect element.
  521. *
  522. * @param $form_state
  523. * The form_state
  524. * @param $element_properties
  525. * An array of element properties for the tableselect element.
  526. *
  527. * @return
  528. * A form with a tableselect element and a submit button.
  529. */
  530. function _form_test_tableselect_form_builder($form, $form_state, $element_properties) {
  531. list($header, $options) = _form_test_tableselect_get_data();
  532. $form['tableselect'] = $element_properties;
  533. $form['tableselect'] += array(
  534. '#prefix' => '<div id="tableselect-wrapper">',
  535. '#suffix' => '</div>',
  536. '#type' => 'tableselect',
  537. '#header' => $header,
  538. '#options' => $options,
  539. '#multiple' => FALSE,
  540. '#empty' => t('Empty text.'),
  541. '#ajax' => array(
  542. 'callback' => '_form_test_tableselect_ajax_callback',
  543. 'wrapper' => 'tableselect-wrapper',
  544. ),
  545. );
  546. $form['submit'] = array(
  547. '#type' => 'submit',
  548. '#value' => t('Submit'),
  549. );
  550. return $form;
  551. }
  552. /**
  553. * Test the tableselect #multiple = TRUE functionality.
  554. */
  555. function _form_test_tableselect_multiple_true_form($form, $form_state) {
  556. return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => TRUE));
  557. }
  558. /**
  559. * Process the tableselect #multiple = TRUE submitted values.
  560. */
  561. function _form_test_tableselect_multiple_true_form_submit($form, &$form_state) {
  562. $selected = $form_state['values']['tableselect'];
  563. foreach ($selected as $key => $value) {
  564. drupal_set_message(t('Submitted: @key = @value', array('@key' => $key, '@value' => $value)));
  565. }
  566. }
  567. /**
  568. * Test the tableselect #multiple = FALSE functionality.
  569. */
  570. function _form_test_tableselect_multiple_false_form($form, $form_state) {
  571. return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => FALSE));
  572. }
  573. /**
  574. * Process the tableselect #multiple = FALSE submitted values.
  575. */
  576. function _form_test_tableselect_multiple_false_form_submit($form, &$form_state) {
  577. drupal_set_message(t('Submitted: @value', array('@value' => $form_state['values']['tableselect'])));
  578. }
  579. /**
  580. * Test functionality of the tableselect #empty property.
  581. */
  582. function _form_test_tableselect_empty_form($form, $form_state) {
  583. return _form_test_tableselect_form_builder($form, $form_state, array('#options' => array()));
  584. }
  585. /**
  586. * Test functionality of the tableselect #js_select property.
  587. */
  588. function _form_test_tableselect_js_select_form($form, $form_state, $action) {
  589. switch ($action) {
  590. case 'multiple-true-default':
  591. $options = array('#multiple' => TRUE);
  592. break;
  593. case 'multiple-false-default':
  594. $options = array('#multiple' => FALSE);
  595. break;
  596. case 'multiple-true-no-advanced-select':
  597. $options = array('#multiple' => TRUE, '#js_select' => FALSE);
  598. break;
  599. case 'multiple-false-advanced-select':
  600. $options = array('#multiple' => FALSE, '#js_select' => TRUE);
  601. break;
  602. }
  603. return _form_test_tableselect_form_builder($form, $form_state, $options);
  604. }
  605. /**
  606. * Tests functionality of vertical tabs.
  607. */
  608. function _form_test_vertical_tabs_form($form, &$form_state) {
  609. $form['vertical_tabs'] = array(
  610. '#type' => 'vertical_tabs',
  611. );
  612. $form['tab1'] = array(
  613. '#type' => 'fieldset',
  614. '#title' => t('Tab 1'),
  615. '#collapsible' => TRUE,
  616. '#group' => 'vertical_tabs',
  617. );
  618. $form['tab1']['field1'] = array(
  619. '#title' => t('Field 1'),
  620. '#type' => 'textfield',
  621. );
  622. $form['tab2'] = array(
  623. '#type' => 'fieldset',
  624. '#title' => t('Tab 2'),
  625. '#collapsible' => TRUE,
  626. '#group' => 'vertical_tabs',
  627. );
  628. $form['tab2']['field2'] = array(
  629. '#title' => t('Field 2'),
  630. '#type' => 'textfield',
  631. );
  632. return $form;
  633. }
  634. /**
  635. * Ajax callback that returns the form element.
  636. */
  637. function _form_test_tableselect_ajax_callback($form, &$form_state) {
  638. return $form['tableselect'];
  639. }
  640. /**
  641. * A multistep form for testing the form storage.
  642. *
  643. * It uses two steps for editing a virtual "thing". Any changes to it are saved
  644. * in the form storage and have to be present during any step. By setting the
  645. * request parameter "cache" the form can be tested with caching enabled, as
  646. * it would be the case, if the form would contain some #ajax callbacks.
  647. *
  648. * @see form_test_storage_form_submit()
  649. */
  650. function form_test_storage_form($form, &$form_state) {
  651. if ($form_state['rebuild']) {
  652. $form_state['input'] = array();
  653. }
  654. // Initialize
  655. if (empty($form_state['storage'])) {
  656. if (empty($form_state['input'])) {
  657. $_SESSION['constructions'] = 0;
  658. }
  659. // Put the initial thing into the storage
  660. $form_state['storage'] = array(
  661. 'thing' => array(
  662. 'title' => 'none',
  663. 'value' => '',
  664. ),
  665. );
  666. }
  667. // Count how often the form is constructed.
  668. $_SESSION['constructions']++;
  669. drupal_set_message("Form constructions: " . $_SESSION['constructions']);
  670. $form['title'] = array(
  671. '#type' => 'textfield',
  672. '#title' => 'Title',
  673. '#default_value' => $form_state['storage']['thing']['title'],
  674. '#required' => TRUE,
  675. );
  676. $form['value'] = array(
  677. '#type' => 'textfield',
  678. '#title' => 'Value',
  679. '#default_value' => $form_state['storage']['thing']['value'],
  680. '#element_validate' => array('form_test_storage_element_validate_value_cached'),
  681. );
  682. $form['continue_button'] = array(
  683. '#type' => 'button',
  684. '#value' => 'Reset',
  685. // Rebuilds the form without keeping the values.
  686. );
  687. $form['continue_submit'] = array(
  688. '#type' => 'submit',
  689. '#value' => 'Continue submit',
  690. '#submit' => array('form_storage_test_form_continue_submit'),
  691. );
  692. $form['submit'] = array(
  693. '#type' => 'submit',
  694. '#value' => 'Save',
  695. );
  696. if (isset($_REQUEST['cache'])) {
  697. // Manually activate caching, so we can test that the storage keeps working
  698. // when it's enabled.
  699. $form_state['cache'] = TRUE;
  700. }
  701. if (isset($_REQUEST['immutable'])) {
  702. $form_state['build_info']['immutable'] = TRUE;
  703. }
  704. return $form;
  705. }
  706. /**
  707. * Emulate legacy AHAH-style ajax callback.
  708. *
  709. * Drupal 6 AHAH callbacks used to operate directly on forms retrieved using
  710. * form_get_cache and stored using form_set_cache after manipulation. This
  711. * callback helps testing whether form_set_cache prevents resaving of immutable
  712. * forms.
  713. */
  714. function form_test_storage_legacy_handler($form_build_id) {
  715. $form_state = array();
  716. $form = form_get_cache($form_build_id, $form_state);
  717. drupal_json_output(array(
  718. 'form' => $form,
  719. 'form_state' => $form_state,
  720. ));
  721. $form['#poisoned'] = TRUE;
  722. $form_state['poisoned'] = TRUE;
  723. form_set_cache($form_build_id, $form, $form_state);
  724. }
  725. /**
  726. * Form element validation handler for 'value' element in form_test_storage_form().
  727. *
  728. * Tests updating of cached form storage during validation.
  729. */
  730. function form_test_storage_element_validate_value_cached($element, &$form_state) {
  731. // If caching is enabled and we receive a certain value, change the storage.
  732. // This presumes that another submitted form value triggers a validation error
  733. // elsewhere in the form. Form API should still update the cached form storage
  734. // though.
  735. if (isset($_REQUEST['cache']) && $form_state['values']['value'] == 'change_title') {
  736. $form_state['storage']['thing']['changed'] = TRUE;
  737. }
  738. }
  739. /**
  740. * Form submit handler to continue multi-step form.
  741. */
  742. function form_storage_test_form_continue_submit($form, &$form_state) {
  743. $form_state['storage']['thing']['title'] = $form_state['values']['title'];
  744. $form_state['storage']['thing']['value'] = $form_state['values']['value'];
  745. $form_state['rebuild'] = TRUE;
  746. }
  747. /**
  748. * Form submit handler to finish multi-step form.
  749. */
  750. function form_test_storage_form_submit($form, &$form_state) {
  751. drupal_set_message("Title: " . check_plain($form_state['values']['title']));
  752. drupal_set_message("Form constructions: " . $_SESSION['constructions']);
  753. if (isset($form_state['storage']['thing']['changed'])) {
  754. drupal_set_message("The thing has been changed.");
  755. }
  756. $form_state['redirect'] = 'node';
  757. }
  758. /**
  759. * A simple form for testing form storage when page caching is enabled.
  760. */
  761. function form_test_storage_page_cache_form($form, &$form_state) {
  762. $form['title'] = array(
  763. '#type' => 'textfield',
  764. '#title' => 'Title',
  765. '#required' => TRUE,
  766. );
  767. $form['test_build_id_old'] = array(
  768. '#type' => 'item',
  769. '#title' => 'Old build id',
  770. '#markup' => 'No old build id',
  771. );
  772. $form['submit'] = array(
  773. '#type' => 'submit',
  774. '#value' => 'Save',
  775. );
  776. $form['rebuild'] = array(
  777. '#type' => 'submit',
  778. '#value' => 'Rebuild',
  779. '#submit' => array('form_test_storage_page_cache_rebuild'),
  780. );
  781. $form['#after_build'] = array('form_test_storage_page_cache_old_build_id');
  782. $form_state['cache'] = TRUE;
  783. return $form;
  784. }
  785. /**
  786. * Form element #after_build callback: output the old form build-id.
  787. */
  788. function form_test_storage_page_cache_old_build_id($form) {
  789. if (isset($form['#build_id_old'])) {
  790. $form['test_build_id_old']['#markup'] = check_plain($form['#build_id_old']);
  791. }
  792. return $form;
  793. }
  794. /**
  795. * Form submit callback: Rebuild the form and continue.
  796. */
  797. function form_test_storage_page_cache_rebuild($form, &$form_state) {
  798. $form_state['rebuild'] = TRUE;
  799. }
  800. /**
  801. * A form for testing form labels and required marks.
  802. */
  803. function form_label_test_form() {
  804. $form['form_checkboxes_test'] = array(
  805. '#type' => 'checkboxes',
  806. '#title' => t('Checkboxes test'),
  807. '#options' => array(
  808. 'first-checkbox' => t('First checkbox'),
  809. 'second-checkbox' => t('Second checkbox'),
  810. 'third-checkbox' => t('Third checkbox'),
  811. '0' => t('0'),
  812. ),
  813. );
  814. $form['form_radios_test'] = array(
  815. '#type' => 'radios',
  816. '#title' => t('Radios test'),
  817. '#options' => array(
  818. 'first-radio' => t('First radio'),
  819. 'second-radio' => t('Second radio'),
  820. 'third-radio' => t('Third radio'),
  821. '0' => t('0'),
  822. ),
  823. // Test #field_prefix and #field_suffix placement.
  824. '#field_prefix' => '<span id="form-test-radios-field-prefix">' . t('Radios #field_prefix element') . '</span>',
  825. '#field_suffix' => '<span id="form-test-radios-field-suffix">' . t('Radios #field_suffix element') . '</span>',
  826. );
  827. $form['form_checkbox_test'] = array(
  828. '#type' => 'checkbox',
  829. '#title' => t('Checkbox test'),
  830. );
  831. $form['form_textfield_test_title_and_required'] = array(
  832. '#type' => 'textfield',
  833. '#title' => t('Textfield test for required with title'),
  834. '#required' => TRUE,
  835. );
  836. $form['form_textfield_test_no_title_required'] = array(
  837. '#type' => 'textfield',
  838. // We use an empty title, since not setting #title suppresses the label
  839. // and required marker.
  840. '#title' => '',
  841. '#required' => TRUE,
  842. );
  843. $form['form_textfield_test_title'] = array(
  844. '#type' => 'textfield',
  845. '#title' => t('Textfield test for title only'),
  846. // Not required.
  847. // Test #prefix and #suffix placement.
  848. '#prefix' => '<div id="form-test-textfield-title-prefix">' . t('Textfield #prefix element') . '</div>',
  849. '#suffix' => '<div id="form-test-textfield-title-suffix">' . t('Textfield #suffix element') . '</div>',
  850. );
  851. $form['form_textfield_test_title_after'] = array(
  852. '#type' => 'textfield',
  853. '#title' => t('Textfield test for title after element'),
  854. '#title_display' => 'after',
  855. );
  856. $form['form_textfield_test_title_invisible'] = array(
  857. '#type' => 'textfield',
  858. '#title' => t('Textfield test for invisible title'),
  859. '#title_display' => 'invisible',
  860. );
  861. // Textfield test for title set not to display.
  862. $form['form_textfield_test_title_no_show'] = array(
  863. '#type' => 'textfield',
  864. );
  865. // Checkboxes & radios with title as attribute.
  866. $form['form_checkboxes_title_attribute'] = array(
  867. '#type' => 'checkboxes',
  868. '#title' => 'Checkboxes test',
  869. '#options' => array(
  870. 'first-checkbox' => 'First checkbox',
  871. 'second-checkbox' => 'Second checkbox',
  872. ),
  873. '#title_display' => 'attribute',
  874. '#required' => TRUE,
  875. );
  876. $form['form_radios_title_attribute'] = array(
  877. '#type' => 'radios',
  878. '#title' => 'Radios test',
  879. '#options' => array(
  880. 'first-radio' => 'First radio',
  881. 'second-radio' => 'Second radio',
  882. ),
  883. '#title_display' => 'attribute',
  884. '#required' => TRUE,
  885. );
  886. return $form;
  887. }
  888. /**
  889. * Menu callback; Invokes a form builder function with a wrapper callback.
  890. */
  891. function form_test_wrapper_callback($form_id) {
  892. $form_state = array(
  893. 'build_info' => array('args' => array()),
  894. 'wrapper_callback' => 'form_test_wrapper_callback_wrapper',
  895. );
  896. return drupal_build_form($form_id, $form_state);
  897. }
  898. /**
  899. * Form wrapper for form_test_wrapper_callback_form().
  900. */
  901. function form_test_wrapper_callback_wrapper($form, &$form_state) {
  902. $form['wrapper'] = array('#markup' => 'Form wrapper callback element output.');
  903. return $form;
  904. }
  905. /**
  906. * Form builder for form wrapper callback test.
  907. */
  908. function form_test_wrapper_callback_form($form, &$form_state) {
  909. $form['builder'] = array('#markup' => 'Form builder element output.');
  910. return $form;
  911. }
  912. /**
  913. * Form builder for form_state_values_clean() test.
  914. */
  915. function form_test_form_state_values_clean_form($form, &$form_state) {
  916. // Build an example form containing multiple submit and button elements; not
  917. // only on the top-level.
  918. $form = array('#tree' => TRUE);
  919. $form['foo'] = array('#type' => 'submit', '#value' => t('Submit'));
  920. $form['bar'] = array('#type' => 'submit', '#value' => t('Submit'));
  921. $form['beer'] = array('#type' => 'value', '#value' => 1000);
  922. $form['baz']['foo'] = array('#type' => 'button', '#value' => t('Submit'));
  923. $form['baz']['baz'] = array('#type' => 'submit', '#value' => t('Submit'));
  924. $form['baz']['beer'] = array('#type' => 'value', '#value' => 2000);
  925. return $form;
  926. }
  927. /**
  928. * Form submit handler for form_state_values_clean() test form.
  929. */
  930. function form_test_form_state_values_clean_form_submit($form, &$form_state) {
  931. form_state_values_clean($form_state);
  932. drupal_json_output($form_state['values']);
  933. exit;
  934. }
  935. /**
  936. * Form constructor for the form_state_values_clean() test.
  937. */
  938. function form_test_form_state_values_clean_advanced_form($form, &$form_state) {
  939. // Build an example form containing a managed file and a submit form element.
  940. $form['image'] = array(
  941. '#type' => 'managed_file',
  942. '#title' => t('Image'),
  943. '#upload_location' => 'public://',
  944. '#default_value' => 0,
  945. );
  946. $form['submit'] = array(
  947. '#type' => 'submit',
  948. '#value' => t('Submit'),
  949. );
  950. return $form;
  951. }
  952. /**
  953. * Form submission handler for form_test_form_state_values_clean_advanced_form().
  954. */
  955. function form_test_form_state_values_clean_advanced_form_submit($form, &$form_state) {
  956. form_state_values_clean($form_state);
  957. print t('You WIN!');
  958. exit;
  959. }
  960. /**
  961. * Build a form to test a checkbox.
  962. */
  963. function _form_test_checkbox($form, &$form_state) {
  964. // A required checkbox.
  965. $form['required_checkbox'] = array(
  966. '#type' => 'checkbox',
  967. '#required' => TRUE,
  968. '#title' => 'required_checkbox',
  969. );
  970. // A disabled checkbox should get its default value back.
  971. $form['disabled_checkbox_on'] = array(
  972. '#type' => 'checkbox',
  973. '#disabled' => TRUE,
  974. '#return_value' => 'disabled_checkbox_on',
  975. '#default_value' => 'disabled_checkbox_on',
  976. '#title' => 'disabled_checkbox_on',
  977. );
  978. $form['disabled_checkbox_off'] = array(
  979. '#type' => 'checkbox',
  980. '#disabled' => TRUE,
  981. '#return_value' => 'disabled_checkbox_off',
  982. '#default_value' => NULL,
  983. '#title' => 'disabled_checkbox_off',
  984. );
  985. // A checkbox is active when #default_value == #return_value.
  986. $form['checkbox_on'] = array(
  987. '#type' => 'checkbox',
  988. '#return_value' => 'checkbox_on',
  989. '#default_value' => 'checkbox_on',
  990. '#title' => 'checkbox_on',
  991. );
  992. // But inactive in any other case.
  993. $form['checkbox_off'] = array(
  994. '#type' => 'checkbox',
  995. '#return_value' => 'checkbox_off',
  996. '#default_value' => 'checkbox_on',
  997. '#title' => 'checkbox_off',
  998. );
  999. // Checkboxes with a #return_value of '0' are supported.
  1000. $form['zero_checkbox_on'] = array(
  1001. '#type' => 'checkbox',
  1002. '#return_value' => '0',
  1003. '#default_value' => '0',
  1004. '#title' => 'zero_checkbox_on',
  1005. );
  1006. // In that case, passing a #default_value != '0' means that the checkbox is off.
  1007. $form['zero_checkbox_off'] = array(
  1008. '#type' => 'checkbox',
  1009. '#return_value' => '0',
  1010. '#default_value' => '1',
  1011. '#title' => 'zero_checkbox_off',
  1012. );
  1013. $form['submit'] = array(
  1014. '#type' => 'submit',
  1015. '#value' => t('Submit')
  1016. );
  1017. return $form;
  1018. }
  1019. /**
  1020. * Return the form values via JSON.
  1021. */
  1022. function _form_test_checkbox_submit($form, &$form_state) {
  1023. drupal_json_output($form_state['values']);
  1024. exit();
  1025. }
  1026. /**
  1027. * Builds a form to test #type 'select' validation.
  1028. */
  1029. function form_test_select($form, &$form_state) {
  1030. $base = array(
  1031. '#type' => 'select',
  1032. '#options' => drupal_map_assoc(array('one', 'two', 'three')),
  1033. );
  1034. $form['select'] = $base + array(
  1035. '#title' => '#default_value one',
  1036. '#default_value' => 'one',
  1037. );
  1038. $form['select_required'] = $base + array(
  1039. '#title' => '#default_value one, #required',
  1040. '#required' => TRUE,
  1041. '#default_value' => 'one',
  1042. );
  1043. $form['select_optional'] = $base + array(
  1044. '#title' => '#default_value one, not #required',
  1045. '#required' => FALSE,
  1046. '#default_value' => 'one',
  1047. );
  1048. $form['empty_value'] = $base + array(
  1049. '#title' => '#default_value one, #required, #empty_value 0',
  1050. '#required' => TRUE,
  1051. '#default_value' => 'one',
  1052. '#empty_value' => 0,
  1053. );
  1054. $form['empty_value_one'] = $base + array(
  1055. '#title' => '#default_value = #empty_value, #required',
  1056. '#required' => TRUE,
  1057. '#default_value' => 'one',
  1058. '#empty_value' => 'one',
  1059. );
  1060. $form['no_default'] = $base + array(
  1061. '#title' => 'No #default_value, #required',
  1062. '#required' => TRUE,
  1063. );
  1064. $form['no_default_optional'] = $base + array(
  1065. '#title' => 'No #default_value, not #required',
  1066. '#required' => FALSE,
  1067. '#description' => 'Should result in "one", because it is not required and there is no #empty_value requested, so default browser behavior of preselecting first option is in effect.',
  1068. );
  1069. $form['no_default_optional_empty_value'] = $base + array(
  1070. '#title' => 'No #default_value, not #required, #empty_value empty string',
  1071. '#empty_value' => '',
  1072. '#required' => FALSE,
  1073. '#description' => 'Should result in an empty string (due to #empty_value), because it is optional.',
  1074. );
  1075. $form['no_default_empty_option'] = $base + array(
  1076. '#title' => 'No #default_value, #required, #empty_option',
  1077. '#required' => TRUE,
  1078. '#empty_option' => '- Choose -',
  1079. );
  1080. $form['no_default_empty_option_optional'] = $base + array(
  1081. '#title' => 'No #default_value, not #required, #empty_option',
  1082. '#empty_option' => '- Dismiss -',
  1083. '#description' => 'Should result in an empty string (default of #empty_value), because it is optional.',
  1084. );
  1085. $form['no_default_empty_value'] = $base + array(
  1086. '#title' => 'No #default_value, #required, #empty_value 0',
  1087. '#required' => TRUE,
  1088. '#empty_value' => 0,
  1089. '#description' => 'Should never result in 0.',
  1090. );
  1091. $form['no_default_empty_value_one'] = $base + array(
  1092. '#title' => 'No #default_value, #required, #empty_value one',
  1093. '#required' => TRUE,
  1094. '#empty_value' => 'one',
  1095. '#description' => 'A mistakenly assigned #empty_value contained in #options should not be valid.',
  1096. );
  1097. $form['no_default_empty_value_optional'] = $base + array(
  1098. '#title' => 'No #default_value, not #required, #empty_value 0',
  1099. '#required' => FALSE,
  1100. '#empty_value' => 0,
  1101. '#description' => 'Should result in 0, because it is optional.',
  1102. );
  1103. $form['multiple'] = $base + array(
  1104. '#title' => '#multiple, #default_value two',
  1105. '#default_value' => array('two'),
  1106. '#multiple' => TRUE,
  1107. );
  1108. $form['multiple_no_default'] = $base + array(
  1109. '#title' => '#multiple, no #default_value',
  1110. '#multiple' => TRUE,
  1111. );
  1112. $form['multiple_no_default_required'] = $base + array(
  1113. '#title' => '#multiple, #required, no #default_value',
  1114. '#required' => TRUE,
  1115. '#multiple' => TRUE,
  1116. );
  1117. $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  1118. return $form;
  1119. }
  1120. /**
  1121. * Form submit handler for form_test_select().
  1122. */
  1123. function form_test_select_submit($form, &$form_state) {
  1124. drupal_json_output($form_state['values']);
  1125. exit();
  1126. }
  1127. /**
  1128. * Form constructor to test expansion of #type checkboxes and radios.
  1129. */
  1130. function form_test_checkboxes_radios($form, &$form_state, $customize = FALSE) {
  1131. $form['#submit'] = array('_form_test_submit_values_json');
  1132. // Expand #type checkboxes, setting custom element properties for some but not
  1133. // all options.
  1134. $form['checkboxes'] = array(
  1135. '#type' => 'checkboxes',
  1136. '#title' => 'Checkboxes',
  1137. '#options' => array(
  1138. 0 => 'Zero',
  1139. 'foo' => 'Foo',
  1140. 1 => 'One',
  1141. 'bar' => 'Bar',
  1142. '>' => 'Special Char',
  1143. ),
  1144. );
  1145. if ($customize) {
  1146. $form['checkboxes'] += array(
  1147. 'foo' => array(
  1148. '#description' => 'Enable to foo.',
  1149. ),
  1150. 1 => array(
  1151. '#weight' => 10,
  1152. ),
  1153. );
  1154. }
  1155. // Expand #type radios, setting custom element properties for some but not
  1156. // all options.
  1157. $form['radios'] = array(
  1158. '#type' => 'radios',
  1159. '#title' => 'Radios',
  1160. '#options' => array(
  1161. 0 => 'Zero',
  1162. 'foo' => 'Foo',
  1163. 1 => 'One',
  1164. 'bar' => 'Bar',
  1165. '>' => 'Special Char',
  1166. ),
  1167. );
  1168. if ($customize) {
  1169. $form['radios'] += array(
  1170. 'foo' => array(
  1171. '#description' => 'Enable to foo.',
  1172. ),
  1173. 1 => array(
  1174. '#weight' => 10,
  1175. ),
  1176. );
  1177. }
  1178. $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  1179. return $form;
  1180. }
  1181. /**
  1182. * Build a form to test disabled elements.
  1183. */
  1184. function _form_test_disabled_elements($form, &$form_state) {
  1185. // Elements that take a simple default value.
  1186. foreach (array('textfield', 'textarea', 'hidden') as $type) {
  1187. $form[$type] = array(
  1188. '#type' => $type,
  1189. '#title' => $type,
  1190. '#default_value' => $type,
  1191. '#test_hijack_value' => 'HIJACK',
  1192. '#disabled' => TRUE,
  1193. );
  1194. }
  1195. // Multiple values option elements.
  1196. foreach (array('checkboxes', 'select') as $type) {
  1197. $form[$type . '_multiple'] = array(
  1198. '#type' => $type,
  1199. '#title' => $type . ' (multiple)',
  1200. '#options' => array(
  1201. 'test_1' => 'Test 1',
  1202. 'test_2' => 'Test 2',
  1203. ),
  1204. '#multiple' => TRUE,
  1205. '#default_value' => array('test_2' => 'test_2'),
  1206. // The keys of #test_hijack_value need to match the #name of the control.
  1207. // @see FormsTestCase::testDisabledElements()
  1208. '#test_hijack_value' => $type == 'select' ? array('' => 'test_1') : array('test_1' => 'test_1'),
  1209. '#disabled' => TRUE,
  1210. );
  1211. }
  1212. // Single values option elements.
  1213. foreach (array('radios', 'select') as $type) {
  1214. $form[$type . '_single'] = array(
  1215. '#type' => $type,
  1216. '#title' => $type . ' (single)',
  1217. '#options' => array(
  1218. 'test_1' => 'Test 1',
  1219. 'test_2' => 'Test 2',
  1220. ),
  1221. '#multiple' => FALSE,
  1222. '#default_value' => 'test_2',
  1223. '#test_hijack_value' => 'test_1',
  1224. '#disabled' => TRUE,
  1225. );
  1226. }
  1227. // Checkbox and radio.
  1228. foreach (array('checkbox', 'radio') as $type) {
  1229. $form[$type . '_unchecked'] = array(
  1230. '#type' => $type,
  1231. '#title' => $type . ' (unchecked)',
  1232. '#return_value' => 1,
  1233. '#default_value' => 0,
  1234. '#test_hijack_value' => 1,
  1235. '#disabled' => TRUE,
  1236. );
  1237. $form[$type . '_checked'] = array(
  1238. '#type' => $type,
  1239. '#title' => $type . ' (checked)',
  1240. '#return_value' => 1,
  1241. '#default_value' => 1,
  1242. '#test_hijack_value' => NULL,
  1243. '#disabled' => TRUE,
  1244. );
  1245. }
  1246. // Weight.
  1247. $form['weight'] = array(
  1248. '#type' => 'weight',
  1249. '#title' => 'weight',
  1250. '#default_value' => 10,
  1251. '#test_hijack_value' => 5,
  1252. '#disabled' => TRUE,
  1253. );
  1254. // Date.
  1255. $form['date'] = array(
  1256. '#type' => 'date',
  1257. '#title' => 'date',
  1258. '#disabled' => TRUE,
  1259. '#default_value' => array(
  1260. 'day' => 19,
  1261. 'month' => 11,
  1262. 'year' => 1978,
  1263. ),
  1264. '#test_hijack_value' => array(
  1265. 'day' => 20,
  1266. 'month' => 12,
  1267. 'year' => 1979,
  1268. ),
  1269. );
  1270. // The #disabled state should propagate to children.
  1271. $form['disabled_container'] = array(
  1272. '#disabled' => TRUE,
  1273. );
  1274. foreach (array('textfield', 'textarea', 'hidden') as $type) {
  1275. $form['disabled_container']['disabled_container_' . $type] = array(
  1276. '#type' => $type,
  1277. '#title' => $type,
  1278. '#default_value' => $type,
  1279. '#test_hijack_value' => 'HIJACK',
  1280. );
  1281. }
  1282. // Text format.
  1283. $form['text_format'] = array(
  1284. '#type' => 'text_format',
  1285. '#title' => 'Text format',
  1286. '#disabled' => TRUE,
  1287. '#default_value' => 'Text value',
  1288. '#format' => 'plain_text',
  1289. '#expected_value' => array(
  1290. 'value' => 'Text value',
  1291. 'format' => 'plain_text',
  1292. ),
  1293. '#test_hijack_value' => array(
  1294. 'value' => 'HIJACK',
  1295. 'format' => 'filtered_html',
  1296. ),
  1297. );
  1298. // Password fields.
  1299. $form['password'] = array(
  1300. '#type' => 'password',
  1301. '#title' => 'Password',
  1302. '#disabled' => TRUE,
  1303. );
  1304. $form['password_confirm'] = array(
  1305. '#type' => 'password_confirm',
  1306. '#title' => 'Password confirm',
  1307. '#disabled' => TRUE,
  1308. );
  1309. // Files.
  1310. $form['file'] = array(
  1311. '#type' => 'file',
  1312. '#title' => 'File',
  1313. '#disabled' => TRUE,
  1314. );
  1315. $form['managed_file'] = array(
  1316. '#type' => 'managed_file',
  1317. '#title' => 'Managed file',
  1318. '#disabled' => TRUE,
  1319. );
  1320. // Buttons.
  1321. $form['image_button'] = array(
  1322. '#type' => 'image_button',
  1323. '#value' => 'Image button',
  1324. '#disabled' => TRUE,
  1325. );
  1326. $form['button'] = array(
  1327. '#type' => 'button',
  1328. '#value' => 'Button',
  1329. '#disabled' => TRUE,
  1330. );
  1331. $form['submit_disabled'] = array(
  1332. '#type' => 'submit',
  1333. '#value' => 'Submit',
  1334. '#disabled' => TRUE,
  1335. );
  1336. $form['submit'] = array(
  1337. '#type' => 'submit',
  1338. '#value' => t('Submit'),
  1339. );
  1340. return $form;
  1341. }
  1342. /**
  1343. * Return the form values via JSON.
  1344. */
  1345. function _form_test_disabled_elements_submit($form, &$form_state) {
  1346. drupal_json_output($form_state['values']);
  1347. exit();
  1348. }
  1349. /**
  1350. * Build a form to test input forgery of enabled elements.
  1351. */
  1352. function _form_test_input_forgery($form, &$form_state) {
  1353. // For testing that a user can't submit a value not matching one of the
  1354. // allowed options.
  1355. $form['checkboxes'] = array(
  1356. '#type' => 'checkboxes',
  1357. '#options' => array(
  1358. 'one' => 'One',
  1359. 'two' => 'Two',
  1360. ),
  1361. );
  1362. $form['submit'] = array(
  1363. '#type' => 'submit',
  1364. '#value' => t('Submit'),
  1365. );
  1366. return $form;
  1367. }
  1368. /**
  1369. * Return the form values via JSON.
  1370. */
  1371. function _form_test_input_forgery_submit($form, &$form_state) {
  1372. drupal_json_output($form_state['values']);
  1373. exit();
  1374. }
  1375. /**
  1376. * Form builder for testing preservation of values during a rebuild.
  1377. */
  1378. function form_test_form_rebuild_preserve_values_form($form, &$form_state) {
  1379. // Start the form with two checkboxes, to test different defaults, and a
  1380. // textfield, to test more than one element type.
  1381. $form = array(
  1382. 'checkbox_1_default_off' => array(
  1383. '#type' => 'checkbox',
  1384. '#title' => t('This checkbox defaults to unchecked.'),
  1385. '#default_value' => FALSE,
  1386. ),
  1387. 'checkbox_1_default_on' => array(
  1388. '#type' => 'checkbox',
  1389. '#title' => t('This checkbox defaults to checked.'),
  1390. '#default_value' => TRUE,
  1391. ),
  1392. 'text_1' => array(
  1393. '#type' => 'textfield',
  1394. '#title' => t('This textfield has a non-empty default value.'),
  1395. '#default_value' => 'DEFAULT 1',
  1396. ),
  1397. );
  1398. // Provide an 'add more' button that rebuilds the form with an additional two
  1399. // checkboxes and a textfield. The test is to make sure that the rebuild
  1400. // triggered by this button preserves the user input values for the initial
  1401. // elements and initializes the new elements with the correct default values.
  1402. if (empty($form_state['storage']['add_more'])) {
  1403. $form['add_more'] = array(
  1404. '#type' => 'submit',
  1405. '#value' => 'Add more',
  1406. '#submit' => array('form_test_form_rebuild_preserve_values_form_add_more'),
  1407. );
  1408. }
  1409. else {
  1410. $form += array(
  1411. 'checkbox_2_default_off' => array(
  1412. '#type' => 'checkbox',
  1413. '#title' => t('This checkbox defaults to unchecked.'),
  1414. '#default_value' => FALSE,
  1415. ),
  1416. 'checkbox_2_default_on' => array(
  1417. '#type' => 'checkbox',
  1418. '#title' => t('This checkbox defaults to checked.'),
  1419. '#default_value' => TRUE,
  1420. ),
  1421. 'text_2' => array(
  1422. '#type' => 'textfield',
  1423. '#title' => t('This textfield has a non-empty default value.'),
  1424. '#default_value' => 'DEFAULT 2',
  1425. ),
  1426. );
  1427. }
  1428. // A submit button that finishes the form workflow (does not rebuild).
  1429. $form['submit'] = array(
  1430. '#type' => 'submit',
  1431. '#value' => 'Submit',
  1432. );
  1433. return $form;
  1434. }
  1435. /**
  1436. * Button submit handler for form_test_form_rebuild_preserve_values_form().
  1437. */
  1438. function form_test_form_rebuild_preserve_values_form_add_more($form, &$form_state) {
  1439. // Rebuild, to test preservation of input values.
  1440. $form_state['storage']['add_more'] = TRUE;
  1441. $form_state['rebuild'] = TRUE;
  1442. }
  1443. /**
  1444. * Form submit handler for form_test_form_rebuild_preserve_values_form().
  1445. */
  1446. function form_test_form_rebuild_preserve_values_form_submit($form, &$form_state) {
  1447. // Finish the workflow. Do not rebuild.
  1448. drupal_set_message(t('Form values: %values', array('%values' => var_export($form_state['values'], TRUE))));
  1449. }
  1450. /**
  1451. * Form constructor for testing form state persistence.
  1452. */
  1453. function form_test_state_persist($form, &$form_state) {
  1454. $form['title'] = array(
  1455. '#type' => 'textfield',
  1456. '#title' => 'title',
  1457. '#default_value' => 'DEFAULT',
  1458. '#required' => TRUE,
  1459. );
  1460. $form_state['value'] = 'State persisted.';
  1461. $form['submit'] = array(
  1462. '#type' => 'submit',
  1463. '#value' => t('Submit'),
  1464. );
  1465. return $form;
  1466. }
  1467. /**
  1468. * Submit handler.
  1469. *
  1470. * @see form_test_state_persist()
  1471. */
  1472. function form_test_state_persist_submit($form, &$form_state) {
  1473. drupal_set_message($form_state['value']);
  1474. $form_state['rebuild'] = TRUE;
  1475. }
  1476. /**
  1477. * Implements hook_form_FORM_ID_alter().
  1478. *
  1479. * @see form_test_state_persist()
  1480. */
  1481. function form_test_form_form_test_state_persist_alter(&$form, &$form_state) {
  1482. // Simulate a form alter implementation inserting form elements that enable
  1483. // caching of the form, e.g. elements having #ajax.
  1484. if (!empty($_REQUEST['cache'])) {
  1485. $form_state['cache'] = TRUE;
  1486. }
  1487. }
  1488. /**
  1489. * Form builder to test programmatic form submissions.
  1490. */
  1491. function form_test_programmatic_form($form, &$form_state) {
  1492. $form['textfield'] = array(
  1493. '#title' => 'Textfield',
  1494. '#type' => 'textfield',
  1495. );
  1496. $form['checkboxes'] = array(
  1497. '#type' => 'checkboxes',
  1498. '#options' => array(
  1499. 1 => 'First checkbox',
  1500. 2 => 'Second checkbox',
  1501. ),
  1502. // Both checkboxes are selected by default so that we can test the ability
  1503. // of programmatic form submissions to uncheck them.
  1504. '#default_value' => array(1, 2),
  1505. );
  1506. // This is used to test that programmatic form submissions can bypass #access
  1507. // restrictions.
  1508. $form['textfield_no_access'] = array(
  1509. '#type' => 'textfield',
  1510. '#title' => 'Textfield no access',
  1511. '#default_value' => 'default value',
  1512. '#access' => FALSE,
  1513. );
  1514. $form['field_to_validate'] = array(
  1515. '#type' => 'radios',
  1516. '#title' => 'Field to validate (in the case of limited validation)',
  1517. '#description' => 'If the form is submitted by clicking the "Submit with limited validation" button, then validation can be limited based on the value of this radio button.',
  1518. '#options' => array(
  1519. 'all' => 'Validate all fields',
  1520. 'textfield' => 'Validate the "Textfield" field',
  1521. 'field_to_validate' => 'Validate the "Field to validate" field',
  1522. ),
  1523. '#default_value' => 'all',
  1524. );
  1525. // The main submit button for the form.
  1526. $form['submit'] = array(
  1527. '#type' => 'submit',
  1528. '#value' => 'Submit',
  1529. );
  1530. // A secondary submit button that allows validation to be limited based on
  1531. // the value of the above radio selector.
  1532. $form['submit_limit_validation'] = array(
  1533. '#type' => 'submit',
  1534. '#value' => 'Submit with limited validation',
  1535. // Use the same submit handler for this button as for the form itself.
  1536. // (This must be set explicitly or otherwise the form API will ignore the
  1537. // #limit_validation_errors property.)
  1538. '#submit' => array('form_test_programmatic_form_submit'),
  1539. );
  1540. if (!empty($form_state['input']['field_to_validate']) && $form_state['input']['field_to_validate'] != 'all') {
  1541. $form['submit_limit_validation']['#limit_validation_errors'] = array(
  1542. array($form_state['input']['field_to_validate']),
  1543. );
  1544. }
  1545. return $form;
  1546. }
  1547. /**
  1548. * Form validation handler for programmatic form submissions.
  1549. *
  1550. * To test that the validation handler is correctly executed, the field value is
  1551. * explicitly required here.
  1552. */
  1553. function form_test_programmatic_form_validate($form, &$form_state) {
  1554. if (empty($form_state['values']['textfield'])) {
  1555. form_set_error('textfield', t('Textfield is required.'));
  1556. }
  1557. }
  1558. /**
  1559. * Form submit handler for programmatic form submissions.
  1560. *
  1561. * To test that the submission handler is correctly executed, we store the
  1562. * submitted values in a place we can access from the caller context.
  1563. */
  1564. function form_test_programmatic_form_submit($form, &$form_state) {
  1565. $form_state['storage']['programmatic_form_submit'] = $form_state['values'];
  1566. }
  1567. /**
  1568. * Form builder to test button click detection.
  1569. */
  1570. function form_test_clicked_button($form, &$form_state) {
  1571. // A single text field. In IE, when a form has only one non-button input field
  1572. // and the ENTER key is pressed while that field has focus, the form is
  1573. // submitted without any information identifying the button responsible for
  1574. // the submission. In other browsers, the form is submitted as though the
  1575. // first button were clicked.
  1576. $form['text'] = array(
  1577. '#title' => 'Text',
  1578. '#type' => 'textfield',
  1579. );
  1580. // Loop through each path argument, addding buttons based on the information
  1581. // in the argument. For example, if the path is
  1582. // form-test/clicked-button/s/i/rb, then 3 buttons are added: a 'submit', an
  1583. // 'image_button', and a 'button' with #access=FALSE. This enables form.test
  1584. // to test a variety of combinations.
  1585. $i=0;
  1586. $args = array_slice(arg(), 2);
  1587. foreach ($args as $arg) {
  1588. $name = 'button' . ++$i;
  1589. // 's', 'b', or 'i' in the argument define the button type wanted.
  1590. if (strpos($arg, 's') !== FALSE) {
  1591. $type = 'submit';
  1592. }
  1593. elseif (strpos($arg, 'b') !== FALSE) {
  1594. $type = 'button';
  1595. }
  1596. elseif (strpos($arg, 'i') !== FALSE) {
  1597. $type = 'image_button';
  1598. }
  1599. else {
  1600. $type = NULL;
  1601. }
  1602. if (isset($type)) {
  1603. $form[$name] = array(
  1604. '#type' => $type,
  1605. '#name' => $name,
  1606. );
  1607. // Image buttons need a #src; the others need a #value.
  1608. if ($type == 'image_button') {
  1609. $form[$name]['#src'] = 'misc/druplicon.png';
  1610. }
  1611. else {
  1612. $form[$name]['#value'] = $name;
  1613. }
  1614. // 'r' for restricted, so we can test that button click detection code
  1615. // correctly takes #access security into account.
  1616. if (strpos($arg, 'r') !== FALSE) {
  1617. $form[$name]['#access'] = FALSE;
  1618. }
  1619. }
  1620. }
  1621. return $form;
  1622. }
  1623. /**
  1624. * Form validation handler for the form_test_clicked_button() form.
  1625. */
  1626. function form_test_clicked_button_validate($form, &$form_state) {
  1627. if (isset($form_state['triggering_element'])) {
  1628. drupal_set_message(t('The clicked button is %name.', array('%name' => $form_state['triggering_element']['#name'])));
  1629. }
  1630. else {
  1631. drupal_set_message('There is no clicked button.');
  1632. }
  1633. }
  1634. /**
  1635. * Form submit handler for the form_test_clicked_button() form.
  1636. */
  1637. function form_test_clicked_button_submit($form, &$form_state) {
  1638. drupal_set_message('Submit handler for form_test_clicked_button executed.');
  1639. }
  1640. /**
  1641. * Form builder to detect form redirect.
  1642. */
  1643. function form_test_redirect($form, &$form_state) {
  1644. $form['redirection'] = array(
  1645. '#type' => 'checkbox',
  1646. '#title' => t('Use redirection'),
  1647. );
  1648. $form['destination'] = array(
  1649. '#type' => 'textfield',
  1650. '#title' => t('Redirect destination'),
  1651. '#states' => array(
  1652. 'visible' => array(
  1653. ':input[name="redirection"]' => array('checked' => TRUE),
  1654. ),
  1655. ),
  1656. );
  1657. $form['submit'] = array(
  1658. '#type' => 'submit',
  1659. '#value' => t('Submit'),
  1660. );
  1661. return $form;
  1662. }
  1663. /**
  1664. * Form submit handler to test different redirect behaviours.
  1665. */
  1666. function form_test_redirect_submit(&$form, &$form_state) {
  1667. if (!empty($form_state['values']['redirection'])) {
  1668. $form_state['redirect'] = !empty($form_state['values']['destination']) ? $form_state['values']['destination'] : NULL;
  1669. }
  1670. else {
  1671. $form_state['redirect'] = FALSE;
  1672. }
  1673. }
  1674. /**
  1675. * Implements hook_form_FORM_ID_alter() for the registration form.
  1676. */
  1677. function form_test_form_user_register_form_alter(&$form, &$form_state) {
  1678. $form['test_rebuild'] = array(
  1679. '#type' => 'submit',
  1680. '#value' => t('Rebuild'),
  1681. '#submit' => array('form_test_user_register_form_rebuild'),
  1682. );
  1683. // If requested, add the test field by attaching the node page form.
  1684. if (!empty($_REQUEST['field'])) {
  1685. $node = (object)array('type' => 'page');
  1686. field_attach_form('node', $node, $form, $form_state);
  1687. }
  1688. }
  1689. /**
  1690. * Submit callback that just lets the form rebuild.
  1691. */
  1692. function form_test_user_register_form_rebuild($form, &$form_state) {
  1693. drupal_set_message('Form rebuilt.');
  1694. $form_state['rebuild'] = TRUE;
  1695. }
  1696. /**
  1697. * Menu callback that returns two instances of the node form.
  1698. */
  1699. function form_test_two_instances() {
  1700. global $user;
  1701. $node1 = (object) array(
  1702. 'uid' => $user->uid,
  1703. 'name' => (isset($user->name) ? $user->name : ''),
  1704. 'type' => 'page',
  1705. 'language' => LANGUAGE_NONE,
  1706. );
  1707. $node2 = clone($node1);
  1708. $return['node_form_1'] = drupal_get_form('page_node_form', $node1);
  1709. $return['node_form_2'] = drupal_get_form('page_node_form', $node2);
  1710. return $return;
  1711. }
  1712. /**
  1713. * Menu callback for testing custom form includes.
  1714. */
  1715. function form_test_load_include_custom($form, &$form_state) {
  1716. $form['button'] = array(
  1717. '#type' => 'submit',
  1718. '#value' => t('Save'),
  1719. '#submit' => array('form_test_load_include_submit'),
  1720. );
  1721. // Specify the include file and enable form caching. That way the form is
  1722. // cached when it is submitted, but needs to find the specified submit handler
  1723. // in the include.
  1724. // Filename is a bit weird here: modules/simpletest/tests/form_test.file.inc
  1725. form_load_include($form_state, 'inc', 'form_test', 'form_test.file');
  1726. $form_state['cache'] = TRUE;
  1727. return $form;
  1728. }
  1729. function form_test_checkbox_type_juggling($form, $form_state, $default_value, $return_value) {
  1730. $form['checkbox'] = array(
  1731. '#type' => 'checkbox',
  1732. '#return_value' => $return_value,
  1733. '#default_value' => $default_value,
  1734. );
  1735. return $form;
  1736. }
  1737. function form_test_checkboxes_zero($form, &$form_state, $json = TRUE) {
  1738. $form['checkbox_off'] = array(
  1739. '#type' => 'checkboxes',
  1740. '#options' => array('foo', 'bar', 'baz'),
  1741. );
  1742. $form['checkbox_zero_default'] = array(
  1743. '#type' => 'checkboxes',
  1744. '#options' => array('foo', 'bar', 'baz'),
  1745. '#default_value' => array(0),
  1746. );
  1747. $form['checkbox_string_zero_default'] = array(
  1748. '#type' => 'checkboxes',
  1749. '#options' => array('foo', 'bar', 'baz'),
  1750. '#default_value' => array('0'),
  1751. );
  1752. $form['submit'] = array(
  1753. '#type' => 'submit',
  1754. '#value' => 'Save',
  1755. );
  1756. if ($json) {
  1757. $form['#submit'][] = '_form_test_checkbox_submit';
  1758. }
  1759. else {
  1760. $form['#submit'][] = '_form_test_checkboxes_zero_no_redirect';
  1761. }
  1762. return $form;
  1763. }
  1764. function _form_test_checkboxes_zero_no_redirect($form, &$form_state) {
  1765. $form_state['redirect'] = FALSE;
  1766. }
  1767. /**
  1768. * Menu callback returns two instances of the same form.
  1769. */
  1770. function form_test_double_form() {
  1771. return array(
  1772. 'form1' => drupal_get_form('form_test_html_id'),
  1773. 'form2' => drupal_get_form('form_test_html_id'),
  1774. );
  1775. }
  1776. /**
  1777. * Builds a simple form to test duplicate HTML IDs.
  1778. */
  1779. function form_test_html_id($form, &$form_state) {
  1780. $form['name'] = array(
  1781. '#type' => 'textfield',
  1782. '#title' => 'name',
  1783. '#required' => TRUE,
  1784. );
  1785. $form['submit'] = array(
  1786. '#type' => 'submit',
  1787. '#value' => 'Save',
  1788. );
  1789. return $form;
  1790. }