我有2个表单,显示以下内容:
它们是动态创建的,而不是来自实体。
我的控制器如下:
<?php
...create the relevant array $rolesListForForm
$form = $this->createFormBuilder()->getForm(); //create empty form
$HRsInThisProject = $em->getRepository('AppBundle:User')->findUsersByProject($prj);
foreach ($HRsInThisProject as $key => $HR)
{
$form->add('roleOf_'.$key, 'choice', array('choices' => $rolesListForForm, 'required' => true, 'data' => $HR['role'], 'label' => false, ));
$form->add('isActive_'.$key, 'choice', array('choices' => [0 => 'Inactive', 1 => 'Active'] , 'required' => true, 'data' => $HR['is_active'], 'label' => false, ));
}
...create the relevant array $HRsInMyDomainForForm
$form->add('showinactive', 'checkbox', array('label' => 'Show inactive users', 'required' => false, 'data' => true, ));
$form->add('HRid', 'choice', array('choices' => $HRsInMyDomainForForm,'required' => false, 'placeholder' => 'Choose a resource', 'label' => false, ));
$form->add('role', 'choice', array('choices' => $rolesListForForm,'required' => false, 'placeholder' => 'Choose a role', 'label' => false, ));
$form->add('save', 'submit', array('label' => 'Save'));
$form->handleRequest($request);
// If form is valid
if ($form->isValid())
{
...lots of logic here to understand if persisting or not stuff on DB, then I simply refresh the current page
return $this->redirect($this->generateUrl('hr_manage', array('projectID' => $prj->getId())));
}
//creation of the second form
$formGlobalSearch=$this->createFormBuilder()
->add('username', 'text', array('constraints' => new UsernameExists()))
->add('role', 'choice', array('choices' => $rolesListForForm,'required' => false, 'placeholder' => 'Choose a role', 'label' => false, ))
->add('submitUsername', 'submit', array('label' => 'Search username globally'))
->getForm();
$formGlobalSearch->handleRequest($request);
if ($formGlobalSearch->isValid())
{
//persist stuff on DB and refresh current page
return $this->redirect($this->generateUrl('hr_manage', array('projectID' => $prj->getId())));
}
return $this->render('HR/show.html.twig', array('projectID' => $prj->getId(), 'hrlist' => $HRsInMyDomain, 'form' => $form->createView(), 'HRs' => $HRsInThisProject, 'formGlobalSearch' => $formGlobalSearch->createView(),));
一切顺利,唯一的问题发生在第二种形式抛出验证错误时。如果它没有正确呈现顶部形式,我看到这样的东西:
以下是树枝的相关部分:
{{ form_start(form) }}
{{ form_row(form.showinactive) }}
<table>
<tr>
<td>User ID</td>
<td>Name</td>
<td>Role</td>
<td>Started</td>
<td>Status</td>
</tr>
{% for key, HR in HRs %}
<tr>
<td>{{ key }}</td>
<td>{{ HR.name }}</td>
<td>{{ form_row(form['roleOf_' ~ key]) }}</td>
<td>{{ HR.started_at|date('Y-m-d H:i:s') }}</td>
<td>{{ form_row(form['isActive_' ~ key]) }}</td>
</tr>
{% endfor %}
</table>
<br>
Add new resource:
{{ form_row(form.HRid) }}{{ form_row(form.role) }}
{{ form_row(form.save) }}
{{ form_end(form) }}
<br>
Or you can search for a user of ProJacked at global level
{{ form_start(formGlobalSearch) }}
{{ form_errors(formGlobalSearch) }}
{{ form_widget(formGlobalSearch) }}
{{ form_end(formGlobalSearch) }}
有什么想法吗?