2015-09-30 131 views
0

我试图用数据库中的数据填充表单域以编辑它们。我已经在谷歌搜索。Symfony 2预填充表单与来自数据库的数据

这里是我的控制器,它返回空场

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

,这是我的模板

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

我错过了什么?

EDIT - 读这为解决

正如约翰·诺尔引伸我建立外部化形式与命令

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

我的实体是不是用户

邮件列表MailingListType是在BFV \ MailingBundle \ Form中生成的表单模板。我自己添加了数据类型。

<?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'; 
} 
} 

在重新格式化的控制器中,我添加了添加表单生成器MailingList $ user [0]而不是$ user的实例。我在许多平时你直接把$变量的表单生成器的网站阅读,但生成以下错误:在控制器

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 

这样:

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

结论:我认为形式呈现来自数据库的填充数据。

+0

http://symfony.com/doc/current/book/forms.html#book-forms-data-class – pbenard

+0

你在google上发现了什么? – Bas

+0

当你做'转储($用户)'你看到任何记录被拉对用户? – Baig

回答

1

要做到这一点,你会想看看form classes那么这将作为一个视图(也将填充),您所提供的数据。所以,在你的榜样,你需要创建一个表单类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', 
     )); 
    } 
} 

那么你的控制器中你会做线沿线的东西:

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

然后控制器的其余部分,你有。当然,你可以在表单类上阅读,因为这是Symfony forms许多高级功能的起点。

+0

我试过这个,它在你创建一个电子邮件用户时运行良好,但是当试图从现有数据创建表单时返回这个错误“表单的视图数据应该是BFV类的一个实例\ MailingBundle \ Entity \ MailingList,但是是一个(n)数组,您可以通过将“data_class”选项设置为null或者添加一个将(n)数组转换为BFV \ MailingBundle \ Entity \ MailingList“ –

+0

最后它工作我用$ user [0]替换$用户返回对象,因为错误说 –

0

我不知道这是什么使区别,但你有尝试:

$form = $this->createFormBuilder($user) 
      ->add(...) 
      ->getForm() 

你也可以检查你的用户是正确地从数据库读取。

例如:

if (!is_object($user)) { 
    $this->createNotFoundException('The user does not exist'); 
} 
+0

感谢您的答案。数据用户从数据库中回想起来没有问题。和你尝试的小试验provoqued一个错误 –