douyi7055 2015-09-30 08:56
浏览 51
已采纳

Symfony 2预填充表单,包含数据库中的数据

I am trying to populate form fields with data from the database in order to edit them. I already searched on google.

Here is my controller which returns empty fields

public function userViewAction($id,Request $request){

    $em = $this->getDoctrine()->getManager()->getRepository('BFVMailingBundle:MailingList');
    $user = $em->findById($id);

    $form = $this->get('form.factory')->createBuilder('form',$user)
        ->add('unsubscribed','checkbox')
        ->add('name','text')
        ->add('givenName','text')
        ->add('additionalName','text',array('required'=>false))
        ->add('familyName','text',array('required'=>false))
        ->add('emailValue','text')
        ->add('language','choice',array(
            'choices'  => array('en_GB' => 'en_GB', 'es_ES' => 'es_ES', 'fr_FR' => 'fr_FR'),
            'required' => true,
        ))
        ->add('commentary','textarea',array('required'=>false))
        ->add('save','submit')
        ->getForm();


    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // perform some action, such as save the object to the database
            $em->flush();
            return $this->redirect($this->generateUrl('user_view',array('id'=>$id)));
        }
    }

and this is my template

<div class="cell">  
    {{ form_start(form, {'attr': {'class': 'form-horizontal'}}) }}
    {{ form_end(form) }}
</div>

Did I miss something?

EDIT - READ THIS FOR THE SOLUTION

As John Noel Implied I build an externalised form with the command

php app/console doctrine:generate:form BFVMailingBundle:MailingList

my entity was MailingList instead of User

the MailingListType is a form template which is generated in BFV\MailingBundle\Form. I've added the data types myself.

<?php
namespace BFV\MailingBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class MailingListType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $str = date("U");
    $codeValue = sha1($str);

    $builder
        ->add('secretCode','hidden',array( 'data' => $codeValue ))
        ->add('name','text')
        ->add('givenName','text')
        ->add('additionalName','text',array('required'=>false))
        ->add('familyName','text',array('required'=>false))
        ->add('emailValue','text')
        ->add('language','choice',array(
            'choices'  => array('en_GB' => 'en_GB', 'es_ES' => 'es_ES', 'fr_FR' => 'fr_FR'),
            'required' => true,
        ))
        ->add('unsubscribed','checkbox')
        ->add('commentary','textarea',array('required'=>false))
        ->add('save','submit')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'BFV\MailingBundle\Entity\MailingList'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'bfv_mailingbundle_mailinglist';
}
}

In the reformated controller I add to add the the form generator the instance of MailingList $user[0] instead of $user. I read in many websites that usually you put $variable directly in the form builder but that generated the following error:

The form's view data is expected to be an instance of class 
BFV\MailingBundle\Entity\MailingList, but is a(n) array. You can avoid 
this error by setting the "data_class" option to null or by adding a
view transformer that transforms a(n) array to an instance of 
BFV\MailingBundle\Entity\MailingList

Thus in the controller:

    public function userViewAction($id,Request $request){
    if (!$id) {
        throw $this->createNotFoundException('No id !!');
    }

    $em = $this->getDoctrine()->getManager()->getRepository('BFVMailingBundle:MailingList');
    $user = $em->findById($id);
    if (!$user){
        throw $this->createNotFoundException('No user with the id selected');
    }

    $form = $this->createForm(new MailingListType(), $user[0]);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em->flush();
            return $this->redirect($this->generateUrl('user_view',array('id'=>$id)));
        }
    }

    return $this->render('BFVMailingBundle:Default:user_view.html.twig',array(
        'user'=>$user,
        'form'=>$form->createView()
            ));
}

Conclusion: I got the view form rendering with populated data from the database.

  • 写回答

2条回答 默认 最新

  • dqotv26286 2015-09-30 09:28
    关注

    To do this you'll want to look into form classes which will then act as a view (and will also populate) the data you provide. So in your example you'd create a form class UserType:

    class UserType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('unsubscribed','checkbox')
                ->add('name','text')
                ->add('givenName','text')
                ->add('additionalName','text',array('required'=>false))
                ->add('familyName','text',array('required'=>false))
                ->add('emailValue','text')
                ->add('language','choice',array(
                    'choices'  => array('en_GB' => 'en_GB', 'es_ES' => 'es_ES', 'fr_FR' => 'fr_FR'),
                    'required' => true,
                ))
                ->add('commentary','textarea',array('required'=>false))
                ->add('save','submit')
            ;
        }
    
        public function getName()
        {
            return 'user';
        }
    
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Your\Entity\Class',
            ));
        }
    }
    

    Then within your controller you'd do something along the lines of:

    $form = $this->createForm(new UserType(), $user);
    

    Then the rest of your controller as you have. Definitely read up on form classes though as that's the starting point for a lot of the advanced functionality of Symfony forms.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容