0

Currently I am using states attribute in my form to show/hide a drodown having days as its value on selecting a parent dropdown value. My working scenerio is that I want to show a dropdown having 31 days if january,march,may.... is selected and to show another dropdown having 30 days as its value when febraury,april,june .... is selected , can some one suggest me how its could be achieved because in usage of #states attribute only one value could be selected.

function sandbox_ajax_dropdown($form, &$form_state) {

$options = array(1 => t('1 月'), 2 => t('2 月'), 3 => t('3 月'), 4 => t('4 月'), 5 => t('5 月'), 6 => t('6 月'), 7 => t('7 月'), 8 => t('8 月'), 9 => t('9 月'), 10 => t('10 月'), 11 => t('11 月'), 12 => t('12 月'),);

$form['state_list'] = array(
    '#type' => 'select',
    '#title' => 'Choose A Day First',
    '#options' => $options
);
$form['illinois_cities'] = array(
    '#type' => 'select',
    '#description' => t("Thirty-One Days."),
    '#states' => array(
        'visible' => array(
            ':input[name="state_list"]' => array('value' => '5'),
        ),
    ),
    '#options' => array(1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
        13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
        25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30'), 31 => t('31')),
);

$form['ohio_cities'] = array(
    '#type' => 'select',
    '#description' => t("Thirty Days."),
    '#states' => array(
        'visible' => array(
            ':input[name="state_list"]' => array('value' => '1', '2'),
        ),
    ),
    '#options' => array(1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
        13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
        25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30'))
);

return $form;

}

0

Found it !!!!!!!! The solution is below highlighted.

function modulename_form_alter(&$form, &$form_state, $form_id) {

if ($form_id == 'webform_client_form_14209') {

    $form['submitted']['dob'] = array(
        '#type' => 'fieldset',
        '#title' => t('出生日期'),
        '#weight' => 4,
    );

    $form['submitted']['dob']['dob_month'] = array(
        '#type' => 'select',
        '#title' => t('月'),
        '#options' => array(
            1 => t('1 月'), 2 => t('2 月'), 3 => t('3 月'), 4 => t('4 月'), 5 => t('5 月'), 6 => t('6 月'), 7 => t('7 月'), 8 => t('8 月'), 9 => t('9 月'), 10 => t('10 月'), 11 => t('11 月'), 12 => t('12 月'),
        ),
        '#description' => t('Select month.'),
        '#required' => true,
    );

    /* for 31 days */
    $form['submitted']['dob']['dob_day_31'] = array(
        '#type' => 'select',
        '#title' => t('日'),
        '#options' => array(1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
            13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
            25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30'), 31 => t('31')),
        '#description' => t("Thirty-One Days."),
        '#required' => true,
        ***'#states' => array(
            'visible' => array(
                ':input[name="submitted[dob][dob_month]"]' => array(
                    array('value' => '1'),
                    array('value' => '3'),
                    array('value' => '5'),
                    array('value' => '7'),
                    array('value' => '8'),
                    array('value' => '10'),
                    array('value' => '12'),
                ),
            ),***
        ),
    );

    /* for 30 days */
    $form['submitted']['dob']['dob_day_30'] = array(
        '#type' => 'select',
        '#title' => t('日'),
        '#options' => array(1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
            13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
            25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30')),
        '#description' => t("Thirty Days."),
        '#required' => true,
        '#states' => array(
            'visible' => array(
                ':input[name="submitted[dob][dob_month]"]' => array(
                    array('value' => '2'),
                    array('value' => '4'),
                    array('value' => '6'),
                    array('value' => '9'),
                    array('value' => '11'),
                ),
            ),
        ),
    );

    $form['submitted']['dob']['dob_year'] = array(
        '#type' => 'select',
        '#title' => t('年'),
        '#options' => array(1 => t('2013'), 2 => t('2012'), 3 => t('2011'), 4 => t('2010'), 5 => t('2009'), 6 => t('2008'), 7 => t('2007'), 8 => t('2006'),
            9 => t('2005'), 10 => t('2004'), 11 => t('2003'), 12 => t('2002'), 13 => t('2001'), 14 => t('2000'), 15 => t('1999'), 16 => t('1998'), 17 => t('1997'),
            18 => t('1996'), 19 => t('1995'), 20 => t('1994'), 21 => t('1993'), 22 => t('1992'), 23 => t('1991'), 24 => t('1990'), 25 => t('1989'), 26 => t('1988'),
            27 => t('1987'), 28 => t('1986'), 29 => t('1985'), 30 => t('1984'), 31 => t('1983'),
        ),
        '#description' => t('Select year.'),
        '#required' => true,
    );
}
return $form;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.