24

我有一个如下表格:

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 -^
    // ...
}

在编辑项目时,如何在渲染之前更改控制器内的表单字段值?

4

4 回答 4

69

您可以使用setData()Symfony 中的表单字段设置数据,如下所示:

$form = $formBuilder->getForm();

$form->get('firstname')->setData('John');
于 2013-07-04T22:09:27.223 回答
3

您可以在控制器中更改标签值:

$request = $this->get('request');
    if ($request->getMethod() == 'POST') 
{
    $form->bind($request);
    if $form->isValid())
    {
        $entity->setTags('test');
        //persist ...
    }
}
于 2014-08-28T12:58:57.483 回答
1
class ItemType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ...
            ->add('tags','text',array(
                'required' => false,
                'attr' => array('name' => 'tags','value'=>'newvalue'),
                'mapped' => false))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyBundle\ItemBundle\Entity\Item',
            'cascade_validation' => true,
        ));
    }
}
于 2013-07-04T21:38:53.530 回答
0

另一种设置值的方法是使用表单选项:

class ItemType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add('tags','text',array(
                'required' => false,
                'attr' => array('name' => 'tags'),
                'mapped' => false,
                'data' => $options['tags_value'],
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyBundle\ItemBundle\Entity\Item',
            'cascade_validation' => true,
            'tags_value' => 'default value if you want',
        ));
    }

然后在控制器中:

public function editAction(Request $request, $id) {
    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('MyBundleItemBundle:Item')->find($id);
    $form = $this->createForm(new ItemType(), $entity, ['tags_value' => 'test text']);
}
于 2018-06-01T11:07:10.177 回答