drupal-forms - Drupal 8、如何构建分层表单?

标签 drupal-forms drupal-8

在 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");
}
...

enter image description here

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

enter image description here

form_state->getValues() 仅包含 ['content']['subfirst']['content']['subsecond' ] 值... 这意味着我必须将 uniques 标签与表单 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() 返回:

enter image description here

我得到了四个值。但是,它们处于同一层级。我如何使用表单 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
...

最佳答案

The solution

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

关于drupal-forms - Drupal 8、如何构建分层表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847616/

相关文章:

drupal-forms - Drupal 8,从带有预览的 BuildForm 添加一个图像字段

php - 在 drupal 7 中的 block 内创建自定义表单

php - Drupal 7 Forms API 条件逻辑在 IE 中不起作用

php - Drupal 7 FAPI-在验证或提交处理程序中添加表单元素

drupal-7 - Drupal 7 Forms API - AJAX Forms 错误 : An illegal choice has been detected. 请联系站点管理员

string - Drupal8 Twig - 将字符串转换为整数

drupal - 查看: Exclude nodes that have the same taxonomy term id(s) as current node

symfony - Twig:如何检查字段的内部 html 内容

php - Drupal 8 自定义注册表单