也许我的方法不是很好(从数据组织的角度来看)。我将用户实体中的用户权限列表存储为使用type = array的一个名为“permissions”的字段。数组的结构类似于:
Array (
'Page 1' => Array (
0 => 'No access',
1 => 'Read only',
2 => 'Edit',
3 => 'Create and Delete')
'Page n' => Array (
0 => 'No access',
1 => 'Read only',
2 => 'Edit',
3 => 'Create and Delete')
)
“页面”来自另一个名为“Page”的实体。我希望能够将此数组呈现为类似于Drupal 7处理其自己的用户权限表单的表:https://www.drupal.org/files/issues/Drupal7Permissions.png
基本上我希望能够打印出表格标题为“页面,无访问权限,只读,编辑,创建和删除”的表格以及“[PAGE NAME],[],[],[ ],[]“
如何实现这一点并让它仍然与Symfony表单引擎兼容(即,它读取我上面发布的数组样本,并将其返回给用户实体)
答案 0 :(得分:2)
直接渲染它是不可能的,但是你可以迭代数组并为数组中的每个元素添加一个字段,如下所示:
//Controller
public function DebugAction(Request $request){
$inputs = Array (
'Page1' => Array (
'No access',
'Read only',
'Edit',
'Create and Delete'
),
'Pagen' => Array (
'No access',
'Read only',
'Edit',
'Create and Delete')
);
$form = $this->createFormBuilder();
foreach($inputs as $title => $values){
$form->add($title, 'choice', array(
'choices' => $values,
'expanded' => true,
'multiple' => true,
));
}
$form = $form->getForm();
return $this->render('debug.html.twig', array(
'form' => $form->createView(),
));
}
{# debug.html.twig #}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
以上输出的形式如下:
答案 1 :(得分:0)
这样的问题是我慢慢离开Symfony表格和Twig模板的原因之一。看似简单的事情最终变得复杂。但是这里有一个使用包含的树枝模板来制作复选框的html表的示例:
{# form.dates is a collection of check boxes #}
{% if form.dates is defined %}
{# really should have passed items as an argument, oh well #}
{% set items = form.dates %}
<td>{% include '@CeradGame/Project/Schedule/Twig/ScheduleSearchCheckboxes.html.twig' %}</td>
{% endif %}
{# ScheduleSearchCheckboxes.html.twig #}
{# render one set of check boxes as a table #}
{# Setting a class on the first item #}
<table border="1">
<tr><th colspan="30">{{ items.vars.label }}</th></tr>
<tr>
{% set itemFirst = true %}
{% for item in items %}
<td align="center">{{ form_label(item) }}<br />
{% if itemFirst %}
{{ form_widget(item, { 'attr': {'class': 'cerad-checkbox-all' }}) }}</td>
{% else %}
{{ form_widget(item) }}</td>
{% endif %}
{% set itemFirst = false %}
{% endfor %}
</tr>
</table>