Symfony 4:CollectionType的“ allow_delete”选项的逻辑在哪里?

时间:2018-07-23 19:47:42

标签: forms symfony4

因此,我正在尝试了解Symfony表单。我正在核心代码中搜索“ allow_delete”选项,以了解其工作原理,但是唯一可以找到它的地方是在CollectionType类中,并且在那里找不到任何逻辑。

文档states

  

如果设置为true,则如果现有项目不包含在   提交的数据,则最终数组中将正确缺少该数据   项目。

它在代码中到底在哪里影响提交的数据?

1 个答案:

答案 0 :(得分:0)

您可以在MergeCollectionListener.php中找到从第91行开始的函数:

// Remove deleted items before adding to free keys that are to be
// replaced
if ($this->allowDelete) {
        foreach ($itemsToDelete as $key) {
            unset($dataToMergeInto[$key]);
        }
    }

$dataToMergeInto设置为$dataToMergeInto = $event->getForm()->getNormData(); ,这是指FormInterface.php中说明的功能:

/**
 * Returns the normalized data of the field.
 *
 * @return mixed When the field is not submitted, the default data is returned.
 *               When the field is submitted, the normalized submitted data is
 *               returned if the field is valid, null otherwise.
 */
public function getNormData();