form_example_states.inc

  1. examples
    1. 7 form_example/form_example_states.inc
    2. 8 form_example/form_example_states.inc

An example of how to use the new #states Form API element, allowing dynamic form behavior with very simple setup.

Functions & methods

NameDescription
form_example_states_formThis form shows off the #states system by dynamically showing parts of the form based on the state of other parts.
form_example_states_form_submitSubmit handler for form_example_states_form().

File

form_example/form_example_states.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * An example of how to use the new #states Form API element, allowing
  5. * dynamic form behavior with very simple setup.
  6. */
  7. /**
  8. * This form shows off the #states system by dynamically showing parts of the
  9. * form based on the state of other parts.
  10. *
  11. * @ingroup form_example
  12. *
  13. * The basic idea is that you add a #states property to the element which is
  14. * to be changed based on some action elsewhere on the form. The #states
  15. * property lists a change which is to be made, and under what conditions
  16. * that change should be made.
  17. *
  18. * For example, in the 'tests_taken' form element below we have:
  19. * @code
  20. * '#states' => array(
  21. * 'visible' => array(
  22. * ':input[name="student_type"]' => array('value' => 'high_school'),
  23. * ),
  24. * ),
  25. * @endcode
  26. * Meaning that the element is to be made visible when the condition is met.
  27. * The condition is a combination of a jQuery selector (which selects the
  28. * element we want to test) and a condition for that element. In this case,
  29. * the condition is whether the return value of the 'student_type' element is
  30. * 'high_school'. If it is, this element will be visible.
  31. *
  32. * So the syntax is:
  33. * @code
  34. * '#states' => array(
  35. * 'action_to_take_on_this_form_element' => array(
  36. * 'jquery_selector_for_another_element' => array('condition_type' => value),
  37. * ),
  38. * ),
  39. * @endcode
  40. *
  41. * If you need an action to take place only when two different conditions are
  42. * true, then you add both of those conditions to the action. See the
  43. * 'country_writein' element below for an example.
  44. *
  45. * Note that the easiest way to select a textfield, checkbox, or select is with
  46. * the @link http://api.jquery.com/input-selector/ ':input' jquery shortcut @endlink,
  47. * which selects any any of those.
  48. *
  49. * There are examples below of changing or hiding an element when a checkbox
  50. * is checked, when a textarea is filled, when a select has a given value.
  51. *
  52. * See drupal_process_states() for full documentation.
  53. *
  54. * See also @link http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#states Form API Reference @endlink
  55. */
  56. function form_example_states_form($form, &$form_state) {
  57. $form['student_type'] = array(
  58. '#type' => 'radios',
  59. '#options' => array(
  60. 'high_school' => t('High School'),
  61. 'undergraduate' => t('Undergraduate'),
  62. 'graduate' => t('Graduate'),
  63. ),
  64. '#title' => t('What type of student are you?')
  65. );
  66. $form['high_school'] = array(
  67. '#type' => 'fieldset',
  68. '#title' => t('High School Information'),
  69. // This #states rule says that the "high school" fieldset should only
  70. // be shown if the "student_type" form element is set to "High School".
  71. '#states' => array(
  72. 'visible' => array(
  73. ':input[name="student_type"]' => array('value' => 'high_school'),
  74. ),
  75. ),
  76. );
  77. // High school information.
  78. $form['high_school']['tests_taken'] = array(
  79. '#type' => 'checkboxes',
  80. '#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),
  81. '#title' => t('What standardized tests did you take?'),
  82. // This #states rule says that this checkboxes array will be visible only
  83. // when $form['student_type'] is set to t('High School').
  84. // It uses the jQuery selector :input[name=student_type] to choose the
  85. // element which triggers the behavior, and then defines the "High School"
  86. // value as the one that triggers visibility.
  87. '#states' => array(
  88. 'visible' => array( // action to take.
  89. ':input[name="student_type"]' => array('value' => 'high_school'),
  90. ),
  91. ),
  92. );
  93. $form['high_school']['sat_score'] = array(
  94. '#type' => 'textfield',
  95. '#title' => t('Your SAT score:'),
  96. '#size' => 4,
  97. // This #states rule limits visibility to when the $form['tests_taken']
  98. // 'SAT' checkbox is checked."
  99. '#states' => array(
  100. 'visible' => array( // action to take.
  101. ':input[name="tests_taken[SAT]"]' => array('checked' => TRUE),
  102. ),
  103. ),
  104. );
  105. $form['high_school']['act_score'] = array(
  106. '#type' => 'textfield',
  107. '#title' => t('Your ACT score:'),
  108. '#size' => 4,
  109. // Set this element visible if the ACT checkbox above is checked.
  110. '#states' => array(
  111. 'visible' => array( // action to take.
  112. ':input[name="tests_taken[ACT]"]' => array('checked' => TRUE),
  113. ),
  114. ),
  115. );
  116. // Undergrad information.
  117. $form['undergraduate'] = array(
  118. '#type' => 'fieldset',
  119. '#title' => t('Undergraduate Information'),
  120. // This #states rule says that the "undergraduate" fieldset should only
  121. // be shown if the "student_type" form element is set to "Undergraduate".
  122. '#states' => array(
  123. 'visible' => array(
  124. ':input[name="student_type"]' => array('value' => 'undergraduate'),
  125. ),
  126. ),
  127. );
  128. $form['undergraduate']['how_many_years'] = array(
  129. '#type' => 'select',
  130. '#title' => t('How many years have you completed?'),
  131. // The options here are integers, but since all the action here happens
  132. // using the DOM on the client, we will have to use strings to work with
  133. // them.
  134. '#options' => array(
  135. 1 => t('One'),
  136. 2 => t('Two'),
  137. 3 => t('Three'),
  138. 4 => t('Four'),
  139. 5 => t('Lots'),
  140. ),
  141. );
  142. $form['undergraduate']['comment'] = array(
  143. '#type' => 'item',
  144. '#description' => t("Wow, that's a long time."),
  145. '#states' => array(
  146. 'visible' => array(
  147. // Note that '5' must be used here instead of the integer 5.
  148. // The information is coming from the DOM as a string.
  149. ':input[name="how_many_years"]' => array('value' => '5'),
  150. ),
  151. ),
  152. );
  153. $form['undergraduate']['school_name'] = array(
  154. '#type' => 'textfield',
  155. '#title' => t('Your college or university:'),
  156. );
  157. $form['undergraduate']['school_country'] = array(
  158. '#type' => 'select',
  159. '#options' => drupal_map_assoc(array(t('UK'), t('Other'))),
  160. '#title' => t('In what country is your college or university located?'),
  161. );
  162. $form['undergraduate']['country_writein'] = array(
  163. '#type' => 'textfield',
  164. '#size' => 20,
  165. '#title' => t('Please enter the name of the country where your college or university is located.'),
  166. // Only show this field if school_country is set to 'Other'.
  167. '#states' => array(
  168. 'visible' => array( // Action to take: Make visible.
  169. ':input[name="school_country"]' => array('value' => t('Other')),
  170. ),
  171. ),
  172. );
  173. $form['undergraduate']['thanks'] = array(
  174. '#type' => 'item',
  175. '#description' => t('Thanks for providing both your school and your country.'),
  176. '#states' => array(
  177. // Here visibility requires that two separate conditions be true.
  178. 'visible' => array(
  179. 'input[name="school_country"]' => array('value' => t('Other')),
  180. 'input[name="country_writein"]' => array('filled' => TRUE),
  181. ),
  182. ),
  183. );
  184. $form['undergraduate']['go_away'] = array(
  185. '#type' => 'submit',
  186. '#value' => t('Done with form'),
  187. '#states' => array(
  188. // Here visibility requires that two separate conditions be true.
  189. 'visible' => array(
  190. 'input[name="school_country"]' => array('value' => t('Other')),
  191. 'input[name="country_writein"]' => array('filled' => TRUE),
  192. ),
  193. ),
  194. );
  195. // Graduate student information.
  196. $form['graduate'] = array(
  197. '#type' => 'fieldset',
  198. '#title' => t('Graduate School Information'),
  199. // This #states rule says that the "graduate" fieldset should only
  200. // be shown if the "student_type" form element is set to "Graduate".
  201. '#states' => array(
  202. 'visible' => array(
  203. ':input[name="student_type"]' => array('value' => 'graduate'),
  204. ),
  205. ),
  206. );
  207. $form['graduate']['more_info'] = array(
  208. '#type' => 'textarea',
  209. '#title' => t('Please describe your graduate studies'),
  210. );
  211. $form['graduate']['info_provide'] = array(
  212. '#type' => 'checkbox',
  213. '#title' => t('Check here if you have provided information above'),
  214. '#disabled' => TRUE,
  215. '#states' => array(
  216. // Mark this checkbox checked if the "more_info" textarea has something
  217. // in it, if it's 'filled'.
  218. 'checked' => array( // Action to take: check the checkbox.
  219. ':input[name="more_info"]' => array('filled' => TRUE),
  220. ),
  221. ),
  222. );
  223. $form['expand_more_info'] = array(
  224. '#type' => 'checkbox',
  225. '#title' => t('Check here if you want to add more information.'),
  226. );
  227. $form['more_info'] = array(
  228. '#type' => 'fieldset',
  229. '#title' => t('Additional Information'),
  230. '#collapsible' => TRUE,
  231. '#collapsed' => TRUE,
  232. // Expand the expand_more_info fieldset if the box is checked.
  233. '#states' => array(
  234. 'expanded' => array(
  235. ':input[name="expand_more_info"]' => array('checked' => TRUE),
  236. ),
  237. ),
  238. );
  239. $form['more_info']['feedback'] = array(
  240. '#type' => 'textarea',
  241. '#title' => t('What do you have to say?'),
  242. );
  243. $form['submit'] = array(
  244. '#type' => 'submit',
  245. '#value' => t('Submit your information'),
  246. );
  247. return $form;
  248. }
  249. /**
  250. * Submit handler for form_example_states_form().
  251. */
  252. function form_example_states_form_submit($form, &$form_state) {
  253. drupal_set_message(t('Submitting values: @values', array('@values' => var_export($form_state['values'], TRUE))));
  254. }
Login or register to post comments