如何循环访问 Sensio 生成器bundle中的表单字段



为了更好地满足我的需求,我已经覆盖了 CRUD 的 Sensio 生成器捆绑包。

我想做的是能够遍历实体字段。默认情况下,它在show.html.twig中完成,但不在新视图和编辑视图中完成。

当我在new.html.twig.twig中实现相同的逻辑时,它不起作用,尽管它对edit.html.twig.twig不起作用。

{#app/Resources/SensioGeneratorBundle/skeleton/crud/views/new.html.twig.twig#}
{% for field, metadata in fields %}
    {% if field not in 'id' %}
        {{ '{{ form_row(edit_form.' ~ field ~ ')}}' }}
    {% endif %}
{% endfor %}

运行生成器时,错误是:变量"字段"在第 9 行的 "crud/views/new.html.twig.twig" 中不存在

好的,实际上这是 Sensio 生成器捆绑包中的一个问题。在文件:sensio\generator-bundle\Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator.php generateNewView 函数缺少参数。它不是传递字段,而是生成ShowView。

这是比较:

protected function generateNewView($dir)
{
    $this->renderFile('crud/views/new.html.twig.twig', $dir.'/new.html.twig', array(
        'bundle'            => $this->bundle->getName(),
        'entity'            => $this->entity,
        'route_prefix'      => $this->routePrefix,
        'route_name_prefix' => $this->routeNamePrefix,
        'actions'           => $this->actions,
    ));
}

protected function generateShowView($dir)
{
    $this->renderFile('crud/views/show.html.twig.twig', $dir.'/show.html.twig', array(
        'bundle'            => $this->bundle->getName(),
        'entity'            => $this->entity,
        'fields'            => $this->metadata->fieldMappings,
        'actions'           => $this->actions,
        'route_prefix'      => $this->routePrefix,
        'route_name_prefix' => $this->routeNamePrefix,
    ));
}

我会尝试将其作为改进发布。

最新更新