drupal 7控制表#states可见,也隐藏标题

时间:2013-10-11 06:53:50

标签: drupal states form-api

我有两个表单字段,其中一个依赖于其他表单,即如果优惠券值为“是”,则显示coupon_code文本框

$form['coupon'] = array(
    '#id' => 'coupon',
    '#key_type' => 'associative',
    '#multiple_toggle' => '1',
    '#type' => 'select',
    '#options' => array(
            '' => 'Select',
            'Yes' => 'Yes',
            'No' => 'No'
            ),
    '#default_value' => '',
    '#required' => TRUE
);

$form['coupon_code'] = array(
    '#id' => 'coupon_code',
    '#type' => 'textfield',
    '#default_value' => $couponCode,
    '#required' => TRUE,
    '#states' => array(
        'visible' => array(// action to take.
            ':input[name="coupon"]' => array('value' => 'Yes'),
        ),
    ),
);

和我的tpl文件

<tr>
 <td class='formlabel' valign=middle>Coupon</td>
            <td align=left>
                <?php echo render($form['coupon']); ?>
                <font class='redmark'>*</font>
            </td>
        </tr>
        <tr>
            <td class='formlabel' valign=middle>Coupon Code</td>
            <td align=left>
                <?php echo render($form['coupon_code']); ?>
                <font class='redmark'>*</font>
            </td>
        </tr>

当优惠券价值为“否”时,优惠券代码不可见,但我还想隐藏标签“优惠券代码”,我该怎么做?

1 个答案:

答案 0 :(得分:0)

将“#title”属性添加到表单数组中。如果字段可见,则将作为标签输出。您还必须更改tpl文件以删除您手动输入的标签。

$form['coupon'] = array(
    '#id' => 'coupon',
    '#key_type' => 'associative',
    '#multiple_toggle' => '1',
    '#title' => 'Coupon',
    '#type' => 'select',
    '#options' => array(
            '' => 'Select',
            'Yes' => 'Yes',
            'No' => 'No'
            ),
    '#default_value' => '',
    '#required' => TRUE
);

$form['coupon_code'] = array(
    '#id' => 'coupon_code',
    '#type' => 'textfield',
    '#default_value' => $couponCode,
    '#required' => TRUE,
    '#title' => 'Coupon Code',
    '#states' => array(
        'visible' => array(// action to take.
            ':input[name="coupon"]' => array('value' => 'Yes'),
        ),
    ),
);

这可能是最好的方法恕我直言,如果你不想这样做,你也可以在优惠券选择中添加一个JavaScript事件触发器,它将显示/隐藏标签。