php - Symfony 2 prefilled form with data from the database -


i trying populate form fields data database in order edit them. searched on google.

here controller 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 action, such save object database             $em->flush();             return $this->redirect($this->generateurl('user_view',array('id'=>$id)));         }     } 

and template

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

did miss something?

edit - read solution

as john noel implied build externalised form command

php app/console doctrine:generate:form bfvmailingbundle:mailinglist 

my entity mailinglist instead of user

the mailinglisttype form template generated in bfv\mailingbundle\form. i've added 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 reformated controller add add the form generator instance of mailinglist $user[0] instead of $user. read in many websites put $variable directly in form builder generated following error:

the form's view data expected instance of class  bfv\mailingbundle\entity\mailinglist, a(n) array. can avoid  error setting "data_class" option null or adding view transformer transforms a(n) array instance of  bfv\mailingbundle\entity\mailinglist 

thus in 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 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: got view form rendering populated data database.

to you'll want form classes act view (and populate) data provide. in example you'd create 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 controller you'd along lines of:

$form = $this->createform(new usertype(), $user); 

then rest of controller have. read on form classes though that's starting point lot of advanced functionality of symfony forms.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -