form_example_tutorial.inc

  1. examples
    1. 6 form_example/form_example_tutorial.inc
    2. 7 form_example/form_example_tutorial.inc
    3. 8 form_example/form_example_tutorial.inc

This is the Form API Tutorial from the handbook.

It goes through nine form examples in increasing complexity to demonstrate Drupal 7 Form API.

Links are provided inline for the related handbook pages.

See also

http://drupal.org/node/262422

Functions & methods

NameDescription
form_example_tutorialMain Form tutorial page.
form_example_tutorial_1This first form function is from the Form Tutorial handbook page
form_example_tutorial_10Example 10: A form with a file upload field.
form_example_tutorial_10_submitSubmit handler for form_example_tutorial_10().
form_example_tutorial_10_validateValidate handler for form_example_tutorial_10().
form_example_tutorial_2This is Example 2, a basic form with a submit button.
form_example_tutorial_3Example 3: A basic form with fieldsets.
form_example_tutorial_4Example 4: Basic form with required fields.
form_example_tutorial_5Example 5: Basic form with additional element attributes.
form_example_tutorial_6Example 6: A basic form with a validate handler.
form_example_tutorial_6_validateNow we add a handler/function to validate the data entered into the "year of birth" field to make sure it's between the values of 1900 and 2000. If not, it displays an error. The value report is $form_state['values'] (see…
form_example_tutorial_7Example 7: With a submit handler.
form_example_tutorial_7_submitSubmit function for form_example_tutorial_7().
form_example_tutorial_7_validateValidation function for form_example_tutorial_7().
form_example_tutorial_8Example 8: A simple multistep form with a Next and a Back button.
form_example_tutorial_8_next_submitSubmit handler for form_example_tutorial_8() next button.
form_example_tutorial_8_next_validateValidate handler for the next button on first page.
form_example_tutorial_8_page_twoReturns the form for the second page of form_example_tutorial_8().
form_example_tutorial_8_page_two_backBack button handler submit handler.
form_example_tutorial_8_page_two_submitThe page 2 submit handler.
form_example_tutorial_9Example 9: A form with a dynamically added new fields.
form_example_tutorial_9_add_nameSubmit handler for "Add another name" button on form_example_tutorial_9().
form_example_tutorial_9_remove_name
form_example_tutorial_9_submitSubmit function for form_example_tutorial_9().
form_example_tutorial_9_validateValidate function for form_example_tutorial_9().

File

form_example/form_example_tutorial.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is the Form API Tutorial from the handbook.
  5. *
  6. * It goes through nine form examples in increasing complexity to demonstrate
  7. * Drupal 7 Form API.
  8. *
  9. * Links are provided inline for the related handbook pages.
  10. *
  11. * @see http://drupal.org/node/262422
  12. */
  13. /**
  14. * Main Form tutorial page.
  15. *
  16. * @see form_example_tutorial_1()
  17. * @see form_example_tutorial_2()
  18. * @see form_example_tutorial_3()
  19. * @see form_example_tutorial_4()
  20. * @see form_example_tutorial_5()
  21. * @see form_example_tutorial_6()
  22. * @see form_example_tutorial_7()
  23. * @see form_example_tutorial_8()
  24. * @see form_example_tutorial_9()
  25. * @see form_example_tutorial_10()
  26. *
  27. * @ingroup form_example
  28. */
  29. function form_example_tutorial() {
  30. return t('This is a set of nine form tutorials tied to the <a href="http://drupal.org/node/262422">Drupal handbook</a>.');
  31. }
  32. //////////////// Tutorial Example 1 //////////////////////
  33. /**
  34. * This first form function is from the @link http://drupal.org/node/717722 Form Tutorial handbook page @endlink
  35. *
  36. * It just creates a very basic form with a textfield.
  37. *
  38. * This function is called the "form constructor function". It builds the form.
  39. * It takes a two arguments, $form and $form_state, but if drupal_get_form()
  40. * sends additional arguments, they will be provided after $form_state.
  41. *
  42. * @ingroup form_example
  43. */
  44. function form_example_tutorial_1($form, &$form_state) {
  45. $form['description'] = array(
  46. '#type' => 'item',
  47. '#title' => t('A form with nothing but a textfield'),
  48. );
  49. // This is the first form element. It's a textfield with a label, "Name"
  50. $form['name'] = array(
  51. '#type' => 'textfield',
  52. '#title' => t('Name'),
  53. );
  54. return $form;
  55. }
  56. //////////////// Tutorial Example 2 //////////////////////
  57. /**
  58. * This is Example 2, a basic form with a submit button.
  59. *
  60. * @see http://drupal.org/node/717726
  61. * @ingroup form_example
  62. */
  63. function form_example_tutorial_2($form, &$form_state) {
  64. $form['description'] = array(
  65. '#type' => 'item',
  66. '#title' => t('A simple form with a submit button'),
  67. );
  68. $form['name'] = array(
  69. '#type' => 'textfield',
  70. '#title' => t('Name'),
  71. );
  72. // Adds a simple submit button that refreshes the form and clears its
  73. // contents. This is the default behavior for forms.
  74. $form['submit'] = array(
  75. '#type' => 'submit',
  76. '#value' => 'Submit',
  77. );
  78. return $form;
  79. }
  80. //////////////// Tutorial Example 3 //////////////////////
  81. /**
  82. * Example 3: A basic form with fieldsets.
  83. *
  84. * We establish a fieldset element and then place two text fields within
  85. * it, one for a first name and one for a last name. This helps us group
  86. * related content.
  87. *
  88. * Study the code below and you'll notice that we renamed the array of the first
  89. * and last name fields by placing them under the $form['name']
  90. * array. This tells Form API these fields belong to the $form['name'] fieldset.
  91. *
  92. * @ingroup form_example
  93. */
  94. function form_example_tutorial_3($form, &$form_state) {
  95. $form['description'] = array(
  96. '#type' => 'item',
  97. '#title' => t('A form with a fieldset'),
  98. );
  99. $form['name'] = array(
  100. '#type' => 'fieldset',
  101. '#title' => t('Name'),
  102. );
  103. $form['name']['first'] = array(
  104. '#type' => 'textfield',
  105. '#title' => t('First name'),
  106. );
  107. $form['name']['last'] = array(
  108. '#type' => 'textfield',
  109. '#title' => t('Last name'),
  110. );
  111. $form['submit'] = array(
  112. '#type' => 'submit',
  113. '#value' => 'Submit',
  114. );
  115. return $form;
  116. }
  117. //////////////// Tutorial Example 4 //////////////////////
  118. /**
  119. * Example 4: Basic form with required fields.
  120. *
  121. * @ingroup form_example
  122. */
  123. function form_example_tutorial_4($form, &$form_state) {
  124. $form['description'] = array(
  125. '#type' => 'item',
  126. '#title' => t('A form with validation'),
  127. );
  128. $form['name'] = array(
  129. '#type' => 'fieldset',
  130. '#title' => t('Name'),
  131. // Make the fieldset collapsible.
  132. '#collapsible' => TRUE, // Added
  133. '#collapsed' => FALSE, // Added
  134. );
  135. // Make these fields required.
  136. $form['name']['first'] = array(
  137. '#type' => 'textfield',
  138. '#title' => t('First name'),
  139. '#required' => TRUE, // Added
  140. );
  141. $form['name']['last'] = array(
  142. '#type' => 'textfield',
  143. '#title' => t('Last name'),
  144. '#required' => TRUE, // Added
  145. );
  146. $form['submit'] = array(
  147. '#type' => 'submit',
  148. '#value' => 'Submit',
  149. );
  150. return $form;
  151. }
  152. //////////////// Tutorial Example 5 //////////////////////
  153. /**
  154. * Example 5: Basic form with additional element attributes.
  155. *
  156. * This demonstrates additional attributes of text form fields.
  157. *
  158. * For a more extensive example on element types
  159. * @see http://drupal.org/node/751826
  160. *
  161. * See the @link http://api.drupal.org/api/file/developer/topics/forms_api.html complete form reference @endlink
  162. *
  163. * @ingroup form_example
  164. */
  165. function form_example_tutorial_5($form, &$form_state) {
  166. $form['description'] = array(
  167. '#type' => 'item',
  168. '#title' => t('A form with additional attributes'),
  169. '#description' => t('This one adds #default_value and #description'),
  170. );
  171. $form['name'] = array(
  172. '#type' => 'fieldset',
  173. '#title' => t('Name'),
  174. '#collapsible' => TRUE,
  175. '#collapsed' => FALSE,
  176. );
  177. $form['name']['first'] = array(
  178. '#type' => 'textfield',
  179. '#title' => t('First name'),
  180. '#required' => TRUE,
  181. '#default_value' => "First name", // added default value.
  182. '#description' => "Please enter your first name.", // added description
  183. '#size' => 20, // added
  184. '#maxlength' => 20, // added
  185. );
  186. $form['name']['last'] = array(
  187. '#type' => 'textfield',
  188. '#title' => t('Last name'),
  189. '#required' => TRUE,
  190. );
  191. $form['submit'] = array(
  192. '#type' => 'submit',
  193. '#value' => 'Submit',
  194. );
  195. return $form;
  196. }
  197. //////////////// Tutorial Example 6 //////////////////////
  198. /**
  199. * Example 6: A basic form with a validate handler.
  200. *
  201. * From http://drupal.org/node/717736
  202. * @see form_example_tutorial_6_validate()
  203. */
  204. function form_example_tutorial_6($form, &$form_state) {
  205. $form['description'] = array(
  206. '#type' => 'item',
  207. '#title' => t('A form with a validation handler'),
  208. );
  209. $form['name'] = array(
  210. '#type' => 'fieldset',
  211. '#title' => t('Name'),
  212. '#collapsible' => TRUE,
  213. '#collapsed' => FALSE,
  214. );
  215. $form['name']['first'] = array(
  216. '#type' => 'textfield',
  217. '#title' => t('First name'),
  218. '#required' => TRUE,
  219. '#default_value' => "First name",
  220. '#description' => "Please enter your first name.",
  221. '#size' => 20,
  222. '#maxlength' => 20,
  223. );
  224. $form['name']['last'] = array(
  225. '#type' => 'textfield',
  226. '#title' => t('Last name'),
  227. '#required' => TRUE,
  228. );
  229. // New form field added to permit entry of year of birth.
  230. // The data entered into this field will be validated with
  231. // the default validation function.
  232. $form['year_of_birth'] = array(
  233. '#type' => 'textfield',
  234. '#title' => "Year of birth",
  235. '#description' => 'Format is "YYYY"',
  236. );
  237. $form['submit'] = array(
  238. '#type' => 'submit',
  239. '#value' => 'Submit',
  240. );
  241. return $form;
  242. }
  243. /**
  244. * Now we add a handler/function to validate the data entered into the
  245. * "year of birth" field to make sure it's between the values of 1900
  246. * and 2000. If not, it displays an error. The value report is
  247. * $form_state['values'] (see http://drupal.org/node/144132#form-state).
  248. *
  249. * Notice the name of the function. It is simply the name of the form
  250. * followed by '_validate'. This is always the name of the default validation
  251. * function. An alternate list of validation functions could have been provided
  252. * in $form['#validate'].
  253. *
  254. * @see form_example_tutorial_6()
  255. */
  256. function form_example_tutorial_6_validate($form, &$form_state) {
  257. $year_of_birth = $form_state['values']['year_of_birth'];
  258. if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
  259. form_set_error('year_of_birth', t('Enter a year between 1900 and 2000.'));
  260. }
  261. }
  262. //////////////// Tutorial Example 7 //////////////////////
  263. /**
  264. * Example 7: With a submit handler.
  265. *
  266. * From the handbook page:
  267. * http://drupal.org/node/717740
  268. *
  269. * @see form_example_tutorial_7_validate()
  270. * @see form_example_tutorial_7_submit()
  271. * @ingroup form_example
  272. */
  273. function form_example_tutorial_7($form, &$form_state) {
  274. $form['description'] = array(
  275. '#type' => 'item',
  276. '#title' => t('A form with a submit handler'),
  277. );
  278. $form['name'] = array(
  279. '#type' => 'fieldset',
  280. '#title' => t('Name'),
  281. '#collapsible' => TRUE,
  282. '#collapsed' => FALSE,
  283. );
  284. $form['name']['first'] = array(
  285. '#type' => 'textfield',
  286. '#title' => t('First name'),
  287. '#required' => TRUE,
  288. '#default_value' => "First name",
  289. '#description' => "Please enter your first name.",
  290. '#size' => 20,
  291. '#maxlength' => 20,
  292. );
  293. $form['name']['last'] = array(
  294. '#type' => 'textfield',
  295. '#title' => t('Last name'),
  296. '#required' => TRUE,
  297. );
  298. $form['year_of_birth'] = array(
  299. '#type' => 'textfield',
  300. '#title' => "Year of birth",
  301. '#description' => 'Format is "YYYY"',
  302. );
  303. $form['submit'] = array(
  304. '#type' => 'submit',
  305. '#value' => 'Submit',
  306. );
  307. return $form;
  308. }
  309. /**
  310. * Validation function for form_example_tutorial_7().
  311. */
  312. function form_example_tutorial_7_validate($form, &$form_state) {
  313. $year_of_birth = $form_state['values']['year_of_birth'];
  314. if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
  315. form_set_error('year_of_birth', t('Enter a year between 1900 and 2000.'));
  316. }
  317. }
  318. /**
  319. * Submit function for form_example_tutorial_7().
  320. *
  321. * Adds a submit handler/function to our form to send a successful
  322. * completion message to the screen.
  323. */
  324. function form_example_tutorial_7_submit($form, &$form_state) {
  325. drupal_set_message(t('The form has been submitted. name="@first @last", year of birth=@year_of_birth',
  326. array('@first' => $form_state['values']['first'], '@last' => $form_state['values']['last'], '@year_of_birth' => $form_state['values']['year_of_birth'])));
  327. }
  328. //////////////// Tutorial Example 8 //////////////////////
  329. /**
  330. * Example 8: A simple multistep form with a Next and a Back button.
  331. *
  332. * Handbook page: http://drupal.org/node/717750.
  333. *
  334. * For more extensive multistep forms, see
  335. * @link form_example_wizard.inc form_example_wizard.inc @endlink
  336. *
  337. *
  338. * Adds logic to our form builder to give it two pages.
  339. * The @link ajax_example_wizard AJAX Example's Wizard Example @endlink
  340. * gives an AJAX version of this same idea.
  341. *
  342. * @see form_example_tutorial_8_submit()
  343. * @see form_example_tutorial_8_validate()
  344. * @see form_example_tutorial_8_page_two()
  345. * @see form_example_tutorial_8_page_two_submit()
  346. * @see form_example_tutorial_8_next_submit()
  347. * @see form_example_tutorial.inc
  348. * @ingroup form_example
  349. */
  350. function form_example_tutorial_8($form, &$form_state) {
  351. // Display page 2 if $form_state['page_num'] == 2
  352. if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) {
  353. return form_example_tutorial_8_page_two($form, $form_state);
  354. }
  355. // Otherwise we build page 1.
  356. $form_state['page_num'] = 1;
  357. $form['description'] = array(
  358. '#type' => 'item',
  359. '#title' => t('A basic multistep form (page 1)'),
  360. );
  361. $form['first'] = array(
  362. '#type' => 'textfield',
  363. '#title' => t('First name'),
  364. '#description' => "Please enter your first name.",
  365. '#size' => 20,
  366. '#maxlength' => 20,
  367. '#required' => TRUE,
  368. '#default_value' => !empty($form_state['values']['first']) ? $form_state['values']['first'] : '',
  369. );
  370. $form['last'] = array(
  371. '#type' => 'textfield',
  372. '#title' => t('Last name'),
  373. '#default_value' => !empty($form_state['values']['last']) ? $form_state['values']['last'] : '',
  374. );
  375. $form['year_of_birth'] = array(
  376. '#type' => 'textfield',
  377. '#title' => "Year of birth",
  378. '#description' => 'Format is "YYYY"',
  379. '#default_value' => !empty($form_state['values']['year_of_birth']) ? $form_state['values']['year_of_birth'] : '',
  380. );
  381. $form['next'] = array(
  382. '#type' => 'submit',
  383. '#value' => 'Next >>',
  384. '#submit' => array('form_example_tutorial_8_next_submit'),
  385. '#validate' => array('form_example_tutorial_8_next_validate'),
  386. );
  387. return $form;
  388. }
  389. /**
  390. * Returns the form for the second page of form_example_tutorial_8().
  391. */
  392. function form_example_tutorial_8_page_two($form, &$form_state) {
  393. $form['description'] = array(
  394. '#type' => 'item',
  395. '#title' => t('A basic multistep form (page 2)'),
  396. );
  397. $form['color'] = array(
  398. '#type' => 'textfield',
  399. '#title' => t('Favorite color'),
  400. '#required' => TRUE,
  401. '#default_value' => !empty($form_state['values']['color']) ? $form_state['values']['color'] : '',
  402. );
  403. $form['submit'] = array(
  404. '#type' => 'submit',
  405. '#value' => t('Submit'),
  406. '#submit' => array('form_example_tutorial_8_page_two_submit'),
  407. );
  408. $form['back'] = array(
  409. '#type' => 'submit',
  410. '#value' => t('<< Back'),
  411. '#submit' => array('form_example_tutorial_8_page_two_back'),
  412. // We won't bother validating the required 'color' field, since they
  413. // have to come back to this page to submit anyway.
  414. '#limit_validation_errors' => array(),
  415. );
  416. return $form;
  417. }
  418. /**
  419. * Validate handler for the next button on first page.
  420. */
  421. function form_example_tutorial_8_next_validate($form, &$form_state) {
  422. $year_of_birth = $form_state['values']['year_of_birth'];
  423. if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
  424. form_set_error('year_of_birth', t('Enter a year between 1900 and 2000.'));
  425. }
  426. }
  427. /**
  428. * Submit handler for form_example_tutorial_8() next button.
  429. *
  430. * Capture the values from page one and store them away so they can be used
  431. * at final submit time.
  432. */
  433. function form_example_tutorial_8_next_submit($form, &$form_state) {
  434. // Values are saved for each page.
  435. // to carry forward to subsequent pages in the form.
  436. // and we tell FAPI to rebuild the form.
  437. $form_state['page_values'][1] = $form_state['values'];
  438. if (!empty($form_state['page_values'][2])) {
  439. $form_state['values'] = $form_state['page_values'][2];
  440. }
  441. // When form rebuilds, it will look at this to figure which page to build.
  442. $form_state['page_num'] = 2;
  443. $form_state['rebuild'] = TRUE;
  444. }
  445. /**
  446. * Back button handler submit handler.
  447. *
  448. * Since #limit_validation_errors = array() is set, values from page 2
  449. * will be discarded. We load the page 1 values instead.
  450. */
  451. function form_example_tutorial_8_page_two_back($form, &$form_state) {
  452. $form_state['values'] = $form_state['page_values'][1];
  453. $form_state['page_num'] = 1;
  454. $form_state['rebuild'] = TRUE;
  455. }
  456. /**
  457. * The page 2 submit handler.
  458. *
  459. * This is the final submit handler. Gather all the data together and output
  460. * it in a drupal_set_message().
  461. */
  462. function form_example_tutorial_8_page_two_submit($form, &$form_state) {
  463. // Normally, some code would go here to alter the database with the data
  464. // collected from the form. Instead sets a message with drupal_set_message()
  465. // to validate that the code worked.
  466. $page_one_values = $form_state['page_values'][1];
  467. drupal_set_message(t('The form has been submitted. name="@first @last", year of birth=@year_of_birth',
  468. array('@first' => $page_one_values['first'], '@last' => $page_one_values['last'], '@year_of_birth' => $page_one_values['year_of_birth'])));
  469. if (!empty($page_one_values['first2'])) {
  470. drupal_set_message(t('Second name: name="@first @last", year of birth=@year_of_birth',
  471. array('@first' => $page_one_values['first2'], '@last' => $page_one_values['last2'], '@year_of_birth' => $page_one_values['year_of_birth2'])));
  472. }
  473. drupal_set_message(t('And the favorite color is @color', array('@color' => $form_state['values']['color'])));
  474. // If we wanted to redirect on submission, set $form_state['redirect']
  475. // $form_state['redirect'] = 'node'; // Redirects the user to /node.
  476. }
  477. //////////////// Tutorial Example 9 //////////////////////
  478. /**
  479. * Example 9: A form with a dynamically added new fields.
  480. *
  481. * This example adds default values so that when the form is rebuilt,
  482. * the form will by default have the previously-entered values.
  483. *
  484. * From handbook page http://drupal.org/node/717746.
  485. *
  486. * @see form_example_tutorial_9_add_name()
  487. * @see form_example_tutorial_9_remove_name()
  488. * @see form_example_tutorial_9_submit()
  489. * @see form_example_tutorial_9_validate()
  490. * @ingroup form_example
  491. */
  492. function form_example_tutorial_9($form, &$form_state) {
  493. // We will have many fields with the same name, so we need to be able to
  494. // access the form hierarchically.
  495. $form['#tree'] = TRUE;
  496. $form['description'] = array(
  497. '#type' => 'item',
  498. '#title' => t('A form with dynamically added new fields'),
  499. );
  500. if (empty($form_state['num_names'])) {
  501. $form_state['num_names'] = 1;
  502. }
  503. // Build the number of name fieldsets indicated by $form_state['num_names']
  504. for ($i = 1; $i <= $form_state['num_names']; $i++) {
  505. $form['name'][$i] = array(
  506. '#type' => 'fieldset',
  507. '#title' => t('Name #@num', array('@num' => $i)),
  508. '#collapsible' => TRUE,
  509. '#collapsed' => FALSE,
  510. );
  511. $form['name'][$i]['first'] = array(
  512. '#type' => 'textfield',
  513. '#title' => t('First name'),
  514. '#description' => t("Enter first name."),
  515. '#size' => 20,
  516. '#maxlength' => 20,
  517. '#required' => TRUE,
  518. );
  519. $form['name'][$i]['last'] = array(
  520. '#type' => 'textfield',
  521. '#title' => t('Enter Last name'),
  522. '#required' => TRUE,
  523. );
  524. $form['name'][$i]['year_of_birth'] = array(
  525. '#type' => 'textfield',
  526. '#title' => t("Year of birth"),
  527. '#description' => t('Format is "YYYY"'),
  528. );
  529. }
  530. $form['submit'] = array(
  531. '#type' => 'submit',
  532. '#value' => 'Submit',
  533. );
  534. // Adds "Add another name" button
  535. $form['add_name'] = array(
  536. '#type' => 'submit',
  537. '#value' => t('Add another name'),
  538. '#submit' => array('form_example_tutorial_9_add_name'),
  539. );
  540. // If we have more than one name, this button allows removal of the
  541. // last name.
  542. if ($form_state['num_names'] > 1) {
  543. $form['remove_name'] = array(
  544. '#type' => 'submit',
  545. '#value' => t('Remove latest name'),
  546. '#submit' => array('form_example_tutorial_9_remove_name'),
  547. // Since we are removing a name, don't validate until later.
  548. '#limit_validation_errors' => array(),
  549. );
  550. }
  551. return $form;
  552. }
  553. /**
  554. * Submit handler for "Add another name" button on form_example_tutorial_9().
  555. *
  556. * $form_state['num_names'] tells the form builder function how many name
  557. * fieldsets to build, so here we increment it.
  558. *
  559. * All elements of $form_state are persisted, so there's no need to use a
  560. * particular key, like the old $form_state['storage']. We can just use
  561. * $form_state['num_names'].
  562. */
  563. function form_example_tutorial_9_add_name($form, &$form_state) {
  564. // Everything in $form_state is persistent, so we'll just use
  565. // $form_state['add_name']
  566. $form_state['num_names']++;
  567. // Setting $form_state['rebuild'] = TRUE causes the form to be rebuilt again.
  568. $form_state['rebuild'] = TRUE;
  569. }
  570. function form_example_tutorial_9_remove_name($form, &$form_state) {
  571. if ($form_state['num_names'] > 1) {
  572. $form_state['num_names']--;
  573. }
  574. // Setting $form_state['rebuild'] = TRUE causes the form to be rebuilt again.
  575. $form_state['rebuild'] = TRUE;
  576. }
  577. /**
  578. * Validate function for form_example_tutorial_9().
  579. *
  580. * Adds logic to validate the form to check the validity of the new fields,
  581. * if they exist.
  582. */
  583. function form_example_tutorial_9_validate($form, &$form_state) {
  584. for ($i = 1; $i <= $form_state['num_names']; $i++) {
  585. $year_of_birth = $form_state['values']['name'][$i]['year_of_birth'];
  586. if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
  587. form_set_error("name][$i][year_of_birth", t('Enter a year between 1900 and 2000.'));
  588. }
  589. }
  590. }
  591. /**
  592. * Submit function for form_example_tutorial_9().
  593. */
  594. function form_example_tutorial_9_submit($form, &$form_state) {
  595. $output = t("Form 9 has been submitted.");
  596. for ($i = 1; $i <= $form_state['num_names']; $i++) {
  597. $output .= t("@num: @first @last (@date)... ", array('@num' => $i, '@first' => $form_state['values']['name'][$i]['first'],
  598. '@last' => $form_state['values']['name'][$i]['last'], '@date' => $form_state['values']['name'][$i]['year_of_birth']));
  599. }
  600. drupal_set_message($output);
  601. }
  602. //////////////// Tutorial Example 10 //////////////////////
  603. /**
  604. * Example 10: A form with a file upload field.
  605. *
  606. * This example allows the user to upload a file to Drupal which is stored
  607. * physically and with a reference in the database.
  608. *
  609. * @see form_example_tutorial_10_submit()
  610. * @see form_example_tutorial_10_validate()
  611. * @ingroup form_example
  612. */
  613. function form_example_tutorial_10($form_state) {
  614. // If you are familiar with how browsers handle files, you know that
  615. // enctype="multipart/form-data" is required. Drupal takes care of that, so
  616. // you don't need to include it yourself.
  617. $form['file'] = array(
  618. '#type' => 'file',
  619. '#title' => t('Image'),
  620. '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
  621. );
  622. $form['submit'] = array(
  623. '#type' => 'submit',
  624. '#value' => t('Submit'),
  625. );
  626. return $form;
  627. }
  628. /**
  629. * Validate handler for form_example_tutorial_10().
  630. */
  631. function form_example_tutorial_10_validate($form, &$form_state) {
  632. $file = file_save_upload('file', array(
  633. 'file_validate_is_image' => array(), // Validates file is really an image.
  634. 'file_validate_extensions' => array('png gif jpg jpeg'), // Validate extensions.
  635. ));
  636. // If the file passed validation:
  637. if ($file) {
  638. // Move the file, into the Drupal file system
  639. if ($file = file_move($file, 'public://')) {
  640. // Save the file for use in the submit handler.
  641. $form_state['storage']['file'] = $file;
  642. }
  643. else {
  644. form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
  645. }
  646. }
  647. else {
  648. form_set_error('file', t('No file was uploaded.'));
  649. }
  650. }
  651. /**
  652. * Submit handler for form_example_tutorial_10().
  653. */
  654. function form_example_tutorial_10_submit($form, &$form_state) {
  655. $file = $form_state['storage']['file'];
  656. // We are done with the file, remove it from storage.
  657. unset($form_state['storage']['file']);
  658. // Make the storage of the file permanent
  659. $file->status = FILE_STATUS_PERMANENT;
  660. // Save file status.
  661. file_save($file);
  662. // Set a response to the user.
  663. drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
  664. }
Login or register to post comments