- <?php
- * @file
- * An example of how to use the new #states Form API element, allowing
- * dynamic form behavior with very simple setup.
- */
-
- * States demo form.
- *
- * This form shows off the #states system by dynamically showing parts of the
- * form based on the state of other parts.
- *
- * @ingroup form_example
- *
- * The basic idea is that you add a #states property to the element which is
- * to be changed based on some action elsewhere on the form. The #states
- * property lists a change which is to be made, and under what conditions
- * that change should be made.
- *
- * For example, in the 'tests_taken' form element below we have:
- * @code
- * '#states' => array(
- * 'visible' => array(
- * ':input[name="student_type"]' => array('value' => 'high_school'),
- * ),
- * ),
- * @endcode
- * Meaning that the element is to be made visible when the condition is met.
- * The condition is a combination of a jQuery selector (which selects the
- * element we want to test) and a condition for that element. In this case,
- * the condition is whether the return value of the 'student_type' element is
- * 'high_school'. If it is, this element will be visible.
- *
- * So the syntax is:
- * @code
- * '#states' => array(
- * 'action_to_take_on_this_form_element' => array(
- * 'jquery_selector_for_another_element' => array(
- * 'condition_type' => value,
- * ),
- * ),
- * ),
- * @endcode
- *
- * If you need an action to take place only when two different conditions are
- * true, then you add both of those conditions to the action. See the
- * 'country_writein' element below for an example.
- *
- * Note that the easiest way to select a textfield, checkbox, or select is with
- * the
- * @link http://api.jquery.com/input-selector/ ':input' jquery shortcut @endlink,
- * which selects any of those.
- *
- * There are examples below of changing or hiding an element when a checkbox
- * is checked, when a textarea is filled, when a select has a given value.
- *
- * See backdrop_process_states() for full documentation.
- *
- * @see forms_api_reference.html
- */
- function form_example_states_form($form, &$form_state) {
- $form['student_type'] = array(
- '#type' => 'radios',
- '#options' => array(
- 'high_school' => t('High School'),
- 'undergraduate' => t('Undergraduate'),
- 'graduate' => t('Graduate'),
- ),
- '#title' => t('What type of student are you?'),
- );
- $form['high_school'] = array(
- '#type' => 'fieldset',
- '#title' => t('High School Information'),
-
-
- '#states' => array(
- 'visible' => array(
- ':input[name="student_type"]' => array('value' => 'high_school'),
- ),
- ),
- );
-
-
- $form['high_school']['tests_taken'] = array(
- '#type' => 'checkboxes',
- '#options' => backdrop_map_assoc(array(t('SAT'), t('ACT'))),
- '#title' => t('What standardized tests did you take?'),
-
-
-
-
-
- '#states' => array(
-
- 'visible' => array(
- ':input[name="student_type"]' => array('value' => 'high_school'),
- ),
- ),
- );
-
- $form['high_school']['sat_score'] = array(
- '#type' => 'textfield',
- '#title' => t('Your SAT score:'),
- '#size' => 4,
-
-
-
- '#states' => array(
-
- 'visible' => array(
- ':input[name="tests_taken[SAT]"]' => array('checked' => TRUE),
- ),
- ),
- );
- $form['high_school']['act_score'] = array(
- '#type' => 'textfield',
- '#title' => t('Your ACT score:'),
- '#size' => 4,
-
-
- '#states' => array(
-
- 'visible' => array(
- ':input[name="tests_taken[ACT]"]' => array('checked' => TRUE),
- ),
- ),
- );
-
-
- $form['undergraduate'] = array(
- '#type' => 'fieldset',
- '#title' => t('Undergraduate Information'),
-
-
- '#states' => array(
- 'visible' => array(
- ':input[name="student_type"]' => array('value' => 'undergraduate'),
- ),
- ),
- );
-
- $form['undergraduate']['how_many_years'] = array(
- '#type' => 'select',
- '#title' => t('How many years have you completed?'),
-
-
-
- '#options' => array(
- 1 => t('One'),
- 2 => t('Two'),
- 3 => t('Three'),
- 4 => t('Four'),
- 5 => t('Lots'),
- ),
- );
-
- $form['undergraduate']['comment'] = array(
- '#type' => 'item',
- '#description' => t("Wow, that's a long time."),
- '#states' => array(
- 'visible' => array(
-
-
- ':input[name="how_many_years"]' => array('value' => '5'),
- ),
- ),
- );
- $form['undergraduate']['school_name'] = array(
- '#type' => 'textfield',
- '#title' => t('Your college or university:'),
- );
- $form['undergraduate']['school_country'] = array(
- '#type' => 'select',
- '#options' => backdrop_map_assoc(array(t('UK'), t('Other'))),
- '#title' => t('In what country is your college or university located?'),
- );
- $form['undergraduate']['country_writein'] = array(
- '#type' => 'textfield',
- '#size' => 20,
- '#title' => t('Please enter the name of the country where your college or university is located.'),
-
-
- '#states' => array(
-
- 'visible' => array(
- ':input[name="school_country"]' => array('value' => t('Other')),
- ),
- ),
- );
-
- $form['undergraduate']['thanks'] = array(
- '#type' => 'item',
- '#description' => t('Thanks for providing both your school and your country.'),
- '#states' => array(
-
- 'visible' => array(
- ':input[name="school_country"]' => array('value' => t('Other')),
- ':input[name="country_writein"]' => array('filled' => TRUE),
- ),
- ),
- );
- $form['undergraduate']['go_away'] = array(
- '#type' => 'submit',
- '#value' => t('Done with form'),
- '#states' => array(
-
- 'visible' => array(
- ':input[name="school_country"]' => array('value' => t('Other')),
- ':input[name="country_writein"]' => array('filled' => TRUE),
- ),
- ),
- );
-
-
- $form['graduate'] = array(
- '#type' => 'fieldset',
- '#title' => t('Graduate School Information'),
-
-
- '#states' => array(
- 'visible' => array(
- ':input[name="student_type"]' => array('value' => 'graduate'),
- ),
- ),
- );
- $form['graduate']['more_info'] = array(
- '#type' => 'textarea',
- '#title' => t('Please describe your graduate studies'),
- );
-
- $form['graduate']['info_provide'] = array(
- '#type' => 'checkbox',
- '#title' => t('Check here if you have provided information above'),
- '#disabled' => TRUE,
- '#states' => array(
-
-
- 'checked' => array(
- ':input[name="more_info"]' => array('filled' => TRUE),
- ),
- ),
- );
-
- $form['average'] = array(
- '#type' => 'textfield',
- '#title' => t('Enter your average'),
-
-
- '#states' => array(
- 'visible' => array(
- ':input[name="student_type"]' => array(
- array('value' => 'high_school'),
- array('value' => 'undergraduate'),
- ),
- ),
- ),
- );
-
- $form['expand_more_info'] = array(
- '#type' => 'checkbox',
- '#title' => t('Check here if you want to add more information.'),
- );
- $form['more_info'] = array(
- '#type' => 'fieldset',
- '#title' => t('Additional Information'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
-
-
- '#states' => array(
- 'expanded' => array(
- ':input[name="expand_more_info"]' => array('checked' => TRUE),
- ),
- ),
- );
- $form['more_info']['feedback'] = array(
- '#type' => 'textarea',
- '#title' => t('What do you have to say?'),
- );
-
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit your information'),
- );
-
- return $form;
- }
-
- * Submit handler for form_example_states_form().
- */
- function form_example_states_form_submit($form, &$form_state) {
- backdrop_set_message(t('Submitting values: @values', array('@values' => var_export($form_state['values'], TRUE))));
- }