将对象绑定到Symfony窗体
我有一个预先填充了一些数据的实体,我想在表单上显示它们:这里state是活动的,customerId=1将对象绑定到Symfony窗体,symfony,Symfony,我有一个预先填充了一些数据的实体,我想在表单上显示它们:这里state是活动的,customerId=1 看来提交不是正确的绑定函数。我希望表单会相应地预先填写,在POST请求之后,我有一个更新的BenFile实体。您可以使用createForm方法轻松完成: // in the controller public function index(Request $request) { $file = new BenFile(); $file->setCustomerId(1
看来提交不是正确的绑定函数。我希望表单会相应地预先填写,在POST请求之后,我有一个更新的BenFile实体。您可以使用createForm方法轻松完成:
// in the controller
public function index(Request $request)
{
$file = new BenFile();
$file->setCustomerId(1);
$file->setState('ACTIVE');
$form = $this->createForm("ben-file-create", $file);
// handle submit
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// flush entity
$fileNew = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($fileNew );
$entityManager->flush();
}
return $this->render('create-file', array());
}
您还可以在BenFile构造函数中设置这些值,因此表单将以活动状态呈现,假设他的状态一开始总是活动的,那么CustomerId可能是登录用户的值?
// in the controller
public function index(Request $request)
{
$file = new BenFile();
$file->setCustomerId(1);
$file->setState('ACTIVE');
$form = $this->createForm("ben-file-create", $file);
// handle submit
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// flush entity
$fileNew = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($fileNew );
$entityManager->flush();
}
return $this->render('create-file', array());
}