form_test.module

You are here

  1. 7 modules/simpletest/tests/form_test.module
  2. 8 core/modules/system/tests/modules/form_test/form_test.module

Helper module for the form API tests.

Functions

Namesort descending Description
block_form_form_test_alter_form_alter Implements hook_form_FORM_ID_alter() on behalf of block.module.
form_label_test_form A form for testing form labels and required marks.
form_storage_test_form_continue_submit Form submit handler to continue multi-step form.
form_test_alter_form Form builder for testing hook_form_alter() and hook_form_FORM_ID_alter().
form_test_checkboxes_radios Form constructor to test expansion of #type checkboxes and radios.
form_test_checkboxes_zero
form_test_checkbox_type_juggling
form_test_clicked_button Form builder to test button click detection.
form_test_clicked_button_submit Form submit handler for the form_test_clicked_button() form.
form_test_clicked_button_validate Form validation handler for the form_test_clicked_button() form.
form_test_double_form Menu callback returns two instances of the same form.
form_test_element_validate_name Form element validation handler for 'name' in form_test_validate_form().
form_test_form_alter Implements hook_form_alter().
form_test_form_form_test_alter_form_alter Implements hook_form_FORM_ID_alter().
form_test_form_form_test_state_persist_alter Implements hook_form_FORM_ID_alter().
form_test_form_rebuild_preserve_values_form Form builder for testing preservation of values during a rebuild.
form_test_form_rebuild_preserve_values_form_add_more Button submit handler for form_test_form_rebuild_preserve_values_form().
form_test_form_rebuild_preserve_values_form_submit Form submit handler for form_test_form_rebuild_preserve_values_form().
form_test_form_state_values_clean_advanced_form Form constructor for the form_state_values_clean() test.
form_test_form_state_values_clean_advanced_form_submit Form submission handler for form_test_form_state_values_clean_advanced_form().
form_test_form_state_values_clean_form Form builder for form_state_values_clean() test.
form_test_form_state_values_clean_form_submit Form submit handler for form_state_values_clean() test form.
form_test_form_user_register_form_alter Implements hook_form_FORM_ID_alter() for the registration form.
form_test_html_id Builds a simple form to test duplicate HTML IDs.
form_test_limit_validation_errors_element_validate_test Form element validation handler for the 'test' element.
form_test_limit_validation_errors_form Builds a simple form with a button triggering partial validation.
form_test_limit_validation_errors_form_partial_submit Form submit handler for the partial validation submit button.
form_test_load_include_custom Menu callback for testing custom form includes.
form_test_menu Implements hook_menu().
form_test_programmatic_form Form builder to test programmatic form submissions.
form_test_programmatic_form_submit Form submit handler for programmatic form submissions.
form_test_programmatic_form_validate Form validation handler for programmatic form submissions.
form_test_redirect Form builder to detect form redirect.
form_test_redirect_submit Form submit handler to test different redirect behaviours.
form_test_select Builds a form to test #type 'select' validation.
form_test_select_submit Form submit handler for form_test_select().
form_test_state_persist Form constructor for testing form state persistence.
form_test_state_persist_submit Submit handler.
form_test_storage_element_validate_value_cached Form element validation handler for 'value' element in form_test_storage_form().
form_test_storage_form A multistep form for testing the form storage.
form_test_storage_form_submit Form submit handler to finish multi-step form.
form_test_storage_legacy_handler Emulate legacy AHAH-style ajax callback.
form_test_storage_page_cache_form A simple form for testing form storage when page caching is enabled.
form_test_storage_page_cache_old_build_id Form element #after_build callback: output the old form build-id.
form_test_storage_page_cache_rebuild Form submit callback: Rebuild the form and continue.
form_test_two_instances Menu callback that returns two instances of the node form.
form_test_user_register_form_rebuild Submit callback that just lets the form rebuild.
form_test_validate_form Form builder for testing drupal_validate_form().
form_test_validate_form_validate Form validation handler for form_test_validate_form().
form_test_validate_required_form Form constructor to test the #required property.
form_test_validate_required_form_no_title Form constructor to test the #required property without #title.
form_test_validate_required_form_no_title_submit Form submission handler for form_test_validate_required_form_no_title().
form_test_validate_required_form_submit Form submission handler for form_test_validate_required_form().
form_test_wrapper_callback Menu callback; Invokes a form builder function with a wrapper callback.
form_test_wrapper_callback_form Form builder for form wrapper callback test.
form_test_wrapper_callback_wrapper Form wrapper for form_test_wrapper_callback_form().
system_form_form_test_alter_form_alter Implements hook_form_FORM_ID_alter() on behalf of system.module.
_form_test_checkbox Build a form to test a checkbox.
_form_test_checkboxes_zero_no_redirect
_form_test_checkbox_submit Return the form values via JSON.
_form_test_disabled_elements Build a form to test disabled elements.
_form_test_disabled_elements_submit Return the form values via JSON.
_form_test_input_forgery Build a form to test input forgery of enabled elements.
_form_test_input_forgery_submit Return the form values via JSON.
_form_test_submit_values_json Form submit handler to return form values as JSON.
_form_test_tableselect_empty_form Test functionality of the tableselect #empty property.
_form_test_tableselect_form_builder Build a form to test the tableselect element.
_form_test_tableselect_get_data Create a header and options array. Helper function for callbacks.
_form_test_tableselect_js_select_form Test functionality of the tableselect #js_select property.
_form_test_tableselect_multiple_false_form Test the tableselect #multiple = FALSE functionality.
_form_test_tableselect_multiple_false_form_submit Process the tableselect #multiple = FALSE submitted values.
_form_test_tableselect_multiple_true_form Test the tableselect #multiple = TRUE functionality.
_form_test_tableselect_multiple_true_form_submit Process the tableselect #multiple = TRUE submitted values.
_form_test_vertical_tabs_form Tests functionality of vertical tabs.

File

modules/simpletest/tests/form_test.module
View source
  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. '#type' => 'tableselect',
  535. '#header' => $header,
  536. '#options' => $options,
  537. '#multiple' => FALSE,
  538. '#empty' => t('Empty text.'),
  539. );
  540. $form['submit'] = array(
  541. '#type' => 'submit',
  542. '#value' => t('Submit'),
  543. );
  544. return $form;
  545. }
  546. /**
  547. * Test the tableselect #multiple = TRUE functionality.
  548. */
  549. function _form_test_tableselect_multiple_true_form($form, $form_state) {
  550. return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => TRUE));
  551. }
  552. /**
  553. * Process the tableselect #multiple = TRUE submitted values.
  554. */
  555. function _form_test_tableselect_multiple_true_form_submit($form, &$form_state) {
  556. $selected = $form_state['values']['tableselect'];
  557. foreach ($selected as $key => $value) {
  558. drupal_set_message(t('Submitted: @key = @value', array('@key' => $key, '@value' => $value)));
  559. }
  560. }
  561. /**
  562. * Test the tableselect #multiple = FALSE functionality.
  563. */
  564. function _form_test_tableselect_multiple_false_form($form, $form_state) {
  565. return _form_test_tableselect_form_builder($form, $form_state, array('#multiple' => FALSE));
  566. }
  567. /**
  568. * Process the tableselect #multiple = FALSE submitted values.
  569. */
  570. function _form_test_tableselect_multiple_false_form_submit($form, &$form_state) {
  571. drupal_set_message(t('Submitted: @value', array('@value' => $form_state['values']['tableselect'])));
  572. }
  573. /**
  574. * Test functionality of the tableselect #empty property.
  575. */
  576. function _form_test_tableselect_empty_form($form, $form_state) {
  577. return _form_test_tableselect_form_builder($form, $form_state, array('#options' => array()));
  578. }
  579. /**
  580. * Test functionality of the tableselect #js_select property.
  581. */
  582. function _form_test_tableselect_js_select_form($form, $form_state, $action) {
  583. switch ($action) {
  584. case 'multiple-true-default':
  585. $options = array('#multiple' => TRUE);
  586. break;
  587. case 'multiple-false-default':
  588. $options = array('#multiple' => FALSE);
  589. break;
  590. case 'multiple-true-no-advanced-select':
  591. $options = array('#multiple' => TRUE, '#js_select' => FALSE);
  592. break;
  593. case 'multiple-false-advanced-select':
  594. $options = array('#multiple' => FALSE, '#js_select' => TRUE);
  595. break;
  596. }
  597. return _form_test_tableselect_form_builder($form, $form_state, $options);
  598. }
  599. /**
  600. * Tests functionality of vertical tabs.
  601. */
  602. function _form_test_vertical_tabs_form($form, &$form_state) {
  603. $form['vertical_tabs'] = array(
  604. '#type' => 'vertical_tabs',
  605. );
  606. $form['tab1'] = array(
  607. '#type' => 'fieldset',
  608. '#title' => t('Tab 1'),
  609. '#collapsible' => TRUE,
  610. '#group' => 'vertical_tabs',
  611. );
  612. $form['tab1']['field1'] = array(
  613. '#title' => t('Field 1'),
  614. '#type' => 'textfield',
  615. );
  616. $form['tab2'] = array(
  617. '#type' => 'fieldset',
  618. '#title' => t('Tab 2'),
  619. '#collapsible' => TRUE,
  620. '#group' => 'vertical_tabs',
  621. );
  622. $form['tab2']['field2'] = array(
  623. '#title' => t('Field 2'),
  624. '#type' => 'textfield',
  625. );
  626. return $form;
  627. }
  628. /**
  629. * A multistep form for testing the form storage.
  630. *
  631. * It uses two steps for editing a virtual "thing". Any changes to it are saved
  632. * in the form storage and have to be present during any step. By setting the
  633. * request parameter "cache" the form can be tested with caching enabled, as
  634. * it would be the case, if the form would contain some #ajax callbacks.
  635. *
  636. * @see form_test_storage_form_submit()
  637. */
  638. function form_test_storage_form($form, &$form_state) {
  639. if ($form_state['rebuild']) {
  640. $form_state['input'] = array();
  641. }
  642. // Initialize
  643. if (empty($form_state['storage'])) {
  644. if (empty($form_state['input'])) {
  645. $_SESSION['constructions'] = 0;
  646. }
  647. // Put the initial thing into the storage
  648. $form_state['storage'] = array(
  649. 'thing' => array(
  650. 'title' => 'none',
  651. 'value' => '',
  652. ),
  653. );
  654. }
  655. // Count how often the form is constructed.
  656. $_SESSION['constructions']++;
  657. drupal_set_message("Form constructions: " . $_SESSION['constructions']);
  658. $form['title'] = array(
  659. '#type' => 'textfield',
  660. '#title' => 'Title',
  661. '#default_value' => $form_state['storage']['thing']['title'],
  662. '#required' => TRUE,
  663. );
  664. $form['value'] = array(
  665. '#type' => 'textfield',
  666. '#title' => 'Value',
  667. '#default_value' => $form_state['storage']['thing']['value'],
  668. '#element_validate' => array('form_test_storage_element_validate_value_cached'),
  669. );
  670. $form['continue_button'] = array(
  671. '#type' => 'button',
  672. '#value' => 'Reset',
  673. // Rebuilds the form without keeping the values.
  674. );
  675. $form['continue_submit'] = array(
  676. '#type' => 'submit',
  677. '#value' => 'Continue submit',
  678. '#submit' => array('form_storage_test_form_continue_submit'),
  679. );
  680. $form['submit'] = array(
  681. '#type' => 'submit',
  682. '#value' => 'Save',
  683. );
  684. if (isset($_REQUEST['cache'])) {
  685. // Manually activate caching, so we can test that the storage keeps working
  686. // when it's enabled.
  687. $form_state['cache'] = TRUE;
  688. }
  689. if (isset($_REQUEST['immutable'])) {
  690. $form_state['build_info']['immutable'] = TRUE;
  691. }
  692. return $form;
  693. }
  694. /**
  695. * Emulate legacy AHAH-style ajax callback.
  696. *
  697. * Drupal 6 AHAH callbacks used to operate directly on forms retrieved using
  698. * form_get_cache and stored using form_set_cache after manipulation. This
  699. * callback helps testing whether form_set_cache prevents resaving of immutable
  700. * forms.
  701. */
  702. function form_test_storage_legacy_handler($form_build_id) {
  703. $form_state = array();
  704. $form = form_get_cache($form_build_id, $form_state);
  705. drupal_json_output(array(
  706. 'form' => $form,
  707. 'form_state' => $form_state,
  708. ));
  709. $form['#poisoned'] = TRUE;
  710. $form_state['poisoned'] = TRUE;
  711. form_set_cache($form_build_id, $form, $form_state);
  712. }
  713. /**
  714. * Form element validation handler for 'value' element in form_test_storage_form().
  715. *
  716. * Tests updating of cached form storage during validation.
  717. */
  718. function form_test_storage_element_validate_value_cached($element, &$form_state) {
  719. // If caching is enabled and we receive a certain value, change the storage.
  720. // This presumes that another submitted form value triggers a validation error
  721. // elsewhere in the form. Form API should still update the cached form storage
  722. // though.
  723. if (isset($_REQUEST['cache']) && $form_state['values']['value'] == 'change_title') {
  724. $form_state['storage']['thing']['changed'] = TRUE;
  725. }
  726. }
  727. /**
  728. * Form submit handler to continue multi-step form.
  729. */
  730. function form_storage_test_form_continue_submit($form, &$form_state) {
  731. $form_state['storage']['thing']['title'] = $form_state['values']['title'];
  732. $form_state['storage']['thing']['value'] = $form_state['values']['value'];
  733. $form_state['rebuild'] = TRUE;
  734. }
  735. /**
  736. * Form submit handler to finish multi-step form.
  737. */
  738. function form_test_storage_form_submit($form, &$form_state) {
  739. drupal_set_message("Title: " . check_plain($form_state['values']['title']));
  740. drupal_set_message("Form constructions: " . $_SESSION['constructions']);
  741. if (isset($form_state['storage']['thing']['changed'])) {
  742. drupal_set_message("The thing has been changed.");
  743. }
  744. $form_state['redirect'] = 'node';
  745. }
  746. /**
  747. * A simple form for testing form storage when page caching is enabled.
  748. */
  749. function form_test_storage_page_cache_form($form, &$form_state) {
  750. $form['title'] = array(
  751. '#type' => 'textfield',
  752. '#title' => 'Title',
  753. '#required' => TRUE,
  754. );
  755. $form['test_build_id_old'] = array(
  756. '#type' => 'item',
  757. '#title' => 'Old build id',
  758. '#markup' => 'No old build id',
  759. );
  760. $form['submit'] = array(
  761. '#type' => 'submit',
  762. '#value' => 'Save',
  763. );
  764. $form['rebuild'] = array(
  765. '#type' => 'submit',
  766. '#value' => 'Rebuild',
  767. '#submit' => array('form_test_storage_page_cache_rebuild'),
  768. );
  769. $form['#after_build'] = array('form_test_storage_page_cache_old_build_id');
  770. $form_state['cache'] = TRUE;
  771. return $form;
  772. }
  773. /**
  774. * Form element #after_build callback: output the old form build-id.
  775. */
  776. function form_test_storage_page_cache_old_build_id($form) {
  777. if (isset($form['#build_id_old'])) {
  778. $form['test_build_id_old']['#markup'] = check_plain($form['#build_id_old']);
  779. }
  780. return $form;
  781. }
  782. /**
  783. * Form submit callback: Rebuild the form and continue.
  784. */
  785. function form_test_storage_page_cache_rebuild($form, &$form_state) {
  786. $form_state['rebuild'] = TRUE;
  787. }
  788. /**
  789. * A form for testing form labels and required marks.
  790. */
  791. function form_label_test_form() {
  792. $form['form_checkboxes_test'] = array(
  793. '#type' => 'checkboxes',
  794. '#title' => t('Checkboxes test'),
  795. '#options' => array(
  796. 'first-checkbox' => t('First checkbox'),
  797. 'second-checkbox' => t('Second checkbox'),
  798. 'third-checkbox' => t('Third checkbox'),
  799. '0' => t('0'),
  800. ),
  801. );
  802. $form['form_radios_test'] = array(
  803. '#type' => 'radios',
  804. '#title' => t('Radios test'),
  805. '#options' => array(
  806. 'first-radio' => t('First radio'),
  807. 'second-radio' => t('Second radio'),
  808. 'third-radio' => t('Third radio'),
  809. '0' => t('0'),
  810. ),
  811. // Test #field_prefix and #field_suffix placement.
  812. '#field_prefix' => '<span id="form-test-radios-field-prefix">' . t('Radios #field_prefix element') . '</span>',
  813. '#field_suffix' => '<span id="form-test-radios-field-suffix">' . t('Radios #field_suffix element') . '</span>',
  814. );
  815. $form['form_checkbox_test'] = array(
  816. '#type' => 'checkbox',
  817. '#title' => t('Checkbox test'),
  818. );
  819. $form['form_textfield_test_title_and_required'] = array(
  820. '#type' => 'textfield',
  821. '#title' => t('Textfield test for required with title'),
  822. '#required' => TRUE,
  823. );
  824. $form['form_textfield_test_no_title_required'] = array(
  825. '#type' => 'textfield',
  826. // We use an empty title, since not setting #title suppresses the label
  827. // and required marker.
  828. '#title' => '',
  829. '#required' => TRUE,
  830. );
  831. $form['form_textfield_test_title'] = array(
  832. '#type' => 'textfield',
  833. '#title' => t('Textfield test for title only'),
  834. // Not required.
  835. // Test #prefix and #suffix placement.
  836. '#prefix' => '<div id="form-test-textfield-title-prefix">' . t('Textfield #prefix element') . '</div>',
  837. '#suffix' => '<div id="form-test-textfield-title-suffix">' . t('Textfield #suffix element') . '</div>',
  838. );
  839. $form['form_textfield_test_title_after'] = array(
  840. '#type' => 'textfield',
  841. '#title' => t('Textfield test for title after element'),
  842. '#title_display' => 'after',
  843. );
  844. $form['form_textfield_test_title_invisible'] = array(
  845. '#type' => 'textfield',
  846. '#title' => t('Textfield test for invisible title'),
  847. '#title_display' => 'invisible',
  848. );
  849. // Textfield test for title set not to display.
  850. $form['form_textfield_test_title_no_show'] = array(
  851. '#type' => 'textfield',
  852. );
  853. // Checkboxes & radios with title as attribute.
  854. $form['form_checkboxes_title_attribute'] = array(
  855. '#type' => 'checkboxes',
  856. '#title' => 'Checkboxes test',
  857. '#options' => array(
  858. 'first-checkbox' => 'First checkbox',
  859. 'second-checkbox' => 'Second checkbox',
  860. ),
  861. '#title_display' => 'attribute',
  862. '#required' => TRUE,
  863. );
  864. $form['form_radios_title_attribute'] = array(
  865. '#type' => 'radios',
  866. '#title' => 'Radios test',
  867. '#options' => array(
  868. 'first-radio' => 'First radio',
  869. 'second-radio' => 'Second radio',
  870. ),
  871. '#title_display' => 'attribute',
  872. '#required' => TRUE,
  873. );
  874. return $form;
  875. }
  876. /**
  877. * Menu callback; Invokes a form builder function with a wrapper callback.
  878. */
  879. function form_test_wrapper_callback($form_id) {
  880. $form_state = array(
  881. 'build_info' => array('args' => array()),
  882. 'wrapper_callback' => 'form_test_wrapper_callback_wrapper',
  883. );
  884. return drupal_build_form($form_id, $form_state);
  885. }
  886. /**
  887. * Form wrapper for form_test_wrapper_callback_form().
  888. */
  889. function form_test_wrapper_callback_wrapper($form, &$form_state) {
  890. $form['wrapper'] = array('#markup' => 'Form wrapper callback element output.');
  891. return $form;
  892. }
  893. /**
  894. * Form builder for form wrapper callback test.
  895. */
  896. function form_test_wrapper_callback_form($form, &$form_state) {
  897. $form['builder'] = array('#markup' => 'Form builder element output.');
  898. return $form;
  899. }
  900. /**
  901. * Form builder for form_state_values_clean() test.
  902. */
  903. function form_test_form_state_values_clean_form($form, &$form_state) {
  904. // Build an example form containing multiple submit and button elements; not
  905. // only on the top-level.
  906. $form = array('#tree' => TRUE);
  907. $form['foo'] = array('#type' => 'submit', '#value' => t('Submit'));
  908. $form['bar'] = array('#type' => 'submit', '#value' => t('Submit'));
  909. $form['beer'] = array('#type' => 'value', '#value' => 1000);
  910. $form['baz']['foo'] = array('#type' => 'button', '#value' => t('Submit'));
  911. $form['baz']['baz'] = array('#type' => 'submit', '#value' => t('Submit'));
  912. $form['baz']['beer'] = array('#type' => 'value', '#value' => 2000);
  913. return $form;
  914. }
  915. /**
  916. * Form submit handler for form_state_values_clean() test form.
  917. */
  918. function form_test_form_state_values_clean_form_submit($form, &$form_state) {
  919. form_state_values_clean($form_state);
  920. drupal_json_output($form_state['values']);
  921. exit;
  922. }
  923. /**
  924. * Form constructor for the form_state_values_clean() test.
  925. */
  926. function form_test_form_state_values_clean_advanced_form($form, &$form_state) {
  927. // Build an example form containing a managed file and a submit form element.
  928. $form['image'] = array(
  929. '#type' => 'managed_file',
  930. '#title' => t('Image'),
  931. '#upload_location' => 'public://',
  932. '#default_value' => 0,
  933. );
  934. $form['submit'] = array(
  935. '#type' => 'submit',
  936. '#value' => t('Submit'),
  937. );
  938. return $form;
  939. }
  940. /**
  941. * Form submission handler for form_test_form_state_values_clean_advanced_form().
  942. */
  943. function form_test_form_state_values_clean_advanced_form_submit($form, &$form_state) {
  944. form_state_values_clean($form_state);
  945. print t('You WIN!');
  946. exit;
  947. }
  948. /**
  949. * Build a form to test a checkbox.
  950. */
  951. function _form_test_checkbox($form, &$form_state) {
  952. // A required checkbox.
  953. $form['required_checkbox'] = array(
  954. '#type' => 'checkbox',
  955. '#required' => TRUE,
  956. '#title' => 'required_checkbox',
  957. );
  958. // A disabled checkbox should get its default value back.
  959. $form['disabled_checkbox_on'] = array(
  960. '#type' => 'checkbox',
  961. '#disabled' => TRUE,
  962. '#return_value' => 'disabled_checkbox_on',
  963. '#default_value' => 'disabled_checkbox_on',
  964. '#title' => 'disabled_checkbox_on',
  965. );
  966. $form['disabled_checkbox_off'] = array(
  967. '#type' => 'checkbox',
  968. '#disabled' => TRUE,
  969. '#return_value' => 'disabled_checkbox_off',
  970. '#default_value' => NULL,
  971. '#title' => 'disabled_checkbox_off',
  972. );
  973. // A checkbox is active when #default_value == #return_value.
  974. $form['checkbox_on'] = array(
  975. '#type' => 'checkbox',
  976. '#return_value' => 'checkbox_on',
  977. '#default_value' => 'checkbox_on',
  978. '#title' => 'checkbox_on',
  979. );
  980. // But inactive in any other case.
  981. $form['checkbox_off'] = array(
  982. '#type' => 'checkbox',
  983. '#return_value' => 'checkbox_off',
  984. '#default_value' => 'checkbox_on',
  985. '#title' => 'checkbox_off',
  986. );
  987. // Checkboxes with a #return_value of '0' are supported.
  988. $form['zero_checkbox_on'] = array(
  989. '#type' => 'checkbox',
  990. '#return_value' => '0',
  991. '#default_value' => '0',
  992. '#title' => 'zero_checkbox_on',
  993. );
  994. // In that case, passing a #default_value != '0' means that the checkbox is off.
  995. $form['zero_checkbox_off'] = array(
  996. '#type' => 'checkbox',
  997. '#return_value' => '0',
  998. '#default_value' => '1',
  999. '#title' => 'zero_checkbox_off',
  1000. );
  1001. $form['submit'] = array(
  1002. '#type' => 'submit',
  1003. '#value' => t('Submit')
  1004. );
  1005. return $form;
  1006. }
  1007. /**
  1008. * Return the form values via JSON.
  1009. */
  1010. function _form_test_checkbox_submit($form, &$form_state) {
  1011. drupal_json_output($form_state['values']);
  1012. exit();
  1013. }
  1014. /**
  1015. * Builds a form to test #type 'select' validation.
  1016. */
  1017. function form_test_select($form, &$form_state) {
  1018. $base = array(
  1019. '#type' => 'select',
  1020. '#options' => drupal_map_assoc(array('one', 'two', 'three')),
  1021. );
  1022. $form['select'] = $base + array(
  1023. '#title' => '#default_value one',
  1024. '#default_value' => 'one',
  1025. );
  1026. $form['select_required'] = $base + array(
  1027. '#title' => '#default_value one, #required',
  1028. '#required' => TRUE,
  1029. '#default_value' => 'one',
  1030. );
  1031. $form['select_optional'] = $base + array(
  1032. '#title' => '#default_value one, not #required',
  1033. '#required' => FALSE,
  1034. '#default_value' => 'one',
  1035. );
  1036. $form['empty_value'] = $base + array(
  1037. '#title' => '#default_value one, #required, #empty_value 0',
  1038. '#required' => TRUE,
  1039. '#default_value' => 'one',
  1040. '#empty_value' => 0,
  1041. );
  1042. $form['empty_value_one'] = $base + array(
  1043. '#title' => '#default_value = #empty_value, #required',
  1044. '#required' => TRUE,
  1045. '#default_value' => 'one',
  1046. '#empty_value' => 'one',
  1047. );
  1048. $form['no_default'] = $base + array(
  1049. '#title' => 'No #default_value, #required',
  1050. '#required' => TRUE,
  1051. );
  1052. $form['no_default_optional'] = $base + array(
  1053. '#title' => 'No #default_value, not #required',
  1054. '#required' => FALSE,
  1055. '#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.',
  1056. );
  1057. $form['no_default_optional_empty_value'] = $base + array(
  1058. '#title' => 'No #default_value, not #required, #empty_value empty string',
  1059. '#empty_value' => '',
  1060. '#required' => FALSE,
  1061. '#description' => 'Should result in an empty string (due to #empty_value), because it is optional.',
  1062. );
  1063. $form['no_default_empty_option'] = $base + array(
  1064. '#title' => 'No #default_value, #required, #empty_option',
  1065. '#required' => TRUE,
  1066. '#empty_option' => '- Choose -',
  1067. );
  1068. $form['no_default_empty_option_optional'] = $base + array(
  1069. '#title' => 'No #default_value, not #required, #empty_option',
  1070. '#empty_option' => '- Dismiss -',
  1071. '#description' => 'Should result in an empty string (default of #empty_value), because it is optional.',
  1072. );
  1073. $form['no_default_empty_value'] = $base + array(
  1074. '#title' => 'No #default_value, #required, #empty_value 0',
  1075. '#required' => TRUE,
  1076. '#empty_value' => 0,
  1077. '#description' => 'Should never result in 0.',
  1078. );
  1079. $form['no_default_empty_value_one'] = $base + array(
  1080. '#title' => 'No #default_value, #required, #empty_value one',
  1081. '#required' => TRUE,
  1082. '#empty_value' => 'one',
  1083. '#description' => 'A mistakenly assigned #empty_value contained in #options should not be valid.',
  1084. );
  1085. $form['no_default_empty_value_optional'] = $base + array(
  1086. '#title' => 'No #default_value, not #required, #empty_value 0',
  1087. '#required' => FALSE,
  1088. '#empty_value' => 0,
  1089. '#description' => 'Should result in 0, because it is optional.',
  1090. );
  1091. $form['multiple'] = $base + array(
  1092. '#title' => '#multiple, #default_value two',
  1093. '#default_value' => array('two'),
  1094. '#multiple' => TRUE,
  1095. );
  1096. $form['multiple_no_default'] = $base + array(
  1097. '#title' => '#multiple, no #default_value',
  1098. '#multiple' => TRUE,
  1099. );
  1100. $form['multiple_no_default_required'] = $base + array(
  1101. '#title' => '#multiple, #required, no #default_value',
  1102. '#required' => TRUE,
  1103. '#multiple' => TRUE,
  1104. );
  1105. $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  1106. return $form;
  1107. }
  1108. /**
  1109. * Form submit handler for form_test_select().
  1110. */
  1111. function form_test_select_submit($form, &$form_state) {
  1112. drupal_json_output($form_state['values']);
  1113. exit();
  1114. }
  1115. /**
  1116. * Form constructor to test expansion of #type checkboxes and radios.
  1117. */
  1118. function form_test_checkboxes_radios($form, &$form_state, $customize = FALSE) {
  1119. $form['#submit'] = array('_form_test_submit_values_json');
  1120. // Expand #type checkboxes, setting custom element properties for some but not
  1121. // all options.
  1122. $form['checkboxes'] = array(
  1123. '#type' => 'checkboxes',
  1124. '#title' => 'Checkboxes',
  1125. '#options' => array(
  1126. 0 => 'Zero',
  1127. 'foo' => 'Foo',
  1128. 1 => 'One',
  1129. 'bar' => 'Bar',
  1130. '>' => 'Special Char',
  1131. ),
  1132. );
  1133. if ($customize) {
  1134. $form['checkboxes'] += array(
  1135. 'foo' => array(
  1136. '#description' => 'Enable to foo.',
  1137. ),
  1138. 1 => array(
  1139. '#weight' => 10,
  1140. ),
  1141. );
  1142. }
  1143. // Expand #type radios, setting custom element properties for some but not
  1144. // all options.
  1145. $form['radios'] = array(
  1146. '#type' => 'radios',
  1147. '#title' => 'Radios',
  1148. '#options' => array(
  1149. 0 => 'Zero',
  1150. 'foo' => 'Foo',
  1151. 1 => 'One',
  1152. 'bar' => 'Bar',
  1153. '>' => 'Special Char',
  1154. ),
  1155. );
  1156. if ($customize) {
  1157. $form['radios'] += array(
  1158. 'foo' => array(
  1159. '#description' => 'Enable to foo.',
  1160. ),
  1161. 1 => array(
  1162. '#weight' => 10,
  1163. ),
  1164. );
  1165. }
  1166. $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  1167. return $form;
  1168. }
  1169. /**
  1170. * Build a form to test disabled elements.
  1171. */
  1172. function _form_test_disabled_elements($form, &$form_state) {
  1173. // Elements that take a simple default value.
  1174. foreach (array('textfield', 'textarea', 'hidden') as $type) {
  1175. $form[$type] = array(
  1176. '#type' => $type,
  1177. '#title' => $type,
  1178. '#default_value' => $type,
  1179. '#test_hijack_value' => 'HIJACK',
  1180. '#disabled' => TRUE,
  1181. );
  1182. }
  1183. // Multiple values option elements.
  1184. foreach (array('checkboxes', 'select') as $type) {
  1185. $form[$type . '_multiple'] = array(
  1186. '#type' => $type,
  1187. '#title' => $type . ' (multiple)',
  1188. '#options' => array(
  1189. 'test_1' => 'Test 1',
  1190. 'test_2' => 'Test 2',
  1191. ),
  1192. '#multiple' => TRUE,
  1193. '#default_value' => array('test_2' => 'test_2'),
  1194. // The keys of #test_hijack_value need to match the #name of the control.
  1195. // @see FormsTestCase::testDisabledElements()
  1196. '#test_hijack_value' => $type == 'select' ? array('' => 'test_1') : array('test_1' => 'test_1'),
  1197. '#disabled' => TRUE,
  1198. );
  1199. }
  1200. // Single values option elements.
  1201. foreach (array('radios', 'select') as $type) {
  1202. $form[$type . '_single'] = array(
  1203. '#type' => $type,
  1204. '#title' => $type . ' (single)',
  1205. '#options' => array(
  1206. 'test_1' => 'Test 1',
  1207. 'test_2' => 'Test 2',
  1208. ),
  1209. '#multiple' => FALSE,
  1210. '#default_value' => 'test_2',
  1211. '#test_hijack_value' => 'test_1',
  1212. '#disabled' => TRUE,
  1213. );
  1214. }
  1215. // Checkbox and radio.
  1216. foreach (array('checkbox', 'radio') as $type) {
  1217. $form[$type . '_unchecked'] = array(
  1218. '#type' => $type,
  1219. '#title' => $type . ' (unchecked)',
  1220. '#return_value' => 1,
  1221. '#default_value' => 0,
  1222. '#test_hijack_value' => 1,
  1223. '#disabled' => TRUE,
  1224. );
  1225. $form[$type . '_checked'] = array(
  1226. '#type' => $type,
  1227. '#title' => $type . ' (checked)',
  1228. '#return_value' => 1,
  1229. '#default_value' => 1,
  1230. '#test_hijack_value' => NULL,
  1231. '#disabled' => TRUE,
  1232. );
  1233. }
  1234. // Weight.
  1235. $form['weight'] = array(
  1236. '#type' => 'weight',
  1237. '#title' => 'weight',
  1238. '#default_value' => 10,
  1239. '#test_hijack_value' => 5,
  1240. '#disabled' => TRUE,
  1241. );
  1242. // Date.
  1243. $form['date'] = array(
  1244. '#type' => 'date',
  1245. '#title' => 'date',
  1246. '#disabled' => TRUE,
  1247. '#default_value' => array(
  1248. 'day' => 19,
  1249. 'month' => 11,
  1250. 'year' => 1978,
  1251. ),
  1252. '#test_hijack_value' => array(
  1253. 'day' => 20,
  1254. 'month' => 12,
  1255. 'year' => 1979,
  1256. ),
  1257. );
  1258. // The #disabled state should propagate to children.
  1259. $form['disabled_container'] = array(
  1260. '#disabled' => TRUE,
  1261. );
  1262. foreach (array('textfield', 'textarea', 'hidden') as $type) {
  1263. $form['disabled_container']['disabled_container_' . $type] = array(
  1264. '#type' => $type,
  1265. '#title' => $type,
  1266. '#default_value' => $type,
  1267. '#test_hijack_value' => 'HIJACK',
  1268. );
  1269. }
  1270. // Text format.
  1271. $form['text_format'] = array(
  1272. '#type' => 'text_format',
  1273. '#title' => 'Text format',
  1274. '#disabled' => TRUE,
  1275. '#default_value' => 'Text value',
  1276. '#format' => 'plain_text',
  1277. '#expected_value' => array(
  1278. 'value' => 'Text value',
  1279. 'format' => 'plain_text',
  1280. ),
  1281. '#test_hijack_value' => array(
  1282. 'value' => 'HIJACK',
  1283. 'format' => 'filtered_html',
  1284. ),
  1285. );
  1286. // Password fields.
  1287. $form['password'] = array(
  1288. '#type' => 'password',
  1289. '#title' => 'Password',
  1290. '#disabled' => TRUE,
  1291. );
  1292. $form['password_confirm'] = array(
  1293. '#type' => 'password_confirm',
  1294. '#title' => 'Password confirm',
  1295. '#disabled' => TRUE,
  1296. );
  1297. // Files.
  1298. $form['file'] = array(
  1299. '#type' => 'file',
  1300. '#title' => 'File',
  1301. '#disabled' => TRUE,
  1302. );
  1303. $form['managed_file'] = array(
  1304. '#type' => 'managed_file',
  1305. '#title' => 'Managed file',
  1306. '#disabled' => TRUE,
  1307. );
  1308. // Buttons.
  1309. $form['image_button'] = array(
  1310. '#type' => 'image_button',
  1311. '#value' => 'Image button',
  1312. '#disabled' => TRUE,
  1313. );
  1314. $form['button'] = array(
  1315. '#type' => 'button',
  1316. '#value' => 'Button',
  1317. '#disabled' => TRUE,
  1318. );
  1319. $form['submit_disabled'] = array(
  1320. '#type' => 'submit',
  1321. '#value' => 'Submit',
  1322. '#disabled' => TRUE,
  1323. );
  1324. $form['submit'] = array(
  1325. '#type' => 'submit',
  1326. '#value' => t('Submit'),
  1327. );
  1328. return $form;
  1329. }
  1330. /**
  1331. * Return the form values via JSON.
  1332. */
  1333. function _form_test_disabled_elements_submit($form, &$form_state) {
  1334. drupal_json_output($form_state['values']);
  1335. exit();
  1336. }
  1337. /**
  1338. * Build a form to test input forgery of enabled elements.
  1339. */
  1340. function _form_test_input_forgery($form, &$form_state) {
  1341. // For testing that a user can't submit a value not matching one of the
  1342. // allowed options.
  1343. $form['checkboxes'] = array(
  1344. '#type' => 'checkboxes',
  1345. '#options' => array(
  1346. 'one' => 'One',
  1347. 'two' => 'Two',
  1348. ),
  1349. );
  1350. $form['submit'] = array(
  1351. '#type' => 'submit',
  1352. '#value' => t('Submit'),
  1353. );
  1354. return $form;
  1355. }
  1356. /**
  1357. * Return the form values via JSON.
  1358. */
  1359. function _form_test_input_forgery_submit($form, &$form_state) {
  1360. drupal_json_output($form_state['values']);
  1361. exit();
  1362. }
  1363. /**
  1364. * Form builder for testing preservation of values during a rebuild.
  1365. */
  1366. function form_test_form_rebuild_preserve_values_form($form, &$form_state) {
  1367. // Start the form with two checkboxes, to test different defaults, and a
  1368. // textfield, to test more than one element type.
  1369. $form = array(
  1370. 'checkbox_1_default_off' => array(
  1371. '#type' => 'checkbox',
  1372. '#title' => t('This checkbox defaults to unchecked.'),
  1373. '#default_value' => FALSE,
  1374. ),
  1375. 'checkbox_1_default_on' => array(
  1376. '#type' => 'checkbox',
  1377. '#title' => t('This checkbox defaults to checked.'),
  1378. '#default_value' => TRUE,
  1379. ),
  1380. 'text_1' => array(
  1381. '#type' => 'textfield',
  1382. '#title' => t('This textfield has a non-empty default value.'),
  1383. '#default_value' => 'DEFAULT 1',
  1384. ),
  1385. );
  1386. // Provide an 'add more' button that rebuilds the form with an additional two
  1387. // checkboxes and a textfield. The test is to make sure that the rebuild
  1388. // triggered by this button preserves the user input values for the initial
  1389. // elements and initializes the new elements with the correct default values.
  1390. if (empty($form_state['storage']['add_more'])) {
  1391. $form['add_more'] = array(
  1392. '#type' => 'submit',
  1393. '#value' => 'Add more',
  1394. '#submit' => array('form_test_form_rebuild_preserve_values_form_add_more'),
  1395. );
  1396. }
  1397. else {
  1398. $form += array(
  1399. 'checkbox_2_default_off' => array(
  1400. '#type' => 'checkbox',
  1401. '#title' => t('This checkbox defaults to unchecked.'),
  1402. '#default_value' => FALSE,
  1403. ),
  1404. 'checkbox_2_default_on' => array(
  1405. '#type' => 'checkbox',
  1406. '#title' => t('This checkbox defaults to checked.'),
  1407. '#default_value' => TRUE,
  1408. ),
  1409. 'text_2' => array(
  1410. '#type' => 'textfield',
  1411. '#title' => t('This textfield has a non-empty default value.'),
  1412. '#default_value' => 'DEFAULT 2',
  1413. ),
  1414. );
  1415. }
  1416. // A submit button that finishes the form workflow (does not rebuild).
  1417. $form['submit'] = array(
  1418. '#type' => 'submit',
  1419. '#value' => 'Submit',
  1420. );
  1421. return $form;
  1422. }
  1423. /**
  1424. * Button submit handler for form_test_form_rebuild_preserve_values_form().
  1425. */
  1426. function form_test_form_rebuild_preserve_values_form_add_more($form, &$form_state) {
  1427. // Rebuild, to test preservation of input values.
  1428. $form_state['storage']['add_more'] = TRUE;
  1429. $form_state['rebuild'] = TRUE;
  1430. }
  1431. /**
  1432. * Form submit handler for form_test_form_rebuild_preserve_values_form().
  1433. */
  1434. function form_test_form_rebuild_preserve_values_form_submit($form, &$form_state) {
  1435. // Finish the workflow. Do not rebuild.
  1436. drupal_set_message(t('Form values: %values', array('%values' => var_export($form_state['values'], TRUE))));
  1437. }
  1438. /**
  1439. * Form constructor for testing form state persistence.
  1440. */
  1441. function form_test_state_persist($form, &$form_state) {
  1442. $form['title'] = array(
  1443. '#type' => 'textfield',
  1444. '#title' => 'title',
  1445. '#default_value' => 'DEFAULT',
  1446. '#required' => TRUE,
  1447. );
  1448. $form_state['value'] = 'State persisted.';
  1449. $form['submit'] = array(
  1450. '#type' => 'submit',
  1451. '#value' => t('Submit'),
  1452. );
  1453. return $form;
  1454. }
  1455. /**
  1456. * Submit handler.
  1457. *
  1458. * @see form_test_state_persist()
  1459. */
  1460. function form_test_state_persist_submit($form, &$form_state) {
  1461. drupal_set_message($form_state['value']);
  1462. $form_state['rebuild'] = TRUE;
  1463. }
  1464. /**
  1465. * Implements hook_form_FORM_ID_alter().
  1466. *
  1467. * @see form_test_state_persist()
  1468. */
  1469. function form_test_form_form_test_state_persist_alter(&$form, &$form_state) {
  1470. // Simulate a form alter implementation inserting form elements that enable
  1471. // caching of the form, e.g. elements having #ajax.
  1472. if (!empty($_REQUEST['cache'])) {
  1473. $form_state['cache'] = TRUE;
  1474. }
  1475. }
  1476. /**
  1477. * Form builder to test programmatic form submissions.
  1478. */
  1479. function form_test_programmatic_form($form, &$form_state) {
  1480. $form['textfield'] = array(
  1481. '#title' => 'Textfield',
  1482. '#type' => 'textfield',
  1483. );
  1484. $form['checkboxes'] = array(
  1485. '#type' => 'checkboxes',
  1486. '#options' => array(
  1487. 1 => 'First checkbox',
  1488. 2 => 'Second checkbox',
  1489. ),
  1490. // Both checkboxes are selected by default so that we can test the ability
  1491. // of programmatic form submissions to uncheck them.
  1492. '#default_value' => array(1, 2),
  1493. );
  1494. // This is used to test that programmatic form submissions can bypass #access
  1495. // restrictions.
  1496. $form['textfield_no_access'] = array(
  1497. '#type' => 'textfield',
  1498. '#title' => 'Textfield no access',
  1499. '#default_value' => 'default value',
  1500. '#access' => FALSE,
  1501. );
  1502. $form['field_to_validate'] = array(
  1503. '#type' => 'radios',
  1504. '#title' => 'Field to validate (in the case of limited validation)',
  1505. '#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.',
  1506. '#options' => array(
  1507. 'all' => 'Validate all fields',
  1508. 'textfield' => 'Validate the "Textfield" field',
  1509. 'field_to_validate' => 'Validate the "Field to validate" field',
  1510. ),
  1511. '#default_value' => 'all',
  1512. );
  1513. // The main submit button for the form.
  1514. $form['submit'] = array(
  1515. '#type' => 'submit',
  1516. '#value' => 'Submit',
  1517. );
  1518. // A secondary submit button that allows validation to be limited based on
  1519. // the value of the above radio selector.
  1520. $form['submit_limit_validation'] = array(
  1521. '#type' => 'submit',
  1522. '#value' => 'Submit with limited validation',
  1523. // Use the same submit handler for this button as for the form itself.
  1524. // (This must be set explicitly or otherwise the form API will ignore the
  1525. // #limit_validation_errors property.)
  1526. '#submit' => array('form_test_programmatic_form_submit'),
  1527. );
  1528. if (!empty($form_state['input']['field_to_validate']) && $form_state['input']['field_to_validate'] != 'all') {
  1529. $form['submit_limit_validation']['#limit_validation_errors'] = array(
  1530. array($form_state['input']['field_to_validate']),
  1531. );
  1532. }
  1533. return $form;
  1534. }
  1535. /**
  1536. * Form validation handler for programmatic form submissions.
  1537. *
  1538. * To test that the validation handler is correctly executed, the field value is
  1539. * explicitly required here.
  1540. */
  1541. function form_test_programmatic_form_validate($form, &$form_state) {
  1542. if (empty($form_state['values']['textfield'])) {
  1543. form_set_error('textfield', t('Textfield is required.'));
  1544. }
  1545. }
  1546. /**
  1547. * Form submit handler for programmatic form submissions.
  1548. *
  1549. * To test that the submission handler is correctly executed, we store the
  1550. * submitted values in a place we can access from the caller context.
  1551. */
  1552. function form_test_programmatic_form_submit($form, &$form_state) {
  1553. $form_state['storage']['programmatic_form_submit'] = $form_state['values'];
  1554. }
  1555. /**
  1556. * Form builder to test button click detection.
  1557. */
  1558. function form_test_clicked_button($form, &$form_state) {
  1559. // A single text field. In IE, when a form has only one non-button input field
  1560. // and the ENTER key is pressed while that field has focus, the form is
  1561. // submitted without any information identifying the button responsible for
  1562. // the submission. In other browsers, the form is submitted as though the
  1563. // first button were clicked.
  1564. $form['text'] = array(
  1565. '#title' => 'Text',
  1566. '#type' => 'textfield',
  1567. );
  1568. // Loop through each path argument, addding buttons based on the information
  1569. // in the argument. For example, if the path is
  1570. // form-test/clicked-button/s/i/rb, then 3 buttons are added: a 'submit', an
  1571. // 'image_button', and a 'button' with #access=FALSE. This enables form.test
  1572. // to test a variety of combinations.
  1573. $i=0;
  1574. $args = array_slice(arg(), 2);
  1575. foreach ($args as $arg) {
  1576. $name = 'button' . ++$i;
  1577. // 's', 'b', or 'i' in the argument define the button type wanted.
  1578. if (strpos($arg, 's') !== FALSE) {
  1579. $type = 'submit';
  1580. }
  1581. elseif (strpos($arg, 'b') !== FALSE) {
  1582. $type = 'button';
  1583. }
  1584. elseif (strpos($arg, 'i') !== FALSE) {
  1585. $type = 'image_button';
  1586. }
  1587. else {
  1588. $type = NULL;
  1589. }
  1590. if (isset($type)) {
  1591. $form[$name] = array(
  1592. '#type' => $type,
  1593. '#name' => $name,
  1594. );
  1595. // Image buttons need a #src; the others need a #value.
  1596. if ($type == 'image_button') {
  1597. $form[$name]['#src'] = 'misc/druplicon.png';
  1598. }
  1599. else {
  1600. $form[$name]['#value'] = $name;
  1601. }
  1602. // 'r' for restricted, so we can test that button click detection code
  1603. // correctly takes #access security into account.
  1604. if (strpos($arg, 'r') !== FALSE) {
  1605. $form[$name]['#access'] = FALSE;
  1606. }
  1607. }
  1608. }
  1609. return $form;
  1610. }
  1611. /**
  1612. * Form validation handler for the form_test_clicked_button() form.
  1613. */
  1614. function form_test_clicked_button_validate($form, &$form_state) {
  1615. if (isset($form_state['triggering_element'])) {
  1616. drupal_set_message(t('The clicked button is %name.', array('%name' => $form_state['triggering_element']['#name'])));
  1617. }
  1618. else {
  1619. drupal_set_message('There is no clicked button.');
  1620. }
  1621. }
  1622. /**
  1623. * Form submit handler for the form_test_clicked_button() form.
  1624. */
  1625. function form_test_clicked_button_submit($form, &$form_state) {
  1626. drupal_set_message('Submit handler for form_test_clicked_button executed.');
  1627. }
  1628. /**
  1629. * Form builder to detect form redirect.
  1630. */
  1631. function form_test_redirect($form, &$form_state) {
  1632. $form['redirection'] = array(
  1633. '#type' => 'checkbox',
  1634. '#title' => t('Use redirection'),
  1635. );
  1636. $form['destination'] = array(
  1637. '#type' => 'textfield',
  1638. '#title' => t('Redirect destination'),
  1639. '#states' => array(
  1640. 'visible' => array(
  1641. ':input[name="redirection"]' => array('checked' => TRUE),
  1642. ),
  1643. ),
  1644. );
  1645. $form['submit'] = array(
  1646. '#type' => 'submit',
  1647. '#value' => t('Submit'),
  1648. );
  1649. return $form;
  1650. }
  1651. /**
  1652. * Form submit handler to test different redirect behaviours.
  1653. */
  1654. function form_test_redirect_submit(&$form, &$form_state) {
  1655. if (!empty($form_state['values']['redirection'])) {
  1656. $form_state['redirect'] = !empty($form_state['values']['destination']) ? $form_state['values']['destination'] : NULL;
  1657. }
  1658. else {
  1659. $form_state['redirect'] = FALSE;
  1660. }
  1661. }
  1662. /**
  1663. * Implements hook_form_FORM_ID_alter() for the registration form.
  1664. */
  1665. function form_test_form_user_register_form_alter(&$form, &$form_state) {
  1666. $form['test_rebuild'] = array(
  1667. '#type' => 'submit',
  1668. '#value' => t('Rebuild'),
  1669. '#submit' => array('form_test_user_register_form_rebuild'),
  1670. );
  1671. // If requested, add the test field by attaching the node page form.
  1672. if (!empty($_REQUEST['field'])) {
  1673. $node = (object)array('type' => 'page');
  1674. field_attach_form('node', $node, $form, $form_state);
  1675. }
  1676. }
  1677. /**
  1678. * Submit callback that just lets the form rebuild.
  1679. */
  1680. function form_test_user_register_form_rebuild($form, &$form_state) {
  1681. drupal_set_message('Form rebuilt.');
  1682. $form_state['rebuild'] = TRUE;
  1683. }
  1684. /**
  1685. * Menu callback that returns two instances of the node form.
  1686. */
  1687. function form_test_two_instances() {
  1688. global $user;
  1689. $node1 = (object) array(
  1690. 'uid' => $user->uid,
  1691. 'name' => (isset($user->name) ? $user->name : ''),
  1692. 'type' => 'page',
  1693. 'language' => LANGUAGE_NONE,
  1694. );
  1695. $node2 = clone($node1);
  1696. $return['node_form_1'] = drupal_get_form('page_node_form', $node1);
  1697. $return['node_form_2'] = drupal_get_form('page_node_form', $node2);
  1698. return $return;
  1699. }
  1700. /**
  1701. * Menu callback for testing custom form includes.
  1702. */
  1703. function form_test_load_include_custom($form, &$form_state) {
  1704. $form['button'] = array(
  1705. '#type' => 'submit',
  1706. '#value' => t('Save'),
  1707. '#submit' => array('form_test_load_include_submit'),
  1708. );
  1709. // Specify the include file and enable form caching. That way the form is
  1710. // cached when it is submitted, but needs to find the specified submit handler
  1711. // in the include.
  1712. // Filename is a bit weird here: modules/simpletest/tests/form_test.file.inc
  1713. form_load_include($form_state, 'inc', 'form_test', 'form_test.file');
  1714. $form_state['cache'] = TRUE;
  1715. return $form;
  1716. }
  1717. function form_test_checkbox_type_juggling($form, $form_state, $default_value, $return_value) {
  1718. $form['checkbox'] = array(
  1719. '#type' => 'checkbox',
  1720. '#return_value' => $return_value,
  1721. '#default_value' => $default_value,
  1722. );
  1723. return $form;
  1724. }
  1725. function form_test_checkboxes_zero($form, &$form_state, $json = TRUE) {
  1726. $form['checkbox_off'] = array(
  1727. '#type' => 'checkboxes',
  1728. '#options' => array('foo', 'bar', 'baz'),
  1729. );
  1730. $form['checkbox_zero_default'] = array(
  1731. '#type' => 'checkboxes',
  1732. '#options' => array('foo', 'bar', 'baz'),
  1733. '#default_value' => array(0),
  1734. );
  1735. $form['checkbox_string_zero_default'] = array(
  1736. '#type' => 'checkboxes',
  1737. '#options' => array('foo', 'bar', 'baz'),
  1738. '#default_value' => array('0'),
  1739. );
  1740. $form['submit'] = array(
  1741. '#type' => 'submit',
  1742. '#value' => 'Save',
  1743. );
  1744. if ($json) {
  1745. $form['#submit'][] = '_form_test_checkbox_submit';
  1746. }
  1747. else {
  1748. $form['#submit'][] = '_form_test_checkboxes_zero_no_redirect';
  1749. }
  1750. return $form;
  1751. }
  1752. function _form_test_checkboxes_zero_no_redirect($form, &$form_state) {
  1753. $form_state['redirect'] = FALSE;
  1754. }
  1755. /**
  1756. * Menu callback returns two instances of the same form.
  1757. */
  1758. function form_test_double_form() {
  1759. return array(
  1760. 'form1' => drupal_get_form('form_test_html_id'),
  1761. 'form2' => drupal_get_form('form_test_html_id'),
  1762. );
  1763. }
  1764. /**
  1765. * Builds a simple form to test duplicate HTML IDs.
  1766. */
  1767. function form_test_html_id($form, &$form_state) {
  1768. $form['name'] = array(
  1769. '#type' => 'textfield',
  1770. '#title' => 'name',
  1771. '#required' => TRUE,
  1772. );
  1773. $form['submit'] = array(
  1774. '#type' => 'submit',
  1775. '#value' => 'Save',
  1776. );
  1777. return $form;
  1778. }