How to change form field value in symfony 2

懵懂的女人 提交于 2019-12-21 03:33:13

问题


I have a form like below:

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,
        ));
    }
}

My edit action

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

While I am editing an item, How can I change form field value inside controller before rendering it?


回答1:


You can use setData() to set data on form fields in Symfony, like this:

$form = $formBuilder->getForm();

$form->get('firstname')->setData('John');



回答2:


You can change tags value in the controller:

$request = $this->get('request');
    if ($request->getMethod() == 'POST') 
{
    $form->bind($request);
    if $form->isValid())
    {
        $entity->setTags('test');
        //persist ...
    }
}



回答3:


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,
        ));
    }
}



回答4:


Another way to set the value is to use form options:

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',
        ));
    }

And then in controller:

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']);
}


来源:https://stackoverflow.com/questions/17477572/how-to-change-form-field-value-in-symfony-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!