如何使用数据库中的数据预填充 symfony 中的文本字段。我在主机表中有一个名为 hostFee 的字段,当我创建表单时,我希望该数据预先填充此文本字段。
我正在为新的 BookingSpecsType() 创建一个表单...
这是我的表单构建器元素。
$builder->add('hostFee', 'text', array(
'required'=>false,
'error_bubbling'=>true,
'label'=>'Do you charge a hosting fee?',
'data' => '??????? (How do I fill this text field dynamically with the Host table hostFee column data) ?????',
'attr'=>array(
'placeholder'=>'If yes, enter dollar amount $0.00',
'class'=>'form-control'
)
));
Run Code Online (Sandbox Code Playgroud)
谢谢。
小智 5
该文档提供了许多示例。
当您$this->createForm在 Controller 操作中使用时,第二个参数允许您将表单与对象结合。
例如:
public function editAction()
{
$user = $this->getDoctrine()->getRepository('User')->find(1); // YOUR OBJECT RETRIEVED FROM THE DB FOR EXAMPLE
$form = $this->createForm(new EditType(), $user, array(
'action' => $this->generateUrl('account_edit'),
));
return $this->render(
'AcmeAccountBundle:Account:edit.html.twig',
array('form' => $form->createView())
);
}
Run Code Online (Sandbox Code Playgroud)