我有一个如下表格:
class ItemType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('tags','text',array(
'required' => false,
'attr' => array('name' => 'tags'),
'mapped' => false))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyBundle\ItemBundle\Entity\Item',
'cascade_validation' => true,
));
}
}
我的编辑动作
public function editAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBundleItemBundle:Item')->find($id);
$form = $this->createForm(new ItemType(), $entity);
//$form->get('tags')->setValue('test text');
// ^- this is what i would like to do -^
// ...
}
在编辑项目时,如何在渲染之前更改控制器内的表单字段值?