【问题标题】:Drupal 8, how build a hierarchical form?Drupal 8,如何建立分层表格?
【发布时间】:2015-11-21 19:27:23
【问题描述】:

在 Drupal 8 中,我不明白如何构建“分层”表单。

我有这个样本表格

...
public function buildForm(array $form, FormStateInterface $form_state) {
    $form['description'] = array(
        '#type' => 'fieldset',
        '#title' => t('Main description'),
    );
    $form['description']['subfirst'] = array(
        '#type' => 'textfield',
        '#title' => t('subfirst'),
    );
    $form['description']['subsecond'] = array(
        '#type' => 'textfield',
        '#title' => t('subsecond'),
    );

    $form['content'] = array(
        '#type' => 'fieldset',
        '#title' => t('Main description'),
    );
    $form['content']['subfirst'] = array(
        '#type' => 'textfield',
        '#title' => t('subfirst'),
    );
    $form['content']['subsecond'] = array(
        '#type' => 'textfield',
        '#title' => t('subsecond'),
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );
    return $form;
}

public function submitForm(array &$form, FormStateInterface $form_state) {
    dpm($form_state->getValues(),"getValues");
}
...

当我提交表单时,我的 form_state->getValues() 返回:

form_state->getValues() 仅包含 ['content']['subfirst']['content']['subsecond'] 值... 这意味着我必须使用带有 api 形式的唯一标签?我觉得很奇怪……

然后,我改变我的形式:

$form['content']['subfirst'] become $form['content']['totosubfirst']

$form['content']['subsecond'] become $form['content']['todosubsecond']

新代码:

public function buildForm(array $form, FormStateInterface $form_state) {
    $form['description'] = array(
        '#type' => 'fieldset',
        '#title' => t('Main description'),
    );
    $form['description']['subfirst'] = array(
        '#type' => 'textfield',
        '#title' => t('subfirst'),
    );
    $form['description']['subsecond'] = array(
        '#type' => 'textfield',
        '#title' => t('subsecond'),
    );

    $form['content'] = array(
        '#type' => 'fieldset',
        '#title' => t('Main description'),
    );
    $form['content']['totosubfirst'] = array(
        '#type' => 'textfield',
        '#title' => t('subfirst'),
    );
    $form['content']['totosubsecond'] = array(
        '#type' => 'textfield',
        '#title' => t('subsecond'),
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );
    return $form;
}

当我提交表单时,我的 form_state->getValues() 返回:

我得到了四个值。但是,它们处于同一层级。我如何使用表单 api 来获得这样的 form_state:

'description' => 'subfirst' => string(3) "AAA"

'description' => 'subsecond' => string(3) "BBB"

'content' => 'totosubfirst' => string(3) "CCC"

'content' => 'totosubsecond' => string(3) "DDD"

?

我想要一个分层的 form_state,因为在我想要创建一个自定义函数之后:

foreach element in description
  do that
foreach element in content
  do that
...

【问题讨论】:

    标签: drupal-forms drupal-8


    【解决方案1】:

    The solution

    在父元素上设置#tree => TRUE,则值将相应嵌套

    【讨论】:

    • 呃……谢谢!我要求将此添加到 getValue() 的文档中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多